Beispiel #1
0
 /// <summary>
 /// Converts one or more arguments of any type to string in the best way possible.
 /// </summary>
 /// <param name="what">Arguments that will converted to string.</param>
 /// <returns>The string formed by the given arguments.</returns>
 public static string Str(params Variant[] what)
 {
     using var whatGodot = new Godot.Collections.Array(what);
     NativeFuncs.godotsharp_str((godot_array)whatGodot.NativeValue, out godot_string ret);
     using (ret)
         return(Marshaling.ConvertStringToManaged(ret));
 }
Beispiel #2
0
        /// <summary>
        /// Calls the method represented by this <see cref="Callable"/>.
        /// Arguments can be passed and should match the method's signature.
        /// </summary>
        /// <param name="args">Arguments that will be passed to the method call.</param>
        /// <returns>The value returned by the method.</returns>
        public unsafe Variant Call(params Variant[] args)
        {
            using godot_callable callable = Marshaling.ConvertCallableToNative(this);

            int argc = args.Length;

            Span <godot_variant.movable> argsStoreSpan = argc <= VarArgsSpanThreshold ?
                                                         stackalloc godot_variant.movable[VarArgsSpanThreshold].Cleared() :
                                                         new godot_variant.movable[argc];

            Span <IntPtr> argsSpan = argc <= 10 ?
                                     stackalloc IntPtr[argc] :
                                     new IntPtr[argc];

            using var variantSpanDisposer = new VariantSpanDisposer(argsStoreSpan);

            fixed(godot_variant *varargs = &MemoryMarshal.GetReference(argsStoreSpan).DangerousSelfRef)
            fixed(IntPtr * argsPtr = &MemoryMarshal.GetReference(argsSpan))
            {
                for (int i = 0; i < argc; i++)
                {
                    varargs[i] = (godot_variant)args[i].NativeVar;
                    argsPtr[i] = new IntPtr(&varargs[i]);
                }

                godot_variant ret = NativeFuncs.godotsharp_callable_call(callable,
                                                                         (godot_variant **)argsPtr, argc, out _);

                return(Variant.CreateTakingOwnershipOfDisposableValue(ret));
            }
        }
Beispiel #3
0
        /// <summary>
        /// Prints one or more arguments to strings in the best way possible to console.
        /// No newline is added at the end.
        ///
        /// Note: Due to limitations with Godot's built-in console, this only prints to the terminal.
        /// If you need to print in the editor, use another method, such as <see cref="Print(object[])"/>.
        /// </summary>
        /// <example>
        /// <code>
        /// GD.PrintRaw("A");
        /// GD.PrintRaw("B");
        /// // Prints AB
        /// </code>
        /// </example>
        /// <param name="what">Arguments that will be printed.</param>
        public static void PrintRaw(params object[] what)
        {
            string str = string.Concat(GetPrintParams(what));

            using var godotStr = Marshaling.ConvertStringToNative(str);
            NativeFuncs.godotsharp_printraw(godotStr);
        }
Beispiel #4
0
 private Dictionary(godot_dictionary nativeValueToOwn)
 {
     NativeValue = (godot_dictionary.movable)(nativeValueToOwn.IsAllocated ?
                                              nativeValueToOwn :
                                              NativeFuncs.godotsharp_dictionary_new());
     _weakReferenceToSelf = DisposablesTracker.RegisterDisposable(this);
 }
Beispiel #5
0
        /// <summary>
        /// Searches this <see cref="Array"/> for an item
        /// and returns its index or -1 if not found.
        /// </summary>
        /// <param name="item">The <see cref="Variant"/> item to search for.</param>
        /// <returns>The index of the item, or -1 if not found.</returns>
        public int IndexOf(Variant item)
        {
            godot_variant variantValue = (godot_variant)item.NativeVar;
            var           self         = (godot_array)NativeValue;

            return(NativeFuncs.godotsharp_array_index_of(ref self, variantValue));
        }
