using System; using System.Collections.Generic; using System.Diagnostics; using System.Text; using Godot; /// /// Godot's global functions. /// public static class GD { /// /// Decodes a byte array back to a value, without decoding objects. /// Note: If you need object deserialization, see . /// /// Byte array that will be decoded to a . /// The decoded . public static Variant BytesToVar(Span bytes) => Godot.GD.BytesToVar(bytes); /// /// Decodes a byte array back to a value. Decoding objects is allowed. /// Warning: Deserialized object can contain code which gets executed. Do not use this /// option if the serialized object comes from untrusted sources to avoid potential security /// threats (remote code execution). /// /// Byte array that will be decoded to a . /// The decoded . public static Variant BytesToVarWithObjects(Span bytes) => Godot.GD.BytesToVarWithObjects(bytes); /// /// Converts to in the best way possible. /// The parameter uses the values. /// /// /// /// Variant a = new Godot.Collections.Array { 4, 2.5, 1.2 }; /// GD.Print(a.VariantType == Variant.Type.Array); // Prints true /// /// var b = GD.Convert(a, Variant.Type.PackedByteArray); /// GD.Print(b); // Prints [4, 2, 1] /// GD.Print(b.VariantType == Variant.Type.Array); // Prints false /// /// /// The Variant converted to the given . public static Variant Convert(Variant what, Variant.Type type) => Godot.GD.Convert(what, type); /// /// Returns the integer hash of the passed . /// /// /// /// GD.Print(GD.Hash("a")); // Prints 177670 /// /// /// Variable that will be hashed. /// Hash of the variable passed. public static int Hash(Variant var) => Godot.GD.Hash(var); /// /// Loads a resource from the filesystem located at . /// The resource is loaded on the method call (unless it's referenced already /// elsewhere, e.g. in another script or in the scene), which might cause slight delay, /// especially when loading scenes. To avoid unnecessary delays when loading something /// multiple times, either store the resource in a variable. /// /// Note: Resource paths can be obtained by right-clicking on a resource in the FileSystem /// dock and choosing "Copy Path" or by dragging the file from the FileSystem dock into the script. /// /// Important: The path must be absolute, a local path will just return . /// This method is a simplified version of , which can be used /// for more advanced scenarios. /// /// /// /// // Load a scene called main located in the root of the project directory and cache it in a variable. /// var main = GD.Load("res://main.tscn"); // main will contain a PackedScene resource. /// /// /// Path of the to load. /// The loaded . public static Resource Load(string path) => Godot.GD.Load(path); /// /// Loads a resource from the filesystem located at . /// The resource is loaded on the method call (unless it's referenced already /// elsewhere, e.g. in another script or in the scene), which might cause slight delay, /// especially when loading scenes. To avoid unnecessary delays when loading something /// multiple times, either store the resource in a variable. /// /// Note: Resource paths can be obtained by right-clicking on a resource in the FileSystem /// dock and choosing "Copy Path" or by dragging the file from the FileSystem dock into the script. /// /// Important: The path must be absolute, a local path will just return . /// This method is a simplified version of , which can be used /// for more advanced scenarios. /// /// /// /// // Load a scene called main located in the root of the project directory and cache it in a variable. /// var main = GD.Load<PackedScene>("res://main.tscn"); // main will contain a PackedScene resource. /// /// /// Path of the to load. /// The type to cast to. Should be a descendant of . public static T Load(string path) where T : class => Godot.GD.Load(path); private static string AppendPrintParams(object[] parameters) { if (parameters == null) { return "null"; } var sb = new StringBuilder(); for (int i = 0; i < parameters.Length; i++) { sb.Append(parameters[i]?.ToString() ?? "null"); } return sb.ToString(); } private static string AppendPrintParams(char separator, object[] parameters) { if (parameters == null) { return "null"; } var sb = new StringBuilder(); for (int i = 0; i < parameters.Length; i++) { if (i != 0) sb.Append(separator); sb.Append(parameters[i]?.ToString() ?? "null"); } return sb.ToString(); } /// /// Prints a message to the console. /// /// Note: Consider using and /// to print error and warning messages instead of . /// This distinguishes them from print messages used for debugging purposes, /// while also displaying a stack trace when an error or warning is printed. /// /// Message that will be printed. public static void Print(string what) { Debug.WriteLine("Info: " + what); Godot.GD.Print(what); } /// /// Converts one or more arguments of any type to string in the best way possible /// and prints them to the console. /// /// Note: Consider using and /// to print error and warning messages instead of . /// This distinguishes them from print messages used for debugging purposes, /// while also displaying a stack trace when an error or warning is printed. /// /// /// /// var a = new Godot.Collections.Array { 1, 2, 3 }; /// GD.Print("a", "b", a); // Prints ab[1, 2, 3] /// /// /// Arguments that will be printed. public static void Print(params object[] what) { Print(AppendPrintParams(what)); } /// /// Prints a message to the console. /// The following BBCode tags are supported: b, i, u, s, indent, code, url, center, /// right, color, bgcolor, fgcolor. /// Color tags only support named colors such as red, not hexadecimal color codes. /// Unsupported tags will be left as-is in standard output. /// When printing to standard output, the supported subset of BBCode is converted to /// ANSI escape codes for the terminal emulator to display. Displaying ANSI escape codes /// is currently only supported on Linux and macOS. Support for ANSI escape codes may vary /// across terminal emulators, especially for italic and strikethrough. /// /// Note: Consider using and /// to print error and warning messages instead of or /// . /// This distinguishes them from print messages used for debugging purposes, /// while also displaying a stack trace when an error or warning is printed. /// /// Message that will be printed. public static void PrintRich(string what) { Debug.WriteLine("Info: " + what); Godot.GD.PrintRich(what); } /// /// Converts one or more arguments of any type to string in the best way possible /// and prints them to the console. /// The following BBCode tags are supported: b, i, u, s, indent, code, url, center, /// right, color, bgcolor, fgcolor. /// Color tags only support named colors such as red, not hexadecimal color codes. /// Unsupported tags will be left as-is in standard output. /// When printing to standard output, the supported subset of BBCode is converted to /// ANSI escape codes for the terminal emulator to display. Displaying ANSI escape codes /// is currently only supported on Linux and macOS. Support for ANSI escape codes may vary /// across terminal emulators, especially for italic and strikethrough. /// /// Note: Consider using and /// to print error and warning messages instead of or /// . /// This distinguishes them from print messages used for debugging purposes, /// while also displaying a stack trace when an error or warning is printed. /// /// /// /// GD.PrintRich("[code][b]Hello world![/b][/code]"); // Prints out: [b]Hello world![/b] /// /// /// Arguments that will be printed. public static void PrintRich(params object[] what) { PrintRich(AppendPrintParams(what)); } /// /// Prints a message to standard error line. /// /// Message that will be printed. public static void PrintErr(string what) { Debug.WriteLine("Error: " + what); Godot.GD.PrintErr(what); } /// /// Prints one or more arguments to strings in the best way possible to standard error line. /// /// /// /// GD.PrintErr("prints to stderr"); /// /// /// Arguments that will be printed. public static void PrintErr(params object[] what) { PrintErr(AppendPrintParams(what)); } /// /// Prints a message to the OS terminal. /// Unlike , no newline is added at the end. /// /// Message that will be printed. public static void PrintRaw(string what) { Debug.WriteLine(what); Godot.GD.PrintRaw(what); } /// /// Prints one or more arguments to strings in the best way possible to the OS terminal. /// Unlike , no newline is added at the end. /// /// /// /// GD.PrintRaw("A"); /// GD.PrintRaw("B"); /// GD.PrintRaw("C"); /// // Prints ABC to terminal /// /// /// Arguments that will be printed. public static void PrintRaw(params object[] what) { PrintRaw(AppendPrintParams(what)); } /// /// Prints one or more arguments to the console with a space between each argument. /// /// /// /// GD.PrintS("A", "B", "C"); // Prints A B C /// /// /// Arguments that will be printed. public static void PrintS(params object[] what) { string message = AppendPrintParams(' ', what); PrintErr(message); Godot.GD.PrintS(what); } /// /// Prints one or more arguments to the console with a tab between each argument. /// /// /// /// GD.PrintT("A", "B", "C"); // Prints A B C /// /// /// Arguments that will be printed. public static void PrintT(params object[] what) { string message = AppendPrintParams('\t', what); PrintErr(message); Godot.GD.PrintT(what); } /// /// Pushes an error message to Godot's built-in debugger and to the OS terminal. /// /// Note: Errors printed this way will not pause project execution. /// /// /// /// GD.PushError("test error"); // Prints "test error" to debugger and terminal as error call /// /// /// Error message. public static void PushError(string message) { Debug.WriteLine("Error: " + message); Godot.GD.PushError(message); } /// /// Pushes an error message to Godot's built-in debugger and to the OS terminal. /// /// Note: Errors printed this way will not pause project execution. /// /// /// /// GD.PushError("test_error"); // Prints "test error" to debugger and terminal as error call /// /// /// Arguments that form the error message. public static void PushError(params object[] what) { PushError(AppendPrintParams(what)); } /// /// Pushes a warning message to Godot's built-in debugger and to the OS terminal. /// /// /// /// GD.PushWarning("test warning"); // Prints "test warning" to debugger and terminal as warning call /// /// /// Warning message. public static void PushWarning(string message) { Debug.WriteLine("Warning: " + message); Godot.GD.PushWarning(message); } /// /// Pushes a warning message to Godot's built-in debugger and to the OS terminal. /// /// /// /// GD.PushWarning("test warning"); // Prints "test warning" to debugger and terminal as warning call /// /// /// Arguments that form the warning message. public static void PushWarning(params object[] what) { PushWarning(AppendPrintParams(what)); } /// /// Returns a random floating point value between 0.0 and 1.0 (inclusive). /// /// /// /// GD.Randf(); // Returns e.g. 0.375671 /// /// /// A random number. public static float Randf() => Godot.GD.Randf(); /// /// Returns a normally-distributed pseudo-random floating point value /// using Box-Muller transform with the specified /// and a standard . /// This is also called Gaussian distribution. /// /// A random normally-distributed number. public static double Randfn(double mean, double deviation) => Godot.GD.Randfn(mean, deviation); /// /// Returns a random unsigned 32-bit integer. /// Use remainder to obtain a random value in the interval [0, N - 1] /// (where N is smaller than 2^32). /// /// /// /// GD.Randi(); // Returns random integer between 0 and 2^32 - 1 /// GD.Randi() % 20; // Returns random integer between 0 and 19 /// GD.Randi() % 100; // Returns random integer between 0 and 99 /// GD.Randi() % 100 + 1; // Returns random integer between 1 and 100 /// /// /// A random number. public static uint Randi() => Godot.GD.Randi(); /// /// Randomizes the seed (or the internal state) of the random number generator. /// The current implementation uses a number based on the device's time. /// /// Note: This method is called automatically when the project is run. /// If you need to fix the seed to have consistent, reproducible results, /// use to initialize the random number generator. /// public static void Randomize() => Godot.GD.Randomize(); /// /// Returns a random floating point value between /// and (inclusive). /// /// /// /// GD.RandRange(0.0, 20.5); // Returns e.g. 7.45315 /// GD.RandRange(-10.0, 10.0); // Returns e.g. -3.844535 /// /// /// A random number inside the given range. public static double RandRange(double from, double to) => Godot.GD.RandRange(from, to); /// /// Returns a random signed 32-bit integer between /// and (inclusive). If is lesser than /// , they are swapped. /// /// /// /// GD.RandRange(0, 1); // Returns either 0 or 1 /// GD.RandRange(-10, 1000); // Returns random integer between -10 and 1000 /// /// /// A random number inside the given range. public static int RandRange(int from, int to) => Godot.GD.RandRange(from, to); /// /// Given a , returns a randomized /// value. The may be modified. /// Passing the same consistently returns the same value. /// /// Note: "Seed" here refers to the internal state of the pseudo random number /// generator, currently implemented as a 64 bit integer. /// /// /// /// var a = GD.RandFromSeed(4); /// /// /// /// Seed to use to generate the random number. /// If a different seed is used, its value will be modified. /// /// A random number. public static uint RandFromSeed(ref ulong seed) => Godot.GD.RandFromSeed(ref seed); /// /// Returns a that iterates from /// 0 (inclusive) to (exclusive) /// in steps of 1. /// /// The last index. public static IEnumerable Range(int end) => Godot.GD.Range(end); /// /// Returns a that iterates from /// (inclusive) to (exclusive) /// in steps of 1. /// /// The first index. /// The last index. public static IEnumerable Range(int start, int end) => Godot.GD.Range(start, end); /// /// Returns a that iterates from /// (inclusive) to (exclusive) /// in steps of . /// The argument can be negative, but not 0. /// /// /// is 0. /// /// The first index. /// The last index. /// The amount by which to increment the index on each iteration. public static IEnumerable Range(int start, int end, int step) => Godot.GD.Range(start, end, step); /// /// Sets seed for the random number generator to . /// Setting the seed manually can ensure consistent, repeatable results for /// most random functions. /// /// /// /// ulong mySeed = (ulong)GD.Hash("Godot Rocks"); /// GD.Seed(mySeed); /// var a = GD.Randf() + GD.Randi(); /// GD.Seed(mySeed); /// var b = GD.Randf() + GD.Randi(); /// // a and b are now identical /// /// /// Seed that will be used. public static void Seed(ulong seed) => Godot.GD.Seed(seed); /// /// Converts a formatted string that was returned by /// to the original value. /// /// /// /// string a = "{ \"a\": 1, \"b\": 2 }"; // a is a string /// var b = GD.StrToVar(a).AsGodotDictionary(); // b is a Dictionary /// GD.Print(b["a"]); // Prints 1 /// /// /// String that will be converted to Variant. /// The decoded Variant. public static Variant StrToVar(string str) => Godot.GD.StrToVar(str); /// /// Encodes a value to a byte array, without encoding objects. /// Deserialization can be done with . /// Note: If you need object serialization, see . /// /// that will be encoded. /// The encoded as an array of bytes. public static byte[] VarToBytes(Variant var) => Godot.GD.VarToBytes(var); /// /// Encodes a . Encoding objects is allowed (and can potentially /// include executable code). Deserialization can be done with . /// /// that will be encoded. /// The encoded as an array of bytes. public static byte[] VarToBytesWithObjects(Variant var) => Godot.GD.VarToBytesWithObjects(var); /// /// Converts a to a formatted string that /// can later be parsed using . /// /// /// /// var a = new Godot.Collections.Dictionary { ["a"] = 1, ["b"] = 2 }; /// GD.Print(GD.VarToStr(a)); /// // Prints: /// // { /// // "a": 1, /// // "b": 2 /// // } /// /// /// Variant that will be converted to string. /// The encoded as a string. public static string VarToStr(Variant var) => Godot.GD.VarToStr(var); /// /// Get the that corresponds for the given . /// /// The for the given . public static Variant.Type TypeToVariantType(Type type) => Godot.GD.TypeToVariantType(type); }