Skip to content

Commit

Permalink
Add InstanceId::lookup_validity()
Browse files Browse the repository at this point in the history
  • Loading branch information
Bromeon committed Sep 17, 2024
1 parent ccd67dc commit 8b37feb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
3 changes: 3 additions & 0 deletions godot-core/src/obj/gd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,12 @@ impl<T: GodotClass> Gd<T> {

/// ⚠️ Looks up the given instance ID and returns the associated object.
///
/// Corresponds to Godot's global function `instance_from_id()`.
///
/// # Panics
/// If no such instance ID is registered, or if the dynamic type of the object behind that instance ID
/// is not compatible with `T`.
#[doc(alias = "instance_from_id")]
pub fn from_instance_id(instance_id: InstanceId) -> Self {
Self::try_from_instance_id(instance_id).unwrap_or_else(|err| {
panic!(
Expand Down
11 changes: 11 additions & 0 deletions godot-core/src/obj/instance_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ impl InstanceId {
self.to_u64() & (1u64 << 63) != 0
}

/// Dynamically checks if the instance behind the ID exists.
///
/// Rather slow, involves engine round-trip plus object DB lookup. If you need the object, use
/// [`Gd::from_instance_id()`][crate::obj::Gd::from_instance_id] instead.
///
/// This corresponds to Godot's global function `is_instance_id_valid()`.
#[doc(alias = "is_instance_id_valid")]
pub fn lookup_validity(self) -> bool {
crate::gen::utilities::is_instance_id_valid(self.to_i64())
}

// Private: see rationale above
pub(crate) fn to_u64(self) -> u64 {
self.value.get()
Expand Down

0 comments on commit 8b37feb

Please sign in to comment.