Beispiel #6
0
        /// <summary>
        /// Prints one or more arguments to the console with a tab between each argument.
        /// </summary>
        /// <example>
        /// <code>
        /// GD.PrintT("A", "B", "C"); // Prints A       B       C
        /// </code>
        /// </example>
        /// <param name="what">Arguments that will be printed.</param>
        public static void PrintT(params object[] what)
        {
            string str = string.Join('\t', GetPrintParams(what));

            using var godotStr = Marshaling.ConvertStringToNative(str);
            NativeFuncs.godotsharp_printt(godotStr);
        }
Beispiel #7
0
        internal static unsafe godot_bool Set(IntPtr godotObjectGCHandle, godot_string_name *name, godot_variant *value)
        {
            try
            {
                var godotObject = (Object)GCHandle.FromIntPtr(godotObjectGCHandle).Target;

                if (godotObject == null)
                {
                    throw new InvalidOperationException();
                }

                if (godotObject.SetGodotClassPropertyValue(CustomUnsafe.AsRef(name), CustomUnsafe.AsRef(value)))
                {
                    return(godot_bool.True);
                }

                var nameManaged = StringName.CreateTakingOwnershipOfDisposableValue(
                    NativeFuncs.godotsharp_string_name_new_copy(CustomUnsafe.AsRef(name)));

                Variant valueManaged = Variant.CreateCopyingBorrowed(*value);

                return(godotObject._Set(nameManaged, valueManaged).ToGodotBool());
            }
            catch (Exception e)
            {
                ExceptionUtils.LogException(e);
                return(godot_bool.False);
            }
        }
Beispiel #8
0
        /// <summary>
        /// Adds an item to the end of this <see cref="Array"/>.
        /// This is the same as <c>append</c> or <c>push_back</c> in GDScript.
        /// </summary>
        /// <param name="item">The <see cref="Variant"/> item to add.</param>
        public void Add(Variant item)
        {
            godot_variant variantValue = (godot_variant)item.NativeVar;
            var           self         = (godot_array)NativeValue;

            _ = NativeFuncs.godotsharp_array_add(ref self, variantValue);
        }
Beispiel #9
0
        /// <summary>
        /// Gets the resource or property name indicated by <paramref name="idx"/> (0 to <see cref="GetSubNameCount"/>).
        /// </summary>
        /// <param name="idx">The subname index.</param>
        /// <returns>The subname at the given index <paramref name="idx"/>.</returns>
        public string GetSubName(int idx)
        {
            var self = (godot_node_path)NativeValue;

            NativeFuncs.godotsharp_node_path_get_subname(self, idx, out godot_string subName);
            using (subName)
                return(Marshaling.ConvertStringToManaged(subName));
        }
Beispiel #10
0
        /// <summary>
        /// Returns all subnames concatenated with a colon character (<c>:</c>)
        /// as separator, i.e. the right side of the first colon in a node path.
        /// </summary>
        /// <example>
        /// <code>
        /// var nodepath = new NodePath("Path2D/PathFollow2D/Sprite2D:texture:load_path");
        /// GD.Print(nodepath.GetConcatenatedSubnames()); // texture:load_path
        /// </code>
        /// </example>
        /// <returns>The subnames concatenated with <c>:</c>.</returns>
        public string GetConcatenatedSubNames()
        {
            var self = (godot_node_path)NativeValue;

            NativeFuncs.godotsharp_node_path_get_concatenated_subnames(self, out godot_string subNames);
            using (subNames)
                return(Marshaling.ConvertStringToManaged(subNames));
        }
