Built-in GDScript functions.
This contains the list of built-in gdscript functions. Mostly math functions and other utilities. Everything else is expanded by objects.
Returns a 32 bit color with red, green, blue and alpha channels. Each channel has 8bits of information ranging from 0 to 255.
'r8' red channel
'g8' green channel
'b8' blue channel
'a8' alpha channel
[codeblock]
red = Color8(255, 0, 0)
[/codeblock]
Returns color 'name' with alpha ranging from 0 to 1. Note: 'name' is defined in color_names.inc.
[codeblock]
red = ColorN('red')
[/codeblock]
Returns the absolute value of parameter 's' (i.e. unsigned value, works for integer and float).
[codeblock]
# a is 1
a = abs(-1)
[/codeblock]
Returns the arc cosine of 's' in radians. Use to get the angle of cosine 's'.
[codeblock]
# c is 0.523599 or 30 degrees if converted with rad2deg(s)
c = acos(0.866025)
[/codeblock]
Returns the arc sine of 's' in radians. Use to get the angle of sine 's'.
[codeblock]
# s is 0.523599 or 30 degrees if converted with rad2deg(s)
s = asin(0.5)
[/codeblock]
Returns the arc tangent of y/x in radians. Use to get the angle of tangent y/x. To compute the value, the function takes into account the sign of both arguments in order to determine the quadrant.
[codeblock]
# a is 3.141593
a = atan(0,-1)
[/codeblock]
Decode a byte array back to a value.
Rounds 's' upward, returning the smallest integral value that is not less than 's'.
[codeblock]
# i is 2
i = ceil(1.45)
# i is 2
i = ceil(1.001)
[/codeblock]
Returns a character as String of the given ASCII code.
[codeblock]
# a is 'A'
a = char(65)
# a is 'a'
a = char(65+32)
[/codeblock]
Clamp 'val' and return a value not less than 'min' and not more than 'max'.
[codeblock]
speed = 1000
# a is 20
a = clamp(speed, 1, 20)
speed = -10
# a is 1
a = clamp(speed, 1, 20)
[/codeblock]
Convert from a type to another in the best way possible. The "type" parameter uses the enum TYPE_* in [@Global Scope].
[codeblock]
a = Vector2(1, 0)
# prints 1
print(a.length())
a = convert(a, TYPE_STRING)
# prints 6
# (1, 0) is 6 characters
print(a.length())
[/codeblock]
Returns the cosine of angle 's' in radians.
[codeblock]
# prints 1 and -1
print(cos(PI*2))
print(cos(PI))
[/codeblock]
Returns the hyperbolic cosine of 's' in radians.
[codeblock]
# prints 1.543081
print(cosh(1))
[/codeblock]
Convert from decibels to linear energy (audio).
Returns the number of digit places after the decimal that the first non-zero digit occurs.
[codeblock]
# n is 2
n = decimals(0.035)
[/codeblock]
Returns the result of 'value' decreased by 'step' * 'amount'.
[codeblock]
# a = 59
a = dectime(60, 10, 0.1))
[/codeblock]
Returns degrees converted to radians.
[codeblock]
# r is 3.141593
r = deg2rad(180)
[/codeblock]
Convert a previously converted instance to dictionary back into an instance. Useful for deserializing.
Easing function, based on exponent. 0 is constant, 1 is linear, 0 to 1 is ease-in, 1+ is ease out. Negative values are in-out/out in.
Returns [b]e[/b] raised to the power of 's'. [b]e[/b] sometimes called "Euler's number" is a mathematical constant whose value is approximately 2.71828.
Returns the largest integer value (rounded down) that is less than or equal to 's'.
Returns the floating-point remainder of x/y (rounded towards zero):
[codeblock]
fmod = x - tquot * y
[/codeblock]
Where tquot is the truncated (i.e., rounded towards zero) result of: x/y.
Returns the integer hash of the variable passed.
[codeblock]
# print 177670
print(hash("a"))
[/codeblock]
Returns the passed instance converted a dictionary (useful for serializing).
[codeblock]
var foo = "bar"
func _ready():
var d = inst2dict(self)
print(d.keys())
print(d.values())
[/codeblock]
Prints out:
[codeblock]
[@subpath, @path, foo]
[, res://test.gd, bar]
[/codeblock]
Returns the Object that corresponds to 'instance_id'. All Objects have a unique instance ID.
[codeblock]
var foo = "bar"
func _ready():
var id = get_instance_id()
var inst = instance_from_id(id)
print(inst.foo)
[/codeblock]
Prints "bar"
Returns a normalized value considering the given range.
[codeblock]
inverse_lerp(3, 5, 4) # return 0.5
[/codeblock]
Returns True/False whether 's' is an infinity value (either positive infinity or negative infinity).
Returns True/False whether 's' is a NaN (Not-A-Number) value.
Returns length of Variant 'var'. Length is the character count of String, element count of Array, size of Dictionary, etc. Note: Generates a fatal error if Variant can not provide a length.
[codeblock]
a = [1, 2, 3, 4]
print(len(a))
[/codeblock]
Prints 4
Linear interpolates between two values by a normalized value.
Convert from linear energy to decibels (audio).
Load a resource from the filesystem located at 'path'. Note: resource paths can be obtained by right clicking on a resource in the Assets Pannel and choosing "Copy Path".
[codeblock]
# load a scene called main located in the root of the project directory
var main = load("res://main.tscn")
[/codeblock]
Natural logarithm. The amount of time needed to reach a certain level of continuous growth. Note: This is not the same as the log funcation on your calculator which is a base 10 logarithm.
[codeblock]
# a is 2.302585
a = log(10)
[/codeblock]
Returns the maximum of two values.
[codeblock]
# a is 2
a = max(1,2)
# a is -3.99
a = max(-3.99, -4)
[/codeblock]
Returns the minimum of two values.
[codeblock]
# a is 1
a = min(1,2)
# a is -4
a = min(-3.99, -4)
[/codeblock]
Returns the nearest larger power of 2 for an integer.
[codeblock]
# a is 4
a = nearest_po2(3)
# a is 4
a = nearest_po2(4)
# a is 8
a = nearest_po2(5)
[/codeblock]
Parse JSON text to a Variant (use [method typeof] to check if it is what you expect).
Be aware that the JSON specification does not define integer or float types, but only a number type. Therefore, parsing a JSON text will convert every numerical values to [float] types.
[codeblock]
p = parse_json('["a", "b", "c"]')
if typeof(p) == TYPE_ARRAY:
print(p[0])
else:
print("unexpected results")
[/codeblock]
Returns the result of 'x' raised to the power of 'y'.
[codeblock]
# a is 32
a = pow(2,5)
[/codeblock]
Returns a resource from the filesystem that is loaded during script parsing. Note: resource paths can be obtained by right clicking on a resource in the Assets Pannel and choosing "Copy Path".
[codeblock]
# load a scene called main located in the root of the project directory
var main = preload("res://main.tscn")
Converts one or more arguments to strings in the best way possible and prints them to the console.
[codeblock]
a = [1,2,3]
print("a","b",a)
[/codeblock]
Prints ab[1, 2, 3]
Print a stack track at code location, only works when running with debugger turned on.
Print one or more arguments to strings in the best way possible to standard error line.
Print one or more arguments to strings in the best way possible to console. No newline is added at the end.
Print one or more arguments to the console with a space between each argument.
Print one or more arguments to the console with a tab between each argument.
Convert from radians to degrees.
Random range, any floating point value between 'from' and 'to'.
Random from seed: pass a seed, and an array with both number and new seed is returned. "Seed" here refers to the internal state of the pseudo random number generator. The internal state of the current implementation is 64 bits.
Return a random floating point value between 0 and 1.
Return a random 32 bits integer value. To obtain a random value between 0 to N (where N is smaller than 2^32 - 1), you can use remainder. For example, to get a random integer between 0 and 19 inclusive, you can use randi() % 20.
Randomize the seed (or the internal state) of the random number generator. Current implementation reseeds using a number based on time.
Return an array with the given range. Range can be 1 argument N (0 to N-1), two arguments (initial, final-1) or three arguments (initial, final-1, increment).
Maps a value from range [istart, istop] to [ostart, ostop].
[codeblock]
range_lerp(75, 0, 100, -1, 1) # returns 0.5
[/codeblock]
Returns the integral value that is nearest to s, with halfway cases rounded away from zero.
Set seed for the random number generator.
Return sign (-1 or +1).
Returns the sine of an angle of s radians.
Returns the hyperbolic sine of s.
Returns the square root of s.
Snap float value to a given step.
Convert one or more arguments to string in the best way possible.
Convert a formatted string that was returned by [method var2str] to the original value.
Returns the tangent of an angle of s radians.
Returns the hyperbolic tangent of s.
Convert a Variant to json text.
Returns whether the given class is exist in [ClassDB].
[codeblock]
type_exists("Sprite") # returns true
type_exists("Variant") # returns false
[/codeblock]
Return the internal type of the given Variant object, using the TYPE_* enum in [@Global Scope].
This method is used to validate the structure and data types of a piece of JSON, similar to XML Schema for XML.
Encode a variable value to a byte array.
Convert a value to a formatted string that can later be parsed using [method str2var].
Return a weak reference to an object.
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.
Stop the function execution and return the current state. Call [method GDFunctionState.resume] on the state to resume execution. This invalidates the state.
Returns anything that was passed to the resume function call. If passed an object and a signal, the execution is resumed when the object's signal is emitted.
Constant that represents how many times the diameter of a circumference fits around its perimeter.
A positive infinity. (For negative infinity, use -INF).
Macro constant that expands to an expression of type float that represents a NaN.
The NaN values are used to identify undefined or non-representable values for floating-point elements, such as the square root of negative numbers or the result of 0/0.