GD
utility.swift:11class GD
Citizens in SwiftGodot
Type members
static func abs(x: Variant
) -> Variant Returns the absolute value of a
Variant
parameterx
(i.e. non-negative value). Supported types: integer, float,Vector2
,Vector2i
,Vector3
,Vector3i
,Vector4
,Vector4i
.static func angleDifference(from: Double, to: Double
) -> Double Returns the difference between the two angles, in the range of
[-PI, +PI]
. Whenfrom
andto
are opposite, returns-PI
iffrom
is smaller thanto
, orPI
otherwise.static func bezierDerivative(start: Double, control1: Double, control2: Double, end: Double, t: Double
) -> Double Returns the derivative at the given
t
on a one-dimensional Bézier curve defined by the givencontrol1
,control2
, andend
points.static func bezierInterpolate(start: Double, control1: Double, control2: Double, end: Double, t: Double
) -> Double Returns the point at the given
t
on a one-dimensional Bézier curve defined by the givencontrol1
,control2
, andend
points.static func bytesToVar(bytes: PackedByteArray
) -> Variant Decodes a byte array back to a
Variant
value, without decoding objects.static func bytesToVarWithObjects(bytes: PackedByteArray
) -> Variant Decodes a byte array back to a
Variant
value. Decoding objects is allowed.static func clamp(value: Variant, min: Variant, max: Variant
) -> Variant Clamps the
value
, returning aVariant
not less thanmin
and not more thanmax
. Any values that can be compared with the less than and greater than operators will work.static func clampf(value: Double, min: Double, max: Double
) -> Double Clamps the
value
, returning a float not less thanmin
and not more thanmax
.static func clampi(value: Int64, min: Int64, max: Int64
) -> Int64 Clamps the
value
, returning an integer not less thanmin
and not more thanmax
.static func cubicInterpolate(from: Double, to: Double, pre: Double, post: Double, weight: Double
) -> Double Cubic interpolates between two values by the factor defined in
weight
withpre
andpost
values.static func cubicInterpolateAngle(from: Double, to: Double, pre: Double, post: Double, weight: Double
) -> Double Cubic interpolates between two rotation values with shortest path by the factor defined in
weight
withpre
andpost
values. See alsolerpAngle()
.static func cubicInterpolateAngleInTime(from: Double, to: Double, pre: Double, post: Double, weight: Double, toT: Double, preT: Double, postT: Double
) -> Double Cubic interpolates between two rotation values with shortest path by the factor defined in
weight
withpre
andpost
values. See alsolerpAngle()
.static func cubicInterpolateInTime(from: Double, to: Double, pre: Double, post: Double, weight: Double, toT: Double, preT: Double, postT: Double
) -> Double Cubic interpolates between two values by the factor defined in
weight
withpre
andpost
values.static func dbToLinear(db: Double
) -> Double Converts from decibels to linear energy (audio).
static func ease(x: Double, curve: Double
) -> Double Returns an “eased” value of
x
based on an easing function defined withcurve
. This easing function is based on an exponent. Thecurve
can be any floating-point number, with specific values leading to the following behaviors:static func errorString(error: Int64
) -> String Returns a human-readable name for the given
GodotError
code.static func hash(variable: Variant
) -> Int64 Returns the integer hash of the passed
variable
.static func instanceFromId(instanceId: Int64
) -> Object? Returns the
Object
that corresponds toinstanceId
. All Objects have a unique instance ID. See alsogetInstanceId
.static func isEqualApprox(a: Double, b: Double
) -> Bool Returns
true
ifa
andb
are approximately equal to each other.static func isFinite(x: Double
) -> Bool Returns whether
x
is a finite value, i.e. it is not@GDScript.NAN
, positive infinity, or negative infinity.static func isInf(x: Double
) -> Bool Returns
true
ifx
is either positive infinity or negative infinity.static func isInstanceIdValid(id: Int64
) -> Bool Returns
true
if the Object that corresponds toid
is a valid object (e.g. has not been deleted from memory). All Objects have a unique instance ID.static func isInstanceValid(instance: Variant
) -> Bool Returns
true
ifinstance
is a valid Object (e.g. has not been deleted from memory).static func isNan(x: Double
) -> Bool Returns
true
ifx
is a NaN (“Not a Number” or invalid) value.static func isSame(a: Variant, b: Variant
) -> Bool Returns
true
, for value types, ifa
andb
share the same value. Returnstrue
, for reference types, if the references ofa
andb
are the same.static func isZeroApprox(x: Double
) -> Bool Returns
true
ifx
is zero or almost zero. The comparison is done using a tolerance calculation with a small internal epsilon.static func lerpAngle(from: Double, to: Double, weight: Double
) -> Double Linearly interpolates between two angles (in radians) by a
weight
value between 0.0 and 1.0.static func linearToDb(lin: Double
) -> Double Converts from linear energy to decibels (audio). This can be used to implement volume sliders that behave as expected (since volume isn’t linear).
static func load(path: String
) -> Resource? Loads a resource from the filesystem located at
path
.static func load<T>(path: String
) -> T? Loads a resource from the filesystem located at
path
.static func max(arg1: Variant, arg2: Variant, Variant...
) -> Variant Returns the maximum of the given numeric values. This function can take any number of arguments.
static func maxf(a: Double, b: Double
) -> Double Returns the maximum of two float values.
static func maxi(a: Int64, b: Int64
) -> Int64 Returns the maximum of two integer values.
static func min(arg1: Variant, arg2: Variant, Variant...
) -> Variant Returns the minimum of the given numeric values. This function can take any number of arguments.
static func minf(a: Double, b: Double
) -> Double Returns the minimum of two float values.
static func mini(a: Int64, b: Int64
) -> Int64 Returns the minimum of two integer values.
static func moveToward(from: Double, to: Double, delta: Double
) -> Double Moves
from
towardto
by thedelta
amount. Will not go pastto
.static func nearestPo2(value: Int64
) -> Int64 Returns the smallest integer power of 2 that is greater than or equal to
value
.static func pingpong(value: Double, length: Double
) -> Double Wraps
value
between0
and thelength
. If the limit is reached, the next value the function returns is decreased to the0
side or increased to thelength
side (like a triangle wave). Iflength
is less than zero, it becomes positive.static func print(Any..., separator: String
) Converts one or more arguments of any type to string in the best way possible and prints them to the console.
static func print(arg1: Variant, Variant...
) Converts one or more arguments of any type to string in the best way possible and prints them to the console.
static func printDebug(Any..., separator: String, fileID: StaticString, function: StaticString, line: UInt
) Converts one or more arguments of any type to string in the best way possible and prints them to the console, with fileID, line, and function name of the calling function.
static func printErr(Any..., separator: String
) Converts one or more arguments of any type to string in the best way possible and prints them to the console.
static func printRich(Any..., separator: String
) Converts one or more arguments of any type to string in the best way possible and prints them to the console.
static func printRich(arg1: Variant, Variant...
) Converts one or more arguments of any type to string in the best way possible and prints them to the console.
static func printVerbose(arg1: Variant, Variant...
) If verbose mode is enabled (
isStdoutVerbose
returningtrue
), converts one or more arguments of any type to string in the best way possible and prints them to the console.static func printerr(arg1: Variant, Variant...
) Prints one or more arguments to strings in the best way possible to standard error line.
static func printraw(arg1: Variant, Variant...
) Prints one or more arguments to strings in the best way possible to the OS terminal. Unlike
print()
, no newline is automatically added at the end.static func prints(arg1: Variant, Variant...
) Prints one or more arguments to the console with a space between each argument.
static func printt(arg1: Variant, Variant...
) Prints one or more arguments to the console with a tab between each argument.
static func pushError(Any..., separator: String
) Pushes an error message to Godot’s built-in debugger and to the OS terminal.
static func pushError(arg1: Variant, Variant...
) Pushes an error message to Godot’s built-in debugger and to the OS terminal.
static func pushWarning(Any..., separator: String
) Pushes a warning message to Godot’s built-in debugger and to the OS terminal.
static func pushWarning(arg1: Variant, Variant...
) Pushes a warning message to Godot’s built-in debugger and to the OS terminal.
static func randFromSeed(Int64
) -> PackedInt64Array Given a
seed
, returns aPackedInt64Array
of size2
, where its first element is the randomized integer value, and the second element is the same asseed
. Passing the sameseed
consistently returns the same array.static func randf(
) -> Double Returns a random floating-point value between
0.0
and1.0
(inclusive).static func randfRange(from: Double, to: Double
) -> Double Returns a random floating-point value between
from
andto
(inclusive).static func randfn(mean: Double, deviation: Double
) -> Double Returns a normally-distributed, pseudo-random floating-point value from the specified
mean
and a standarddeviation
. This is also known as a Gaussian distribution.static func randi(
) -> Int64 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).static func randiRange(from: Int64, to: Int64
) -> Int64 Returns a random signed 32-bit integer between
from
andto
(inclusive). Ifto
is lesser thanfrom
, they are swapped.static func randomize(
) Randomizes the seed (or the internal state) of the random number generator. The current implementation uses a number based on the device’s time.
static func remap(value: Double, istart: Double, istop: Double, ostart: Double, ostop: Double
) -> Double Maps a
value
from range[istart, istop]
to[ostart, ostop]
. See alsolerp()
andinverseLerp()
. Ifvalue
is outside[istart, istop]
, then the resulting value will also be outside[ostart, ostop]
. If this is not desired, useclamp()
on the result of this function.static func ridAllocateId(
) -> Int64 Allocates a unique ID which can be used by the implementation to construct an RID. This is used mainly from native extensions to implement servers.
static func ridFromInt64(base: Int64
) -> RID Creates an RID from a
base
. This is used mainly from native extensions to build servers.static func rotateToward(from: Double, to: Double, delta: Double
) -> Double Rotates
from
towardto
by thedelta
amount. Will not go pastto
.static func seed(base: Int64
) Sets the seed for the random number generator to
base
. Setting the seed manually can ensure consistent, repeatable results for most random functions.static func sign(x: Variant
) -> Variant Returns the same type of
Variant
asx
, with-1
for negative values,1
for positive values, and0
for zeros. Fornan
values it returns 0.static func signf(x: Double
) -> Double Returns
-1.0
ifx
is negative,1.0
ifx
is positive, and0.0
ifx
is zero. Fornan
values ofx
it returns 0.0.static func signi(x: Int64
) -> Int64 Returns
-1
ifx
is negative,1
ifx
is positive, and0
if ifx
is zero.static func sinh(x: Double
) -> Double Returns the hyperbolic sine of
x
.static func smoothstep(from: Double, to: Double, x: Double
) -> Double Returns the result of smoothly interpolating the value of
x
between0
and1
, based on the wherex
lies with respect to the edgesfrom
andto
.static func snappedi(x: Double, step: Int64
) -> Int64 Returns the multiple of
step
that is the closest tox
.static func stepDecimals(x: Double
) -> Int64 Returns the position of the first non-zero digit, after the decimal point. Note that the maximum return value is 10, which is a design decision in the implementation.
static func str(arg1: Variant, Variant...
) -> String Converts one or more arguments of any
Variant
type to aString
in the best way possible.static func strToVar(string: String
) -> Variant Converts a formatted
string
that was returned byvarToStr()
to the originalVariant
.static func typeConvert(variant: Variant, type: Int64
) -> Variant Converts the given
variant
to the giventype
, using theVariant.GType
values. This method is generous with how it handles types, it can automatically convert between array types, convert numericString
s to integer, and converting most things toString
.static func typeString(type: Int64
) -> String Returns a human-readable name of the given
type
, using theVariant.GType
values.static func typeof(variable: Variant
) -> Int64 Returns the internal type of the given
variable
, using theVariant.GType
values.static func varToBytes(variable: Variant
) -> PackedByteArray Encodes a
Variant
value to a byte array, without encoding objects. Deserialization can be done withbytesToVar()
.static func varToBytesWithObjects(variable: Variant
) -> PackedByteArray Encodes a
Variant
value to a byte array. Encoding objects is allowed (and can potentially include executable code). Deserialization can be done withbytesToVarWithObjects()
.static func varToStr(variable: Variant
) -> String Converts a
Variant
variable
to a formattedString
that can then be parsed usingstrToVar()
.static func weakref(obj: Variant
) -> Variant Returns a
WeakRef
instance holding a weak reference toobj
. Returns an emptyWeakRef
instance ifobj
isnull
. Prints an error and returnsnull
ifobj
is neitherObject
-derived nornull
.static func wrap(value: Variant, min: Variant, max: Variant
) -> Variant Wraps the
Variant
value
betweenmin
andmax
. Can be used for creating loop-alike behavior or infinite surfaces.static func wrapf(value: Double, min: Double, max: Double
) -> Double Wraps the float
value
betweenmin
andmax
. Can be used for creating loop-alike behavior or infinite surfaces.static func wrapi(value: Int64, min: Int64, max: Int64
) -> Int64 Wraps the integer
value
betweenmin
andmax
. Can be used for creating loop-alike behavior or infinite surfaces.