Beispiel #11
0
        /// <summary>
        /// Returns a node path with a colon character (<c>:</c>) prepended,
        /// transforming it to a pure property path with no node name (defaults
        /// to resolving from the current node).
        /// </summary>
        /// <example>
        /// <code>
        /// // This will be parsed as a node path to the "x" property in the "position" node.
        /// var nodePath = new NodePath("position:x");
        /// // This will be parsed as a node path to the "x" component of the "position" property in the current node.
        /// NodePath propertyPath = nodePath.GetAsPropertyPath();
        /// GD.Print(propertyPath); // :position:x
        /// </code>
        /// </example>
        /// <returns>The <see cref="NodePath"/> as a pure property path.</returns>
        public NodePath GetAsPropertyPath()
        {
            godot_node_path propertyPath = default;
            var             self         = (godot_node_path)NativeValue;

            NativeFuncs.godotsharp_node_path_get_as_property_path(self, ref propertyPath);
            return(CreateTakingOwnershipOfDisposableValue(propertyPath));
        }
Beispiel #12
0
 /// <summary>
 /// Constructs a <see cref="NodePath"/> from a string <paramref name="path"/>,
 /// e.g.: <c>"Path2D/PathFollow2D/Sprite2D:texture:size"</c>.
 /// A path is absolute if it starts with a slash. Absolute paths
 /// are only valid in the global scene tree, not within individual
 /// scenes. In a relative path, <c>"."</c> and <c>".."</c> indicate
 /// the current node and its parent.
 /// The "subnames" optionally included after the path to the target
 /// node can point to resources or properties, and can also be nested.
 /// </summary>
 /// <example>
 /// Examples of valid NodePaths (assuming that those nodes exist and
 /// have the referenced resources or properties):
 /// <code>
 /// // Points to the Sprite2D node.
 /// "Path2D/PathFollow2D/Sprite2D"
 /// // Points to the Sprite2D node and its "texture" resource.
 /// // GetNode() would retrieve "Sprite2D", while GetNodeAndResource()
 /// // would retrieve both the Sprite2D node and the "texture" resource.
 /// "Path2D/PathFollow2D/Sprite2D:texture"
 /// // Points to the Sprite2D node and its "position" property.
 /// "Path2D/PathFollow2D/Sprite2D:position"
 /// // Points to the Sprite2D node and the "x" component of its "position" property.
 /// "Path2D/PathFollow2D/Sprite2D:position:x"
 /// // Absolute path (from "root")
 /// "/root/Level/Path2D"
 /// </code>
 /// </example>
 /// <param name="path">A string that represents a path in a scene tree.</param>
 public NodePath(string path)
 {
     if (!string.IsNullOrEmpty(path))
     {
         NativeValue          = (godot_node_path.movable)NativeFuncs.godotsharp_node_path_new_from_string(path);
         _weakReferenceToSelf = DisposablesTracker.RegisterDisposable(this);
     }
 }
Beispiel #13
0
 /// <summary>
 /// Constructs a <see cref="StringName"/> from the given <paramref name="name"/> string.
 /// </summary>
 /// <param name="name">String to construct the <see cref="StringName"/> from.</param>
 public StringName(string name)
 {
     if (!string.IsNullOrEmpty(name))
     {
         NativeValue          = (godot_string_name.movable)NativeFuncs.godotsharp_string_name_new_from_string(name);
         _weakReferenceToSelf = DisposablesTracker.RegisterDisposable(this);
     }
 }
Beispiel #14
0
        /// <summary>
        /// Duplicates this <see cref="Dictionary"/>.
        /// </summary>
        /// <param name="deep">If <see langword="true"/>, performs a deep copy.</param>
        /// <returns>A new Godot Dictionary.</returns>
        public Dictionary Duplicate(bool deep = false)
        {
            godot_dictionary newDictionary;
            var self = (godot_dictionary)NativeValue;

            NativeFuncs.godotsharp_dictionary_duplicate(ref self, deep.ToGodotBool(), out newDictionary);
            return(CreateTakingOwnershipOfDisposableValue(newDictionary));
        }
Beispiel #15
0
 public unsafe void Dispose()
 {
     if (_ptr == null)
     {
         return;
     }
     NativeFuncs.godotsharp_stack_info_vector_destroy(ref this);
     _ptr = null;
 }
