@ts-defold/types
Version:
TypeScript definitions for Defold
1,455 lines (1,418 loc) • 456 kB
TypeScript
/** @noSelfInFile */
/// <reference types="@typescript-to-lua/language-extensions" />
/// <reference types="lua-types/5.1" />
/// <reference types="lua-types/special/jit-only" />
// DEFOLD. stable version 1.12.1 (16c6fd602f32de4814660672c38ce3ccbbc1fb59)
/**
* All ids in the engine are represented as hashes, so a string needs to be hashed
before it can be compared with an id.
* @param s string to hash
* @returns a hashed string
* @example To compare a message_id in an on-message callback function:
```lua
function on_message(self, message_id, message, sender)
if message_id == hash("my_message") then
-- Act on the message here
end
end
```
*/
declare function hash(s: string): hash;
/**
* Returns a hexadecimal representation of a hash value.
The returned string is always padded with leading zeros.
* @param h hash value to get hex string for
* @returns hex representation of the hash
* @example ```lua
local h = hash("my_hash")
local hexstr = hash_to_hex(h)
print(hexstr) --> a2bc06d97f580aab
```
*/
declare function hash_to_hex(h: hash): string;
/**
* Pretty printing of Lua values. This function prints Lua values
in a manner similar to +print()+, but will also recurse into tables
and pretty print them. There is a limit to how deep the function
will recurse.
* @param v value to print
* @example Pretty printing a Lua table with a nested table:
```lua
local t2 = { 1, 2, 3, 4 }
local t = { key = "value", key2 = 1234, key3 = t2 }
pprint(t)
```
Resulting in the following output (note that the key order in non array
Lua tables is undefined):
```lua
{
key3 = {
1 = 1,
2 = 2,
3 = 3,
4 = 4,
}
key2 = 1234,
key = value,
}
```
*/
declare function pprint(...v: any[]): void;
/**
* A unique identifier used to reference resources, messages, properties, and other entities within the game.
*/
declare type hash = Readonly<LuaUserdata & { readonly __hash__: unique symbol; }>;
/**
* A reference to game resources, such as game objects, components, and assets.
*/
declare type url = { socket: hash; path: hash; fragment: hash | undefined; };
/**
* A representation of a GUI object.
*/
declare type node = Readonly<LuaUserdata & { readonly __node__: unique symbol; }>;
/**
* A block of memory that can store binary data.
*/
declare type buffer = object;
/**
* A data stream derived from a buffer.
*/
declare type bufferstream = LuaUserdata & number[] & object;
declare namespace b2d {
/**
* Get the Box2D body from a collision object
* @param url the url to the game object collision component
*/
export function get_body(url: string | hash | url): typeof b2d.body | undefined;
/**
* Get the Box2D world from the current collection
* @returns the world if successful. Otherwise `nil`.
*/
export function get_world(): unknown;
}
declare namespace b2d {
export namespace body {
/**
* Dynamic body
*/
export const B2_DYNAMIC_BODY: number;
/**
* Kinematic body
*/
export const B2_KINEMATIC_BODY: number;
/**
* Static (immovable) body
*/
export const B2_STATIC_BODY: number;
/**
* Apply an angular impulse.
* @param body body
* @param impulse impulse the angular impulse in units of kgmm/s
*/
export function apply_angular_impulse(body: typeof b2d.body, impulse: number): void;
/**
* Apply a force at a world point. If the force is not
applied at the center of mass, it will generate a torque and
affect the angular velocity. This wakes up the body.
* @param body body
* @param force the world force vector, usually in Newtons (N).
* @param point the world position of the point of application.
*/
export function apply_force(body: typeof b2d.body, force: vmath.vector3, point: vmath.vector3): void;
/**
* Apply a force to the center of mass. This wakes up the body.
* @param body body
* @param force the world force vector, usually in Newtons (N).
*/
export function apply_force_to_center(body: typeof b2d.body, force: vmath.vector3): void;
/**
* Apply an impulse at a point. This immediately modifies the velocity.
It also modifies the angular velocity if the point of application
is not at the center of mass. This wakes up the body.
* @param body body
* @param impulse the world impulse vector, usually in N-seconds or kg-m/s.
* @param point the world position of the point of application.
*/
export function apply_linear_impulse(body: typeof b2d.body, impulse: vmath.vector3, point: vmath.vector3): void;
/**
* Apply a torque. This affects the angular velocity
without affecting the linear velocity of the center of mass.
This wakes up the body.
* @param body body
* @param torque torque about the z-axis (out of the screen), usually in N-m.
*/
export function apply_torque(body: typeof b2d.body, torque: number): void;
/**
* You can disable sleeping on this body. If you disable sleeping, the body will be woken.
* @param body body
* @param enable if false, the body will never sleep, and consume more CPU
*/
export function enable_sleep(body: typeof b2d.body, enable: boolean): void;
/**
* Get the angle in radians.
* @param body body
* @returns the current world rotation angle in radians.
*/
export function get_angle(body: typeof b2d.body): number;
/**
* Get the angular damping of the body.
* @param body body
* @returns the damping
*/
export function get_angular_damping(body: typeof b2d.body): number;
/**
* Get the angular velocity.
* @param body body
* @returns the angular velocity in radians/second.
*/
export function get_angular_velocity(body: typeof b2d.body): number;
/**
* Get the gravity scale of the body.
* @param body body
* @returns the scale
*/
export function get_gravity_scale(body: typeof b2d.body): number;
/**
* Get the linear damping of the body.
* @param body body
* @returns the damping
*/
export function get_linear_damping(body: typeof b2d.body): number;
/**
* Get the linear velocity of the center of mass.
* @param body body
* @returns the linear velocity of the center of mass.
*/
export function get_linear_velocity(body: typeof b2d.body): vmath.vector3;
/**
* Get the world velocity of a local point.
* @param body body
* @param local_point a point in local coordinates.
* @returns the world velocity of a point.
*/
export function get_linear_velocity_from_local_point(body: typeof b2d.body, local_point: vmath.vector3): vmath.vector3;
/**
* Get the world linear velocity of a world point attached to this body.
* @param body body
* @param world_point a point in world coordinates.
* @returns the world velocity of a point.
*/
export function get_linear_velocity_from_world_point(body: typeof b2d.body, world_point: vmath.vector3): vmath.vector3;
/**
* Get the local position of the center of mass.
* @param body body
* @returns Get the local position of the center of mass.
*/
export function get_local_center_of_mass(body: typeof b2d.body): vmath.vector3;
/**
* Gets a local point relative to the body's origin given a world point.
* @param body body
* @param world_point a point in world coordinates.
* @returns the corresponding local point relative to the body's origin.
*/
export function get_local_point(body: typeof b2d.body, world_point: vmath.vector3): vmath.vector3;
/**
* Gets a local vector given a world vector.
* @param body body
* @param world_vector a vector in world coordinates.
* @returns the corresponding local vector.
*/
export function get_local_vector(body: typeof b2d.body, world_vector: vmath.vector3): vmath.vector3;
/**
* Get the total mass of the body.
* @param body body
* @returns the mass, usually in kilograms (kg).
*/
export function get_mass(body: typeof b2d.body): number;
/**
* Get the world body origin position.
* @param body body
* @returns the world position of the body's origin.
*/
export function get_position(body: typeof b2d.body): vmath.vector3;
/**
* Get the rotational inertia of the body about the local origin.
* @param body body
* @returns the rotational inertia, usually in kg-m^2.
*/
export function get_rotational_inertia(body: typeof b2d.body): number;
/**
* Get the type of this body.
* @param body body
* @returns the body type
*/
export function get_type(body: typeof b2d.body): unknown;
/**
* Get the parent world of this body.
* @param body body
*/
export function get_world(body: typeof b2d.body): unknown;
/**
* Get the world position of the center of mass.
* @param body body
* @returns Get the world position of the center of mass.
*/
export function get_world_center_of_mass(body: typeof b2d.body): vmath.vector3;
/**
* Get the world coordinates of a point given the local coordinates.
* @param body body
* @param local_vector localPoint a point on the body measured relative the the body's origin.
* @returns the same point expressed in world coordinates.
*/
export function get_world_point(body: typeof b2d.body, local_vector: vmath.vector3): vmath.vector3;
/**
* Get the world coordinates of a vector given the local coordinates.
* @param body body
* @param local_vector a vector fixed in the body.
* @returns the same vector expressed in world coordinates.
*/
export function get_world_vector(body: typeof b2d.body, local_vector: vmath.vector3): vmath.vector3;
/**
* Get the active state of the body.
* @param body body
* @returns is the body active
*/
export function is_active(body: typeof b2d.body): boolean;
/**
* Get the sleeping state of this body.
* @param body body
* @returns true if the body is awake, false if it's sleeping.
*/
export function is_awake(body: typeof b2d.body): boolean;
/**
* Is this body in bullet mode
* @param body body
* @returns true if the body is in bullet mode
*/
export function is_bullet(body: typeof b2d.body): boolean;
/**
* Does this body have fixed rotation?
* @param body body
* @returns is the rotation fixed
*/
export function is_fixed_rotation(body: typeof b2d.body): boolean;
/**
* Is this body allowed to sleep
* @param body body
* @returns true if the body is allowed to sleep
*/
export function is_sleeping_enabled(body: typeof b2d.body): boolean;
/**
* This resets the mass properties to the sum of the mass properties of the fixtures.
This normally does not need to be called unless you called SetMassData to override
* @param body body
*/
export function reset_mass_data(body: typeof b2d.body): void;
/**
* Set the active state of the body. An inactive body is not
simulated and cannot be collided with or woken up.
If you pass a flag of true, all fixtures will be added to the
broad-phase.
If you pass a flag of false, all fixtures will be removed from
the broad-phase and all contacts will be destroyed.
Fixtures and joints are otherwise unaffected. You may continue
to create/destroy fixtures and joints on inactive bodies.
Fixtures on an inactive body are implicitly inactive and will
not participate in collisions, ray-casts, or queries.
Joints connected to an inactive body are implicitly inactive.
An inactive body is still owned by a b2World object and remains
in the body list.
* @param body body
* @param enable true if the body should be active
*/
export function set_active(body: typeof b2d.body, enable: boolean): void;
/**
* Set the angular damping of the body.
* @param body body
* @param damping the damping
*/
export function set_angular_damping(body: typeof b2d.body, damping: number): void;
/**
* Set the angular velocity.
* @param body body
* @param omega the new angular velocity in radians/second.
*/
export function set_angular_velocity(body: typeof b2d.body, omega: number): void;
/**
* Set the sleep state of the body. A sleeping body has very low CPU cost.
* @param body body
* @param enable flag set to false to put body to sleep, true to wake it.
*/
export function set_awake(body: typeof b2d.body, enable: boolean): void;
/**
* Should this body be treated like a bullet for continuous collision detection?
* @param body body
* @param enable if true, the body will be in bullet mode
*/
export function set_bullet(body: typeof b2d.body, enable: boolean): void;
/**
* Set this body to have fixed rotation. This causes the mass to be reset.
* @param body body
* @param enable true if the rotation should be fixed
*/
export function set_fixed_rotation(body: typeof b2d.body, enable: boolean): void;
/**
* Set the gravity scale of the body.
* @param body body
* @param scale the scale
*/
export function set_gravity_scale(body: typeof b2d.body, scale: number): void;
/**
* Set the linear damping of the body.
* @param body body
* @param damping the damping
*/
export function set_linear_damping(body: typeof b2d.body, damping: number): void;
/**
* Set the linear velocity of the center of mass.
* @param body body
* @param velocity the new linear velocity of the center of mass.
*/
export function set_linear_velocity(body: typeof b2d.body, velocity: vmath.vector3): void;
/**
* Set the position of the body's origin and rotation.
This breaks any contacts and wakes the other bodies.
Manipulating a body's transform may cause non-physical behavior.
* @param body body
* @param position the world position of the body's local origin.
* @param angle the world position of the body's local origin.
*/
export function set_transform(body: typeof b2d.body, position: vmath.vector3, angle: number): void;
/**
* Set the type of this body. This may alter the mass and velocity.
* @param body body
* @param type the body type
*/
export function set_type(body: typeof b2d.body, type: any): void;
}
}
declare namespace buffer {
/**
* Float, single precision, 4 bytes
*/
export const VALUE_TYPE_FLOAT32: number;
/**
* Signed integer, 2 bytes
*/
export const VALUE_TYPE_INT16: number;
/**
* Signed integer, 4 bytes
*/
export const VALUE_TYPE_INT32: number;
/**
* Signed integer, 8 bytes
*/
export const VALUE_TYPE_INT64: number;
/**
* Signed integer, 1 byte
*/
export const VALUE_TYPE_INT8: number;
/**
* Unsigned integer, 2 bytes
*/
export const VALUE_TYPE_UINT16: number;
/**
* Unsigned integer, 4 bytes
*/
export const VALUE_TYPE_UINT32: number;
/**
* Unsigned integer, 8 bytes
*/
export const VALUE_TYPE_UINT64: number;
/**
* Unsigned integer, 1 byte
*/
export const VALUE_TYPE_UINT8: number;
/**
* Copy all data streams from one buffer to another, element wise.
⚠ Each of the source streams must have a matching stream in the
destination buffer. The streams must match in both type and size.
The source and destination buffer can be the same.
* @param dst the destination buffer
* @param dstoffset the offset to start copying data to
* @param src the source data buffer
* @param srcoffset the offset to start copying data from
* @param count the number of elements to copy
* @example How to copy elements (e.g. vertices) from one buffer to another
```lua
-- copy entire buffer
buffer.copy_buffer(dstbuffer, 0, srcbuffer, 0, #srcbuffer)
-- copy last 10 elements to the front of another buffer
buffer.copy_buffer(dstbuffer, 0, srcbuffer, #srcbuffer - 10, 10)
```
*/
export function copy_buffer(dst: buffer, dstoffset: number, src: buffer, srcoffset: number, count: number): void;
/**
* Copy a specified amount of data from one stream to another.
⚠ The value type and size must match between source and destination streams.
The source and destination streams can be the same.
* @param dst the destination stream
* @param dstoffset the offset to start copying data to (measured in value type)
* @param src the source data stream
* @param srcoffset the offset to start copying data from (measured in value type)
* @param count the number of values to copy (measured in value type)
* @example How to update a texture of a sprite:
```lua
-- copy entire stream
local srcstream = buffer.get_stream(srcbuffer, hash("xyz"))
local dststream = buffer.get_stream(dstbuffer, hash("xyz"))
buffer.copy_stream(dststream, 0, srcstream, 0, #srcstream)
```
*/
export function copy_stream(dst: bufferstream, dstoffset: number, src: bufferstream, srcoffset: number, count: number): void;
/**
* Create a new data buffer containing a specified set of streams. A data buffer
can contain one or more streams with typed data. This is useful for managing
compound data, for instance a vertex buffer could contain separate streams for
vertex position, color, normal etc.
* @param element_count The number of elements the buffer should hold
* @param declaration A table where each entry (table) describes a stream
hash | string `name`: The name of the stream
constant `type`: The data type of the stream
number `count`: The number of values each element should hold
* @returns the new buffer
* @example How to create and initialize a buffer
```lua
function init(self)
local size = 128
self.image = buffer.create( size * size, { {name=hash("rgb"), type=buffer.VALUE_TYPE_UINT8, count=3 } })
self.imagestream = buffer.get_stream(self.image, hash("rgb"))
for y=0,self.height-1 do
for x=0,self.width-1 do
local index = y * self.width * 3 + x * 3 + 1
self.imagestream[index + 0] = self.r
self.imagestream[index + 1] = self.g
self.imagestream[index + 2] = self.b
end
end
```
*/
export function create(element_count: number, declaration: { hash: hash | string; type: number; count: number }): buffer;
/**
* Get a copy of all the bytes from a specified stream as a Lua string.
* @param buffer the source buffer
* @param stream_name the name of the stream
* @returns the buffer data as a Lua string
*/
export function get_bytes(buffer: buffer, stream_name: hash): string;
/**
* Get a named metadata entry from a buffer along with its type.
* @param buf the buffer to get the metadata from
* @param metadata_name name of the metadata entry
* @returns table of metadata values or `nil` if the entry does not exist & numeric type of values or `nil`
* @example How to get a metadata entry from a buffer
```lua
-- retrieve a metadata entry named "somefloats" and its nomeric type
local values, type = buffer.get_metadata(buf, hash("somefloats"))
if metadata then print(#metadata.." values in 'somefloats'") end
```
*/
export function get_metadata(buf: buffer, metadata_name: hash | string): LuaMultiReturn<[object | undefined, number | undefined]>;
/**
* Get a specified stream from a buffer.
* @param buffer the buffer to get the stream from
* @param stream_name the stream name
* @returns the data stream
*/
export function get_stream(buffer: buffer, stream_name: hash | string): bufferstream;
/**
* Creates or updates a metadata array entry on a buffer.
⚠ The value type and count given when updating the entry should match those used when first creating it.
* @param buf the buffer to set the metadata on
* @param metadata_name name of the metadata entry
* @param values actual metadata, an array of numeric values
* @param value_type type of values when stored
* @example How to set a metadata entry on a buffer
```lua
-- create a new metadata entry with three floats
buffer.set_metadata(buf, hash("somefloats"), {1.5, 3.2, 7.9}, buffer.VALUE_TYPE_FLOAT32)
-- ...
-- update to a new set of values
buffer.set_metadata(buf, hash("somefloats"), {-2.5, 10.0, 32.2}, buffer.VALUE_TYPE_FLOAT32)
```
*/
export function set_metadata(buf: buffer, metadata_name: hash | string, values: number[] | LuaSet<number>, value_type: number): void;
}
declare namespace camera {
/**
* Computes zoom so the original display area covers the entire window while preserving aspect ratio.
Equivalent to using max(window_width/width, window_height/height).
*/
export const ORTHO_MODE_AUTO_COVER: number;
/**
* Computes zoom so the original display area (game.project width/height) fits inside the window
while preserving aspect ratio. Equivalent to using min(window_width/width, window_height/height).
*/
export const ORTHO_MODE_AUTO_FIT: number;
/**
* Uses the manually set orthographic zoom value (camera.set_orthographic_zoom).
*/
export const ORTHO_MODE_FIXED: number;
/**
* Gets the effective aspect ratio of the camera. If auto aspect ratio is enabled,
returns the aspect ratio calculated from the current render target dimensions.
Otherwise returns the manually set aspect ratio.
* @param camera camera id
* @returns the effective aspect ratio.
*/
export function get_aspect_ratio(camera: url | number | undefined): number;
/**
* Returns whether auto aspect ratio is enabled. When enabled, the camera automatically
calculates aspect ratio from render target dimensions. When disabled, uses the
manually set aspect ratio value.
* @param camera camera id
* @returns true if auto aspect ratio is enabled
*/
export function get_auto_aspect_ratio(camera: url | number | undefined): boolean;
/**
* This function returns a table with all the camera URLs that have been
registered in the render context.
* @example ```lua
for k,v in pairs(camera.get_cameras()) do
render.set_camera(v)
render.draw(...)
render.set_camera()
end
```
*/
export function get_cameras(): url[];
/**
* get enabled
* @param camera camera id
* @returns true if the camera is enabled
*/
export function get_enabled(camera: url | number | undefined): boolean;
/**
* get far z
* @param camera camera id
* @returns the far z.
*/
export function get_far_z(camera: url | number | undefined): number;
/**
* get field of view
* @param camera camera id
* @returns the field of view.
*/
export function get_fov(camera: url | number | undefined): number;
/**
* get near z
* @param camera camera id
* @returns the near z.
*/
export function get_near_z(camera: url | number | undefined): number;
/**
* get orthographic zoom mode
* @param camera camera id
* @returns one of camera.ORTHO_MODE_FIXED, camera.ORTHO_MODE_AUTO_FIT or
camera.ORTHO_MODE_AUTO_COVER
*/
export function get_orthographic_mode(camera: url | number | undefined): number;
/**
* get orthographic zoom
* @param camera camera id
* @returns the zoom level when the camera uses orthographic projection.
*/
export function get_orthographic_zoom(camera: url | number | undefined): number;
/**
* get projection matrix
* @param camera camera id
* @returns the projection matrix.
*/
export function get_projection(camera: url | number | undefined): vmath.matrix4;
/**
* get view matrix
* @param camera camera id
* @returns the view matrix.
*/
export function get_view(camera: url | number | undefined): vmath.matrix4;
/**
* Converts a screen-space 2D point with view depth to a 3D world point.
z is the view depth in world units measured from the camera plane along the camera forward axis.
If a camera isn't specified, the last enabled camera is used.
* @param pos Screen-space position (x, y) with z as view depth in world units
* @param camera optional camera id
* @returns the world coordinate
* @example Place objects at the touch point with a random Z position, keeping them within the visible view zone.
```lua
function on_input(self, action_id, action)
if action_id == hash("touch") then
if action.pressed then
local percpective_camera = msg.url("#perspective_camera")
local random_z = math.random(camera.get_near_z(percpective_camera) + 0.01, camera.get_far_z(percpective_camera) - 0.01)
local world_position = camera.screen_to_world(vmath.vector3(action.screen_x, action.screen_y, random_z), percpective_camera)
go.set_position(world_position, "/go1")
end
end
end
```
*/
export function screen_to_world(pos: vmath.vector3, camera?: url | number | undefined): vmath.vector3;
/**
* Converts 2D screen coordinates (x,y) to the 3D world-space point on the camera's near plane for that pixel.
If a camera isn't specified, the last enabled camera is used.
* @param x X coordinate on screen.
* @param y Y coordinate on screen.
* @param camera optional camera id
* @returns the world coordinate on the camera near plane
* @example Place objects at the touch point.
```lua
function on_input(self, action_id, action)
if action_id == hash("touch") then
if action.pressed then
local world_position = camera.screen_xy_to_world(action.screen_x, action.screen_y)
go.set_position(world_position, "/go1")
end
end
end
```
*/
export function screen_xy_to_world(x: number, y: number, camera?: url | number | undefined): vmath.vector3;
/**
* Sets the manual aspect ratio for the camera. This value is only used when
auto aspect ratio is disabled. To disable auto aspect ratio and use this
manual value, call camera.set_auto_aspect_ratio(camera, false).
* @param camera camera id
* @param aspect_ratio the manual aspect ratio value.
*/
export function set_aspect_ratio(camera: url | number | undefined, aspect_ratio: number): void;
/**
* Enables or disables automatic aspect ratio calculation. When enabled (true),
the camera automatically calculates aspect ratio from render target dimensions.
When disabled (false), uses the manually set aspect ratio value.
* @param camera camera id
* @param auto_aspect_ratio true to enable auto aspect ratio
*/
export function set_auto_aspect_ratio(camera: url | number | undefined, auto_aspect_ratio: boolean): void;
/**
* set far z
* @param camera camera id
* @param far_z the far z.
*/
export function set_far_z(camera: url | number | undefined, far_z: number): void;
/**
* set field of view
* @param camera camera id
* @param fov the field of view.
*/
export function set_fov(camera: url | number | undefined, fov: number): void;
/**
* set near z
* @param camera camera id
* @param near_z the near z.
*/
export function set_near_z(camera: url | number | undefined, near_z: number): void;
/**
* set orthographic zoom mode
* @param camera camera id
* @param mode camera.ORTHO_MODE_FIXED, camera.ORTHO_MODE_AUTO_FIT or camera.ORTHO_MODE_AUTO_COVER
*/
export function set_orthographic_mode(camera: url | number | undefined, mode: number): void;
/**
* set orthographic zoom
* @param camera camera id
* @param orthographic_zoom the zoom level when the camera uses orthographic projection.
*/
export function set_orthographic_zoom(camera: url | number | undefined, orthographic_zoom: number): void;
/**
* Converts a 3D world position to screen-space coordinates with view depth.
Returns a vector3 where x and y are in screen pixels and z is the view depth in world units
measured from the camera plane along the camera forward axis. The returned z can be used with
camera.screen_to_world to reconstruct the world position on the same pixel ray.
If a camera isn't specified, the last enabled camera is used.
* @param world_pos World-space position
* @param camera optional camera id
* @returns Screen position (x,y in pixels, z is view depth)
* @example Convert go position into screen pisition
```lua
go.update_world_transform("/go1")
local world_pos = go.get_world_position("/go1")
local screen_pos = camera.world_to_screen(world_pos)
```
*/
export function world_to_screen(world_pos: vmath.vector3, camera?: url | number | undefined): vmath.vector3;
}
declare namespace collectionfactory {
/**
* loaded
*/
export const STATUS_LOADED: number;
/**
* loading
*/
export const STATUS_LOADING: number;
/**
* unloaded
*/
export const STATUS_UNLOADED: number;
/**
* The URL identifies the collectionfactory component that should do the spawning.
Spawning is instant, but spawned game objects get their first update calls the following frame. The supplied parameters for position, rotation and scale
will be applied to the whole collection when spawned.
Script properties in the created game objects can be overridden through
a properties-parameter table. The table should contain game object ids
(hash) as keys and property tables as values to be used when initiating each
spawned game object.
See go.property for more information on script properties.
The function returns a table that contains a key for each game object
id (hash), as addressed if the collection file was top level, and the
corresponding spawned instance id (hash) as value with a unique path
prefix added to each instance.
⚠ Calling collectionfactory.create create on a collection factory that is marked as dynamic without having loaded resources
using collectionfactory.load will synchronously load and create resources which may affect application performance.
* @param url the collection factory component to be used
* @param position position to assign to the newly spawned collection
* @param rotation rotation to assign to the newly spawned collection
* @param properties table of script properties to propagate to any new game object instances
* @param scale uniform scaling to apply to the newly spawned collection (must be greater than 0).
* @example How to spawn a collection of game objects:
```lua
function init(self)
-- Spawn a small group of enemies.
local pos = vmath.vector3(100, 12.5, 0)
local rot = vmath.quat_rotation_z(math.pi / 2)
local scale = 0.5
local props = {}
props[hash("/enemy_leader")] = { health = 1000.0 }
props[hash("/enemy_1")] = { health = 200.0 }
props[hash("/enemy_2")] = { health = 400.0, color = hash("green") }
local self.enemy_ids = collectionfactory.create("#enemyfactory", pos, rot, props, scale)
-- enemy_ids now map to the spawned instance ids:
--
-- pprint(self.enemy_ids)
--
-- DEBUG:SCRIPT:
-- {
-- hash: [/enemy_leader] = hash: [/collection0/enemy_leader],
-- hash: [/enemy_1] = hash: [/collection0/enemy_1],
-- hash: [/enemy_2] = hash: [/collection0/enemy_2]
-- }
-- Send "attack" message to the leader. First look up its instance id.
local leader_id = self.enemy_ids[hash("/enemy_leader")]
msg.post(leader_id, "attack")
end
```
How to delete a spawned collection:
```lua
go.delete(self.enemy_ids)
```
*/
export function create(url: string | hash | url, position?: vmath.vector3, rotation?: vmath.quaternion, properties?: object, scale?: number | vmath.vector3): LuaMap<hash, hash>;
/**
* This returns status of the collection factory.
Calling this function when the factory is not marked as dynamic loading always returns COMP_COLLECTION_FACTORY_STATUS_LOADED.
* @param url the collection factory component to get status from
* @returns status of the collection factory component
`collectionfactory.STATUS_UNLOADED`
`collectionfactory.STATUS_LOADING`
`collectionfactory.STATUS_LOADED`
*/
export function get_status(url?: string | hash | url): number;
/**
* Resources loaded are referenced by the collection factory component until the existing (parent) collection is destroyed or collectionfactory.unload is called.
Calling this function when the factory is not marked as dynamic loading does nothing.
* @param url the collection factory component to load
* @param complete_function function to call when resources are loaded.
`self`
object The current object.
`url`
url url of the collection factory component
`result`
boolean True if resource were loaded successfully
* @example How to load resources of a collection factory prototype.
```lua
collectionfactory.load("#factory", function(self, url, result) end)
```
*/
export function load(url?: string | hash | url, complete_function?: (this: any, url: url, result: boolean,) => void): void;
/**
* Changes the prototype for the collection factory.
Setting the prototype to "nil" will revert back to the original prototype.
* @param url the collection factory component
* @param prototype the path to the new prototype, or `nil`
* @example How to unload the previous prototypes resources, and then spawn a new collection
```lua
collectionfactory.unload("#factory") -- unload the previous resources
collectionfactory.set_prototype("#factory", "/main/levels/level1.collectionc")
local ids = collectionfactory.create("#factory", go.get_world_position(), vmath.quat())
```
*/
export function set_prototype(url?: string | hash | url, prototype?: string | undefined): void;
/**
* This decreases the reference count for each resource loaded with collectionfactory.load. If reference is zero, the resource is destroyed.
Calling this function when the factory is not marked as dynamic loading does nothing.
* @param url the collection factory component to unload
* @example How to unload resources of a collection factory prototype loaded with collectionfactory.load
```lua
collectionfactory.unload("#factory")
```
*/
export function unload(url?: string | hash | url): void;
}
declare namespace collectionproxy {
/**
* It's impossible to change the collection if the collection is already loaded.
*/
export const RESULT_ALREADY_LOADED: number;
/**
* It's impossible to change the collection while the collection proxy is loading.
*/
export const RESULT_LOADING: number;
/**
* It's impossible to change the collection for a proxy that isn't excluded.
*/
export const RESULT_NOT_EXCLUDED: number;
/**
* return an indexed table of resources for a collection proxy where the
referenced collection has been excluded using LiveUpdate. Each entry is a
hexadecimal string that represents the data of the specific resource.
This representation corresponds with the filename for each individual
resource that is exported when you bundle an application with LiveUpdate
functionality.
* @param collectionproxy the collectionproxy to check for resources.
* @example ```lua
local function print_resources(self, cproxy)
local resources = collectionproxy.get_resources(cproxy)
for _, v in ipairs(resources) do
print("Resource: " .. v)
end
end
```
*/
export function get_resources(collectionproxy: url): string[];
/**
* return an array of missing resources for a collection proxy. Each
entry is a hexadecimal string that represents the data of the specific
resource. This representation corresponds with the filename for each
individual resource that is exported when you bundle an application with
LiveUpdate functionality. It should be considered good practise to always
check whether or not there are any missing resources in a collection proxy
before attempting to load the collection proxy.
* @param collectionproxy the collectionproxy to check for missing
resources.
* @example ```lua
function init(self)
end
local function callback(self, id, response)
local expected = self.resources[id]
if response ~= nil and response.status == 200 then
print("Successfully downloaded resource: " .. expected)
resource.store_resource(response.response)
else
print("Failed to download resource: " .. expected)
-- error handling
end
end
local function download_resources(self, cproxy)
self.resources = {}
local resources = collectionproxy.missing_resources(cproxy)
for _, v in ipairs(resources) do
print("Downloading resource: " .. v)
local uri = "http://example.defold.com/" .. v
local id = http.request(uri, "GET", callback)
self.resources[id] = v
end
end
```
*/
export function missing_resources(collectionproxy: url): string[];
/**
* The collection should be loaded by the collection proxy.
Setting the collection to "nil" will revert it back to the original collection.
The collection proxy shouldn't be loaded and should have the 'Exclude' checkbox checked.
This functionality is designed to simplify the management of Live Update resources.
* @param url the collection proxy component
* @param prototype the path to the new collection, or `nil`
* @returns collection change was successful & one of the collectionproxy.RESULT_* codes if unsuccessful
* @example The example assume the script belongs to an instance with collection-proxy-component with id "proxy".
```lua
local ok, error = collectionproxy.set_collection("/go#collectionproxy", "/LU/3.collectionc")
if ok then
print("The collection has been changed to /LU/3.collectionc")
else
print("Error changing collection to /LU/3.collectionc ", error)
end
msg.post("/go#collectionproxy", "load")
msg.post("/go#collectionproxy", "init")
msg.post("/go#collectionproxy", "enable")
```
*/
export function set_collection(url?: string | hash | url, prototype?: string | undefined): LuaMultiReturn<[boolean, number]>;
}
declare namespace crash {
/**
* android build fingerprint
*/
export const SYSFIELD_ANDROID_BUILD_FINGERPRINT: number;
/**
* system device language as reported by sys.get_sys_info
*/
export const SYSFIELD_DEVICE_LANGUAGE: number;
/**
* device model as reported by sys.get_sys_info
*/
export const SYSFIELD_DEVICE_MODEL: number;
/**
* engine version as hash
*/
export const SYSFIELD_ENGINE_HASH: number;
/**
* engine version as release number
*/
export const SYSFIELD_ENGINE_VERSION: number;
/**
* system language as reported by sys.get_sys_info
*/
export const SYSFIELD_LANGUAGE: number;
/**
* device manufacturer as reported by sys.get_sys_info
*/
export const SYSFIELD_MANUFACTURER: number;
/**
* The max number of sysfields.
*/
export const SYSFIELD_MAX: number;
/**
* system name as reported by sys.get_sys_info
*/
export const SYSFIELD_SYSTEM_NAME: number;
/**
* system version as reported by sys.get_sys_info
*/
export const SYSFIELD_SYSTEM_VERSION: number;
/**
* system territory as reported by sys.get_sys_info
*/
export const SYSFIELD_TERRITORY: number;
/**
* The max number of user fields.
*/
export const USERFIELD_MAX: number;
/**
* The max size of a single user field.
*/
export const USERFIELD_SIZE: number;
/**
* A table is returned containing the addresses of the call stack.
* @param handle crash dump handle
* @returns table containing the backtrace
*/
export function get_backtrace(handle: number): object;
/**
* The format of read text blob is platform specific
and not guaranteed
but can be useful for manual inspection.
* @param handle crash dump handle
* @returns string with the platform specific data
*/
export function get_extra_data(handle: number): string;
/**
* The function returns a table containing entries with sub-tables that
have fields 'name' and 'address' set for all loaded modules.
* @param handle crash dump handle
*/
export function get_modules(handle: number): { name: unknown; address: unknown }[];
/**
* read signal number from a crash report
* @param handle crash dump handle
* @returns signal number
*/
export function get_signum(handle: number): number;
/**
* reads a system field from a loaded crash dump
* @param handle crash dump handle
* @param index system field enum. Must be less than crash.SYSFIELD_MAX
* @returns value recorded in the crash dump, or `nil` if it didn't exist
*/
export function get_sys_field(handle: number, index: number): string | undefined;
/**
* reads user field from a loaded crash dump
* @param handle crash dump handle
* @param index user data slot index
* @returns user data value recorded in the crash dump
*/
export function get_user_field(handle: number, index: number): string;
/**
* The crash dump will be removed from disk upon a successful
load, so loading is one-shot.
* @returns handle to the loaded dump, or `nil` if no dump was found
*/
export function load_previous(): number | undefined;
/**
* releases a previously loaded crash dump
* @param handle handle to loaded crash dump
*/
export function release(handle: number): void;
/**
* Crashes occuring before the path is set will be stored to a default engine location.
* @param path file path to use
*/
export function set_file_path(path: string): void;
/**
* Store a user value that will get written to a crash dump when
a crash occurs. This can be user id:s, breadcrumb data etc.
There are 32 slots indexed from 0. Each slot stores at most 255 characters.
* @param index slot index. 0-indexed
* @param value string value to store
*/
export function set_user_field(index: number, value: string): void;
/**
* Performs the same steps as if a crash had just occured but
allows the program to continue.
The generated dump can be read by crash.load_previous
*/
export function write_dump(): void;
}
declare namespace factory {
/**
* loaded
*/
export const STATUS_LOADED: number;
/**
* loading
*/
export const STATUS_LOADING: number;
/**
* unloaded
*/
export const STATUS_UNLOADED: number;
/**
* The URL identifies which factory should create the game object.
If the game object is created inside of the frame (e.g. from an update callback), the game object will be created instantly, but none of its component will be updated in the same frame.
Properties defined in scripts in the created game object can be overridden through the properties-parameter below.
See go.property for more information on script properties.
⚠ Calling factory.create on a factory that is marked as dynamic without having loaded resources
using factory.load will synchronously load and create resources which may affect application performance.
* @param url the factory that should create a game object.
* @param position the position of the new game object, the position of the game object calling `factory.create()` is used by default, or if the value is `nil`.
* @param rotation the rotation of the new game object, the rotation of the game object calling `factory.create()` is used by default, or if the value is `nil`.
* @param properties the properties defined in a script attached to the new game object.
* @param scale the scale of the new game object (must be greater than 0), the scale of the game object containing the factory is used by default, or if the value is `nil`
* @returns the global id of the spawned game object
* @example How to create a new game object:
```lua
function init(self)
-- create a new game object and provide property values
self.my_created_object = factory.create("#factory", nil, nil, {my_value = 1})
-- communicate with the object
msg.post(self.my_created_object, "hello")
end
```
And then let the new game object have a script attached:
```lua
go.property("my_value", 0)
function init(self)
-- do something with self.my_value which is now one
end
```
*/
export function create(url: string | hash | url, position?: vmath.vector3, rotation?: vmath.quaternion, properties?: object, scale?: number | vmath.vector3): hash;
/**
* This returns status of the factory.
Calling this function when the factory is not marked as dynamic loading always returns
factory.STATUS_LOADED.
* @param url the factory component to get status from
* @returns status of the factory component
`factory.STATUS_UNLOADED`
`factory.STATUS_LOADING`
`factory.STATUS_LOADED`
*/
export function get_status(url?: string | hash | url): number;
/**
* Resources are referenced by the factory component until the existing (parent) collection is destroyed or factory.unload is called.
Calling this function when the factory is not marked as dynamic loading does nothing.
* @param url the factory component to load
* @param complete_function function to call when resources are loaded.
`self`
object The current object.
`url`
url url of the factory component
`result`
boolean True if resources were loaded successfully
* @example How to load resources of a factory prototype.
```lua
factory.load("#factory", function(self, url, result) end)
```
*/
export function load(url?: string | hash | url, complete_function?: (this: any, url: url, result: boolean,) => void): void;
/**
* Changes the prototype for the factory.
* @param url the factory component
* @param prototype the path to the new prototype, or `nil`
* @example How to unload the previous prototypes resources, and then spawn a new game object
```lua
factory.unload("#factory") -- unload the previous resources
factory.set_prototype("#factory", "/main/levels/enemyA.goc")
local id = factory.create("#factory", go.get_world_position(), vmath.quat())
```
*/
export function set_prototype(url?: string | hash | url, prototype?: string | undefined): void;
/**
* This decreases the reference count for each resource loaded with factory.load. If reference is zero, the resource is destroyed.
Calling this function when the factory is not marked as dynamic loading does nothing.
* @param url the factory component to unload
* @example How to unload resources of a factory prototype loaded with factory.load
```lua
factory.unload("#factory")
```
*/
export function unload(url?: string | hash | url): void;
}
declare namespace font {
/**
* associates a ttf resource to a .fontc file.
* @param fontc The path to the .fontc resource
* @param ttf The path to the .ttf resource
* @example ```lua
local font_hash = hash("/assets/fonts/roboto.fontc")
local ttf_hash = hash("/assets/fonts/Roboto/Roboto-Bold.ttf")
font.add_font(font_hash, ttf_hash)
```
*/
export function add_font(fontc: string | hash, ttf: string | hash): void;
/**
* Gets information about a font, such as the associated font files
* @param fontc The path to the .fontc resource
*/
export function get_info(fontc: string | hash): { path: hash, fonts: { path: string, path_hash: hash }[] };
/**
* prepopulates the font glyph cache with rasterised glyphs
* @param fontc The path to the .fontc resource
* @param text The text to layout
* @param callback (optional) A callback function that is called after the request is finished
`self`
object The current object.
`request_id`
number The request id
`result`
boolean True if request was succesful
`errstring`
string `nil` if the request was successful
* @returns Returns the asynchronous request id
* @example ```lua
local font_hash = hash("/assets/fonts/roboto.fontc")
font.prewarm_text(font_hash, "Some text", function (self, request_id, result, errstring)
-- cache is warm, show the text!
end)
```
*/
export function prewarm_text(fontc: string | hash, text: string, callback?: ((this: any, request_id: any, result: any, errstring: any) => void)): number;
/**
* associates a ttf resource to a .fontc file
* @param fontc The path to the .fontc resource
* @param ttf The path to the .ttf resource
* @example ```lua
local font_hash = hash("/assets/fonts/roboto.fontc")
local ttf_hash = hash("/assets/fonts/Roboto/Roboto-Bold.ttf")
font.remove_font(font_hash, ttf_hash)
```
*/
export function remove_font(fontc: string | hash, ttf: string | hash): void;
}
declare namespace go {
/**
* in-back
*/
export const EASING_INBACK: number;
/**
* in-bounce
*/
export const EASING_INBOUNCE: number;
/**
* in-circlic
*/
export const EASING_INCIRC: number;
/**
* in-cubic
*/
export const EASING_INCUBIC: number;
/**
* in-elastic
*/
export const EASING_INELASTIC: number;
/**
* in-exponential
*/
export const EASING_INEXPO: number;
/**
* in-out-back
*/
export const EASING_INOUTBACK: number;
/**
* in-out-bounce
*/
export const EASING_INOUTBOUNCE: number;
/**
* in-out-circlic
*/
export const EASING_INOUTCIRC: number;
/**
* in-out-cubic
*/
export const EASING_INOUTCUBIC: number;
/**
* in-out-elastic
*/
export const EASING_INOUTELASTIC: number;
/**
* in-out-exponential
*/
export const EASING_INOUTEXPO: number;
/**
* in-out-quadratic
*/
export const EASING_INOUTQUAD: number;
/**
* in-out-quartic
*/
export const EASING_INOUTQUART: number;
/**
* in-out-quintic
*/
export const EASING_INOUTQUINT: number;
/**
* in-out-sine
*/
export const EASING_INOUTSINE: number;
/**
* in-quadratic
*/
export const EASING_INQUAD: number;
/**
* in-quartic
*/
export const EASING_INQUART: number;
/**
* in-quintic
*/
export const EASING_INQUINT: number;
/**
* in-sine
*/
export const EASING_INSINE: number;
/**
* linear interpolation
*/
export const EASING_LINEAR: number;
/**
* out-back
*/
export const EASING_OUTBACK: number;
/**
* out-bounce
*/
export const EASING_OUTBOUNCE: number;
/**
* out-circlic
*/
export const EASING_OUTCIRC: number;
/**
* out-cubic
*/
export const EASING_OUTCUBIC: number;
/**
* out-elastic
*/
export const EASING_OUTELASTIC: number;
/**
* out-exponential
*/
export const EASING_OUTEXPO: number;
/**
* out-in-back
*/
export const EASING_OUTINBACK: number;
/**
* out-in-bounce
*/
export const EASING_OUTINBOUNCE: number;
/**
* out-in-circlic
*/
export const EASING_OUTINCIRC: number;
/**
* out-in-cubic
*/
export const EASING_OUTINCUBIC: number;
/**
* out-in-elastic
*/
export const EASING_OUTINELASTIC: number;
/**
* out-in-exponential
*/
export const EASING_OUTINEXPO: number;
/**
* out-in-quadratic
*/
export const EASING_OUTINQUAD: number;
/**
* out-in-quartic
*/
export const EASING_OUTINQUART: number;
/**
* out-in-quintic
*/
export const EASING_OUTINQUINT: number;
/**
* out-in-sine
*/
export const EASING_OUTINSINE: number;
/**
* out-quadratic
*/
export const EASING_OUTQUAD: number;
/**
* out-quartic
*/
export const EASING_OUTQUART: number;
/**
* out-quintic
*/
export const EASING_OUTQUINT: number;
/**
* out-sine
*/
export const EASING_OUTSINE: number;
/**
* loop backward
*/
export const PLAYBACK_LOOP_BACKWARD: number;
/**
* loop forward
*/
export const PLAYBACK_LOOP_FORWARD: number;
/**
* ping pong loop
*/
export const PLAYBACK_LOOP_PINGPONG: number;
/**
* no playback
*/
export const PLAYBACK_NONE: number;
/**
* once backward
*/
export const PLAYBACK_ONCE_BACKWARD: number;
/**
* once forward
*/
export const PLAYBACK_ONCE_FORWARD: number;
/**
* once ping pong
*/
export const PLAYBACK_ONCE_PINGPONG: number;
/**
* This is only supported for numerical properties. If the node property is already being
animated, that animation will be canceled and replaced by the new one.
If a `complete_function` (lua function) is specified, that function will be called when the animation has completed.
By starting a new animation in that function, several animations can be sequenced together. See the examples for more information.
⚠ If you call `go.animate()` from a game object's `final()` function,
any passed `complete_function` will be ignored and never called upon animation completion.
See the properties guide for which properties can be animated and the animation guide for how
them.
* @param url url of the game object or component having the property
* @param property id of the property to animate
* @param playback playback mode of the animation
`go.PLAYBACK_ONCE_FORWARD`
`go.PLAYBACK_ONCE_BACKWARD`
`go.PLAYBACK_O