Exemplo n.º 1
0
        /// <summary>
        /// Returns a weak reference to an object, or <see langword="null"/>
        /// if the argument is invalid.
        /// A weak reference to an object is not enough to keep the object alive:
        /// when the only remaining references to a referent are weak references,
        /// garbage collection is free to destroy the referent and reuse its memory
        /// for something else. However, until the object is actually destroyed the
        /// weak reference may return the object even if there are no strong references
        /// to it.
        /// </summary>
        /// <param name="obj">The object.</param>
        /// <returns>
        /// The <see cref="WeakRef"/> reference to the object or <see langword="null"/>.
        /// </returns>
        public static WeakRef WeakRef(Object obj)
        {
            if (!IsInstanceValid(obj))
            {
                return(null);
            }

            NativeFuncs.godotsharp_weakref(GetPtr(obj), out godot_ref weakRef);
            using (weakRef)
            {
                if (weakRef.IsNull)
                {
                    return(null);
                }

                return((WeakRef)InteropUtils.UnmanagedGetManaged(weakRef.Reference));
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Returns the <see cref="Object"/> that corresponds to <paramref name="instanceId"/>.
 /// All Objects have a unique instance ID.
 /// </summary>
 /// <example>
 /// <code>
 /// public class MyNode : Node
 /// {
 ///     public string foo = "bar";
 ///
 ///     public override void _Ready()
 ///     {
 ///         ulong id = GetInstanceId();
 ///         var inst = (MyNode)GD.InstanceFromId(Id);
 ///         GD.Print(inst.foo); // Prints bar
 ///     }
 /// }
 /// </code>
 /// </example>
 /// <param name="instanceId">Instance ID of the Object to retrieve.</param>
 /// <returns>The <see cref="Object"/> instance.</returns>
 public static Object InstanceFromId(ulong instanceId)
 {
     return(InteropUtils.UnmanagedGetManaged(NativeFuncs.godotsharp_instance_from_id(instanceId)));
 }