Beispiel #16
0
        public SignalAwaiter(Object source, StringName signal, Object target)
        {
            var awaiterGcHandle = CustomGCHandle.AllocStrong(this);

            using godot_string_name signalSrc = NativeFuncs.godotsharp_string_name_new_copy(
                      (godot_string_name)(signal?.NativeValue ?? default));
            NativeFuncs.godotsharp_internal_signal_awaiter_connect(Object.GetPtr(source), in signalSrc,
                                                                   Object.GetPtr(target), GCHandle.ToIntPtr(awaiterGcHandle));
        }
Beispiel #17
0
        /// <summary>
        /// Removes an element from this <see cref="Array"/> by index.
        /// </summary>
        /// <param name="index">The index of the element to remove.</param>
        public void RemoveAt(int index)
        {
            if (index < 0 || index > Count)
            {
                throw new ArgumentOutOfRangeException(nameof(index));
            }

            var self = (godot_array)NativeValue;

            NativeFuncs.godotsharp_array_remove_at(ref self, index);
        }
Beispiel #18
0
        /// <summary>
        /// Inserts a new item at a given position in the array.
        /// The position must be a valid position of an existing item,
        /// or the position at the end of the array.
        /// Existing items will be moved to the right.
        /// </summary>
        /// <param name="index">The index to insert at.</param>
        /// <param name="item">The <see cref="Variant"/> item to insert.</param>
        public void Insert(int index, Variant item)
        {
            if (index < 0 || index > Count)
            {
                throw new ArgumentOutOfRangeException(nameof(index));
            }

            godot_variant variantValue = (godot_variant)item.NativeVar;
            var           self         = (godot_array)NativeValue;

            NativeFuncs.godotsharp_array_insert(ref self, index, variantValue);
        }
Beispiel #19
0
            public void Resize(int size)
            {
                if (size < 0)
                {
                    throw new ArgumentOutOfRangeException(nameof(size));
                }
                var err = NativeFuncs.godotsharp_stack_info_vector_resize(ref this, size);

                if (err != Error.Ok)
                {
                    throw new InvalidOperationException("Failed to resize vector. Error code is: " + err.ToString());
                }
            }
Beispiel #20
0
        private (Array keys, Array values, int count) GetKeyValuePairs()
        {
            var self = (godot_dictionary)NativeValue;

            godot_array keysArray;

            NativeFuncs.godotsharp_dictionary_keys(ref self, out keysArray);
            var keys = Array.CreateTakingOwnershipOfDisposableValue(keysArray);

            godot_array valuesArray;

            NativeFuncs.godotsharp_dictionary_keys(ref self, out valuesArray);
            var values = Array.CreateTakingOwnershipOfDisposableValue(valuesArray);

            int count = NativeFuncs.godotsharp_dictionary_count(ref self);

            return(keys, values, count);
        }
Beispiel #21
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));
            }
        }
Beispiel #22
0
        public Array(Span <NodePath> array) : this()
        {
            if (array == null)
            {
                throw new ArgumentNullException(nameof(array));
            }

            NativeValue          = (godot_array.movable)NativeFuncs.godotsharp_array_new();
            _weakReferenceToSelf = DisposablesTracker.RegisterDisposable(this);

            int length = array.Length;

            Resize(length);

            for (int i = 0; i < length; i++)
            {
                this[i] = array[i];
            }
        }
Beispiel #23
0
 /// <summary>
 /// Returns the item at the given <paramref name="index"/>.
 /// </summary>
 /// <value>The <see cref="Variant"/> item at the given <paramref name="index"/>.</value>
 public unsafe Variant this[int index]
 {
     get
     {
         GetVariantBorrowElementAt(index, out godot_variant borrowElem);
         return(Variant.CreateCopyingBorrowed(borrowElem));
     }
     set
     {
         if (index < 0 || index >= Count)
         {
             throw new ArgumentOutOfRangeException(nameof(index));
         }
         var            self    = (godot_array)NativeValue;
         godot_variant *ptrw    = NativeFuncs.godotsharp_array_ptrw(ref self);
         godot_variant *itemPtr = &ptrw[index];
         (*itemPtr).Dispose();
         *itemPtr = value.CopyNativeVariant();
     }
 }
