Skip to main content

  | मौसम…

411 समाचार
Public archive edition · Attributed source reporting with original dates · AI drafts are excluded

411 REFERENCE · Wikibooks

Guide to the Godot game engine/Programming/GDScript/Data types

Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.

Programming/GDScript/If conditions Programming/GDScript/Manipulate the SceneTree If conditions Manipulating the SceneTree null Null is the absence of value. Null can be inferred as a function's return value with -> void. Never evaluates as true in if statements: if null: pass # This never runs This is the default value used in a variable if it has not been set. If a function that you made is supposed to returns something, but instead return null, be sure to check the output for errors, and double check the function is working properly. bool A bool is a basic data type which may only have 2 values: true (positive, on or 1) and false (negative, off or 0). bool may be inferred as a function's return value with -> bool. bools evaluate as true in an if statements if it is equal to true: if true: pass # This runs If converted to an integer (with int(bool)), it becomes 0 if it's false and 1 if true. History Bools were invented by George Boole. George was an English mathematician whose work is the result of a special programming system. This system may only have 2 values, true and false. It was called "Boolean logic" in his honor. See the official documentation for more: https://docs.godotengine.org/en/stable/classes/class_bool.html int An int, or integer, is a whole number. int may be inferred as a function's return value with -> int. ints evaluate as true in an if statements if not equal to 0: if 1: pass # This runs Operators Ints are great for maths. They support the following operators: Comparitive: < Less than > More than == Equal to Modifiers (use a number on both sides): * Multiplication / Division + Addition - Subtraction % A type of divide. It sets a 64-bit integer to a limit of the second (randi()%5 is an integer 0 to 5) ^ To the power of Dividing ints or floats by 0 is impossible! Attempting to do so will cause an error!See maths is fun: dividing by zero. Useful constants: INF # Or "infinity". When printed, shows "1.#INF" NAN # Or "not a number". When printed, shows "1.#QNAN" Speaking of NaN, NaN is not equal to NaN (NAN == NAN is never true). Instead, use is_nan(variable) to see if it is NaN. To check for infinity (INF is equal to INF, but INF is not equal to -INF), use is_inf(variable). Ints can be separated with underscores, like 36_234_785 (which makes 36234785) to be easier to read. See also Float, decimal numbers See the official documentation for more: https://docs.godotengine.org/en/stable/classes/class_int.html float Floats, also known as floating-point numbers, are decimal numbers, like 1.7. float may be inferred as a function's return value with -> float. floats evaluate as true in an if statements if not equal to 0.0: if .1: pass # This runs Operators Floats are great for maths. They support the following operators: Comparative: < Less than > More than == Equal to Modifiers (use a number on both sides): * Multiplication / Division + Addition - Subtraction Useful constants: PI # 3.141593 To check if two numbers are approximately equal (I.e: less than 0.00001 difference), use is_equal_approx(num1, num2). Floats can be separated with underscores to be easier to read, like ints. See also Integers, whole numbers See the official documentation for more: https://docs.godotengine.org/en/stable/classes/class_float.html String A versatile data type that can be any length. They are a string of characters. Hence the name "String". String may be inferred as a function's return value with -> String. Strings evaluate as true in an if statement if not empty: if "false": pass # This runs See the official documentation for more: https://docs.godotengine.org/en/stable/classes/class_string.html Array An array is even more versatile than a String. It may be any length, and each value has a numerical index (of int). An Array may be inferred as a function's return value with -> Array. In an if statement Arrays evaluate as false when empty: if [false]: pass # This runs if []: pass # This does not run Arrays are passed by reference rather than value. See Dictionary. See the official documentation for more: https://docs.godotengine.org/en/stable/classes/class_array.html] Dictionary Dictionarys are the most versatile data type in Godot, besides Objects themselves. Dictionary may be inferred as a function's return value with -> Dictionary. Dictionarys evaluate as true in an if statements if not empty: if {"team 1": 0, "team 2": 0}: pass # This runs Every value is indexed with a value. You can use any data type or Object you wish. As the value or key. FuncRefs and Nodes also count, but remember to keep that same FuncRef or Node (instead of using a different FuncRef for the same function). Even another Dictionary or Array can be used as a key or value. Dictionaries are passed as reference rather than value, meaning this is never true: return {"red": Color.red} == {"red": Color.red} # False Although this is: var dictionary={"red": Color.red} return dictionary == dictionary # True This can be useful by saving the same Dictionary in multiple variables, then changing one of them changes them all. If you don't want this functionality, use dictionary.duplicate(true). To compare them, use hash(): return hash({"red": Color.red}) == hash({"red": Color.red}) # True Indexing To get a value from a dictionary, you need to retrieve it with its index. var points = {"Hannah": 5, "Paul": 8, "Tim": 3, "Bob": 0} var username = "Bob" func update_hud(): $Label.text = "Score: " + String(points[username]) So "Bob" has no points. So the label says "Score: 0". See the official documentation for more: https://docs.godotengine.org/en/stable/classes/class_dictionary.html Vector2 A vector is a value with multiple floats. Vector2 is used for 2D positioning among other things. They are indexed at the end with: ".x" or "[0]" ".y" or "[1]" They are complex things, including many functions. Vector2 may be inferred as a function's return value with ->Vector2. Vector2s evaluate as true in an if statements if at least one value is not equal to 0: if Vector2(1, 0): pass # This runs See the official documentation for more: https://docs.godotengine.org/en/stable/classes/class_vector2.html Vector3 A vector is a value with multiple floats. A Vector3 has 3 numbers and used for 3D positional and rotational data, among other things. They are indexed at the end with: ".x" or "[0]" ".y" or "[1]" ".z" or "[2]" Vector3s are complex things, having many functions. Vector3 may be inferred as a function's return value with ->Vector3. Vector3s evaluate as true in an if statements if at least one value is not equal to 0: if Vector3(1, 0, 0): pass # This runs See the official documentation for more: https://docs.godotengine.org/en/stable/classes/class_vector3.html Color Technically considered a vector, it contains 4 values: ".r" - the red channel (E.g: 0 is no red, 1 is full red) ".g"- the green channel ".b" - the blue channel ".a" - alpha (or transparency) Color may be inferred as a function's return value with ->Color. Colors evaluate as true in an "if" statements if not black with no transparency (Color(0, 0, 0, 1) is false): if Color(1, 1, 1): pass # This runs If r, g and b are 1, the color is white. If they are 0, the color is black. Know your color wheel! No color or all the colors! Now, this can be irritating sometimes. How do you quickly get basic colors like orange and yellow? Or common shades of red, green and blue? Color.orange is a simple way to do so. All color constants: black aliceblue #f0f7ffblack antiquewhite #faebd6black aqua #00ffffblack aquamarine #80ffd4black azure #f0ffffblack beige #c0c2b2black bisque #ffe3c4white black blackblanchedalmondbluebluevioletbrownburlywoodcadetbluechartreusechocolatecoralcornflowercornsilkcrimsoncyandarkbluedarkcyandarkgoldenroddarkgraydarkgreendarkkhakidarkmagentadarkolivegreendarkorangedarkorchiddarkreddarksalmondarkseagreendarkslatebluedarkslategraydarkturquoisedarkvioletdeeppinkdeepskybluedodgerbluefirebrickflorawhiteforestgreenfuchsiagainsborowghostwhitegoldgoldenrodgraygreengreenyellowhoneydewhotpinkindianredindigoivorykhakilavenderlavenderblushlawngreenlemonchiffonlightbluelightcorallightcyanlightgoldenrodlightgraylightgreenlightpinklightsalmonlightseagreenlightskybluelightslategraylightsteelbluelightyellowlimelimegreenlinenmagentamaroonmediumaquamarinemediumbluemediumorchidmediumpurplemediumseagreenmediumslatebluemediumspringgreenmediumturquoisemediumvioletredmidnightbluemintcreammistyrosemoccasinnavajowhitenavyblueoldlaceoliveolivedraborangeorangeredorchidpalegoldenrodpalegreenpaleturquoisepalevioletredpapayawhippeachpuffperupinkplumpowderbluepurplerebeccapurpleredrosybrownroyalbluesaddlebrownsalmonsandybrownseagreenseashellsiennasilverskyblueslateblueslategraysnowspringgreensteelbluetantealthistletomatotransparentturquoisevioletwebgraywebgreenwebmaroonwebpurplewheatwhitewhitesmokeyellowyellowgreendimgray See the official documentation for more: https://docs.godotengine.org/en/stable/classes/class_color.html Basis A Basis contains a 3x3 matrix for 3D position, rotation and scale of 3D objects. Basis may be inferred as a function's return value with ->Basis. Basiss evaluate as true in an if statements if at least one of its Vector3s would also evaluate as true: if Basis(Vector3(1, 0, 0), Vector3(0, 0, 0), Vector3(0, 0, 0)): pass # This runs See also Matrices and transforms tutorial. See the official documentation for more: https://docs.godotengine.org/en/stable/classes/class_basis.html Transform2D A Transform2D is a 2x3 matrix used for 2D linear transformations. It can represent transformations such as translation, rotation or scaling. It consists of three Vector2 values: x, y and origin. Transform2D may be inferred as a function's return value with ->Transform2D. Transform2Ds evaluate as true in an if statements if at least one value is not 0: if Transform2D(1, 0, 0, 1, 0, 0): pass # This runs See more: The matrices and transforms tutorial. See the official documentation for more: https://docs.godotengine.org/en/stable/classes/class_transform2d.html Transform A Transform is a 3x4 matrix used for 3D linear transformations. It can represent transformations such as translation, rotation or scaling. It consists of a Basis (first 3 Vector3s) and origin (last value). Transform may be inferred as a function's return value with ->Transform. Transforms evaluate as true in an if statements if at least one value is not 0: if Transform(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0): pass # This runs See more: The matrices and transforms tutorial and the using Transforms tutorial. See the official documentation for more: https://docs.godotengine.org/en/stable/classes/class_transform.html Pool<type>Array For detailed information, please see Array. PoolArrays are optimized versions of Array, but allow storing only one variant type. They also don't contain certain functions, like find(). PoolArrays are passed as value rather than reference: var names = PoolStringArray(["Hannah", "Paul", "Bob"]) var reference_array = ["Hi"] func _ready(): var names_copy = names names_copy.append("Sam") prints(names, names_copy) # Only the second one was changed var reference_array_copy = reference_array reference_array_copy.append("human") prints(reference_array) # Both Arrays were changed here var copy2 = reference_array.duplicate() copy2.append("I am a robot") prints(reference_array, copy2) # They are now both different As you can see, the PoolStringArray does not keep the same values across all references, but the Array does. Allowed types for Pool<type>Arrays are: int, float, string, real (bytes), color, vector2, and vector3. They may only store data types that match the type mentioned in the name, and are more optimized in terms of memory. <noinclude> 100%