Beispiel #24
0
        internal static unsafe godot_bool Get(IntPtr godotObjectGCHandle, godot_string_name *name,
                                              godot_variant *outRet)
        {
            try
            {
                var godotObject = (Object)GCHandle.FromIntPtr(godotObjectGCHandle).Target;

                if (godotObject == null)
                {
                    throw new InvalidOperationException();
                }

                if (godotObject.GetGodotClassPropertyValue(CustomUnsafe.AsRef(name), out godot_variant outRetValue))
                {
                    *outRet = outRetValue;
                    return(godot_bool.True);
                }

                var nameManaged = StringName.CreateTakingOwnershipOfDisposableValue(
                    NativeFuncs.godotsharp_string_name_new_copy(CustomUnsafe.AsRef(name)));

                Variant ret = godotObject._Get(nameManaged);

                if (ret.VariantType == Variant.Type.Nil)
                {
                    *outRet = default;
                    return(godot_bool.False);
                }

                *outRet = Marshaling.ConvertManagedObjectToVariant(ret);
                return(godot_bool.True);
            }
            catch (Exception e)
            {
                ExceptionUtils.LogException(e);
                *outRet = default;
                return(godot_bool.False);
            }
        }
Beispiel #25
0
        /// <summary>
        /// Resizes this <see cref="Array"/> to the given size.
        /// </summary>
        /// <param name="newSize">The new size of the array.</param>
        /// <returns><see cref="Error.Ok"/> if successful, or an error code.</returns>
        public Error Resize(int newSize)
        {
            var self = (godot_array)NativeValue;

            return(NativeFuncs.godotsharp_array_resize(ref self, newSize));
        }
Beispiel #26
0
 /// <summary>
 /// Constructs a new empty <see cref="Dictionary"/>.
 /// </summary>
 public Dictionary()
 {
     NativeValue          = (godot_dictionary.movable)NativeFuncs.godotsharp_dictionary_new();
     _weakReferenceToSelf = DisposablesTracker.RegisterDisposable(this);
 }
Beispiel #27
0
        /// <summary>
        /// Gets the number of resource or property names ("subnames") in the path.
        /// Each subname is listed after a colon character (<c>:</c>) in the node path.
        /// For example, <c>"Path2D/PathFollow2D/Sprite2D:texture:load_path"</c> has 2 subnames.
        /// </summary>
        /// <returns>The number of subnames in the path.</returns>
        public int GetSubNameCount()
        {
            var self = (godot_node_path)NativeValue;

            return(NativeFuncs.godotsharp_node_path_get_subname_count(self));
        }
Beispiel #28
0
        /// <summary>
        /// Returns <see langword="true"/> if the node path is absolute (as opposed to relative),
        /// which means that it starts with a slash character (<c>/</c>). Absolute node paths can
        /// be used to access the root node (<c>"/root"</c>) or autoloads (e.g. <c>"/global"</c>
        /// if a "global" autoload was registered).
        /// </summary>
        /// <returns>If the <see cref="NodePath"/> is an absolute path.</returns>
        public bool IsAbsolute()
        {
            var self = (godot_node_path)NativeValue;

            return(NativeFuncs.godotsharp_node_path_is_absolute(self).ToBool());
        }
Beispiel #29
0
        /// <summary>
        /// Shuffles the contents of this <see cref="Array"/> into a random order.
        /// </summary>
        public void Shuffle()
        {
            var self = (godot_array)NativeValue;

            NativeFuncs.godotsharp_array_shuffle(ref self);
        }
Beispiel #30
0
 /// <summary>
 /// Constructs a new empty <see cref="Array"/>.
 /// </summary>
 public Array()
 {
     NativeValue          = (godot_array.movable)NativeFuncs.godotsharp_array_new();
     _weakReferenceToSelf = DisposablesTracker.RegisterDisposable(this);
 }