Gameobject

Namespace: dmGameObject Language: C++ Type: Defold C++ File: gameobject.h Source: engine/gameobject/src/dmsdk/gameobject/gameobject.h Include: dmsdk/gameobject/gameobject.h

API for manipulating game objects

API

AcquireInstanceIndex

Type: FUNCTION Retrieve an instance index from the index pool for the collection.

Parameters

Returns

AddDynamicResourceHash

Type: FUNCTION Adds a reference to a dynamically created resource into the collection. If the resource is not released before the collection is being destroyed, the collection will automatically free the resource.

Parameters

AssignInstanceIndex

Type: FUNCTION Assign an index to the instance, only if the instance is not null.

Parameters

CreateInstanceId

Type: FUNCTION Creates a new unique instance ID and returns its hash.

Returns

CreateResult

Type: ENUM Create result enum.

Members

Delete

Type: FUNCTION Delete gameobject instance

Parameters

DeleteBones

Type: FUNCTION Recursively delete all instances flagged as bones under the given parent instance. The order of deletion is depth-first, so that the children are deleted before the parents.

Parameters

GAMEOBJECT_CONTEXT_NAME

Type: CONSTANT Name used when registering the game object context with the engine context registry.

GetAbsoluteIdentifier

Type: FUNCTION Get absolute identifier relative to instance. The returned identifier is the representation of the qualified name, i.e. the path from root-collection to the sub-collection which the instance belongs to. Example: if the instance is part of a sub-collection in the root-collection named “sub” and id == “a” the returned identifier represents the path “sub.a”

Parameters

Returns

GetCollection

Type: FUNCTION Retrieve a collection from the specified instance

Parameters

Returns

GetCollectionByHash

Type: FUNCTION Retrieve a collection by socket name hash Note: in native extensions, the register can be retrieved during init using dmEngine::GetGameObjectRegister(dmExtension::AppParams *params)

Parameters

Returns

GetComponent

Type: FUNCTION Get the component, component type and its world

Parameters

Returns

GetComponentId

Type: FUNCTION Get component id from component index.

Parameters

Returns

GetComponentTypeIndex

Type: FUNCTION Get the component type index

Parameters

Returns

GetContext

Type: FUNCTION Retrieve the context for a component type

Parameters

Returns

GetGeneration

Type: FUNCTION Get instance generation counter. The generation changes whenever a new game object instance is allocated, even if it later reuses the same identifier.

Parameters

Returns

GetIdentifier

Type: FUNCTION Get instance identifier

Parameters

Returns

GetInstanceFromIdentifier

Type: FUNCTION Get instance from identifier

Parameters

Returns

GetMessageSocket

Type: FUNCTION Retrieve the message socket for the specified collection.

Parameters

Returns

GetPosition

Type: FUNCTION Get gameobject instance position

Parameters

Returns

GetPropertyAsBool

Type: FUNCTION Retrieve a boolean property from a component.

Parameters

Returns

GetPropertyAsFloat

Type: FUNCTION Retrieve a float property from a component.

Parameters

Returns

GetPropertyAsHash

Type: FUNCTION Retrieve a hash property from a component.

Parameters

Returns

GetPropertyAsMatrix

Type: FUNCTION Retrieve a matrix4 property from a component.

Parameters

Returns

GetPropertyAsQuat

Type: FUNCTION Retrieve a quaternion property from a component.

Parameters

Returns

GetPropertyAsText

Type: FUNCTION Retrieve a text property from a component. The returned pointer is borrowed from the component and must not be freed. Its lifetime is controlled by the component.

Parameters

Returns

GetPropertyAsURL

Type: FUNCTION Retrieve a url property from a component.

Parameters

Returns

GetPropertyAsVector3

Type: FUNCTION Retrieve a vector3 property from a component.

Parameters

Returns

GetPropertyAsVector4

Type: FUNCTION Retrieve a vector4 property from a component.

Parameters

Returns

GetPropertyOptionsCount

Type: FUNCTION Get the property count from a PropertyOptions container

Parameters

Returns

GetPropertyOptionsIndex

Type: FUNCTION Get the index value from a property option at a specific index

Parameters

Returns

GetPropertyOptionsKey

Type: FUNCTION Get the key value from a property option at a specific index

Parameters

Returns

GetRotation

Type: FUNCTION Get gameobject instance rotation

Parameters

Returns

GetScale

Type: FUNCTION Get gameobject instance scale

Parameters

Returns

GetUniformScale

Type: FUNCTION Get gameobject instance uniform scale

Parameters

Returns

GetWorld

Type: FUNCTION Retrieve the world in the collection connected to the supplied component

Parameters

Returns

GetWorldMatrix

Type: FUNCTION Get game object instance world transform as Matrix4.

Parameters

Returns

GetWorldPosition

Type: FUNCTION Get gameobject instance world position

Parameters

Returns

GetWorldRotation

Type: FUNCTION Get gameobject instance world rotation

Parameters

Returns

GetWorldScale

Type: FUNCTION Get game object instance world transform

Parameters

Returns

GetWorldTransform

Type: FUNCTION Get game object instance world transform

Parameters

Returns

GetWorldUniformScale

Type: FUNCTION Get game object instance uniform scale

Parameters

Returns

HCollection

Type: TYPEDEF Gameobject collection handle

HComponent

Type: TYPEDEF Opaque handle to component instance

HComponentInternal

Type: TYPEDEF Opaque handle to internal representation of a component instance

HComponentWorld

Type: TYPEDEF Opaque handle to a component world

HInstance

Type: TYPEDEF Gameobject instance handle

HProperties

Type: TYPEDEF Gameobject properties handle

HPropertyContainer

Type: TYPEDEF Handle to a list of properties (gameobject_props.h)

HPropertyOptions

Type: TYPEDEF Handle to a list of property options

HPrototype

Type: TYPEDEF Gameobject prototype handle

HRegister

Type: TYPEDEF Collection register.

HScript

Type: TYPEDEF Script handle

HScriptInstance

Type: TYPEDEF Script instance handle

InputAction

Type: STRUCT Container of input related information.

InputResult

Type: ENUM Input result enum

Members

InstanceIdMap

Type: TYPEDEF Used for mapping instance ids from a collection definition to newly spawned instances

InstancePropertyContainers

Type: TYPEDEF Contains property containers for game objects to be spawned

INVALID_INSTANCE_POOL_INDEX

Type: CONSTANT Value for an invalid instance index, this must be the same as defined in gamesys_ddf.proto for Create#index.

IsBone

Type: FUNCTION Check whether the instance is flagged as a bone.

Parameters

Returns

New

Type: FUNCTION Create a new gameobject instance

Notes

Parameters

Returns

Playback

Type: ENUM Playback type enum

PropertyDesc

Type: STRUCT Description of a property. If the property is externally mutable, m_ValuePtr points to the value and its length is m_ElementCount. m_Variant always reflects the value.

Members

PropertyResult

Type: ENUM Property result.

Members

PropertyType

Type: ENUM Property types.

Members

PropertyValueType

Type: ENUM Type of property value

Members

PropertyVar

Type: STRUCT Property variant that holds the data for a variable

Members

Result

Type: ENUM Result enumeration.

Members

SceneNode

Type: STRUCT Opaque struct that holds info about the current node

Notes

SceneNodeIterator

Type: STRUCT Opaque struct that holds info about the current position when traversing the scene

SceneNodeProperty

Type: STRUCT Struct that holds info about the current position when traversing the scene

Members

m_Number

double floating point number</dd>

m_Hash

dmhash_t The hashed value.</dd>

m_URL

char[1024] The text representation of the url (if reverse hashes are enabled)</dd>

m_V4

float[4] Used for Vector3, Vector4 and Quat</dd>

m_Bool

bool A boolean value</dd>

m_Text

const char* Text from a text property</dd> </dl>

SceneNodePropertyIterator

Type: STRUCT Holds the property

Members

  • m_Property (dmGameObject::SceneNodeProperty) - property

SceneNodePropertyType

Type: ENUM scene node property types

Members

  • dmGameObject::SCENE_NODE_PROPERTY_TYPE_NUMBER
  • dmGameObject::SCENE_NODE_PROPERTY_TYPE_HASH
  • dmGameObject::SCENE_NODE_PROPERTY_TYPE_URL
  • dmGameObject::SCENE_NODE_PROPERTY_TYPE_VECTOR3
  • dmGameObject::SCENE_NODE_PROPERTY_TYPE_VECTOR4
  • dmGameObject::SCENE_NODE_PROPERTY_TYPE_QUAT
  • dmGameObject::SCENE_NODE_PROPERTY_TYPE_BOOLEAN
  • dmGameObject::SCENE_NODE_PROPERTY_TYPE_TEXT
  • dmGameObject::SCENE_NODE_PROPERTY_TYPE_COUNT

SceneNodeType

Type: ENUM scene node types

Members

  • dmGameObject::SCENE_NODE_TYPE_COLLECTION
  • dmGameObject::SCENE_NODE_TYPE_GAMEOBJECT
  • dmGameObject::SCENE_NODE_TYPE_COMPONENT
  • dmGameObject::SCENE_NODE_TYPE_SUBCOMPONENT

SetBone

Type: FUNCTION Set whether the instance should be flagged as a bone. Instances flagged as bones can have their transforms updated in a batch through SetBoneTransforms. Used for animated skeletons.

Parameters

  • instance (HInstance) - Instance
  • bone (bool) - true if the instance is a bone

SetBoneTransforms

Type: FUNCTION Set the local transforms recursively of all instances flagged as bones, starting with component with id. The order of the transforms is depth-first.

Parameters

  • instance (HInstance) - First Instance of the hierarchy to set
  • component_transform (dmTransform::Transform) - the transform for component root
  • transforms (dmTransform::Transform*) - Array of transforms to set depth-first for the bone instances
  • transform_count (uint32_t) - Size of the transforms array

Returns

  • Number - of instances found

SetIdentifier

Type: FUNCTION Set instance identifier. Must be unique within the collection.

Parameters

  • collection (dmGameObject::HCollection) - Collection
  • instance (dmGameObject::HInstance) - Instance
  • identifier (dmhash_t) - Identifier

Returns

  • result (dmGameObject::Result) - RESULT_OK on success

SetPosition

Type: FUNCTION Set gameobject instance position

Parameters

  • instance (dmGameObject::HInstance) - Gameobject instance
  • position (dmVMath::Point3) - New Position

SetPropertyFromBool

Type: FUNCTION Sets the value of a boolean property on a component.

Parameters

  • instance (HInstance) - Instance of the game object
  • component_id (dmhash_t) - Id of the component
  • property_id (dmhash_t) - Id of the property
  • value (bool) - Value of the property

Returns

  • PROPERTY_RESULT_OK - if the value could be set

SetPropertyFromHash

Type: FUNCTION Sets the value of a hash property on a component.

Parameters

  • instance (HInstance) - Instance of the game object
  • component_id (dmhash_t) - Id of the component
  • property_id (dmhash_t) - Id of the property
  • value (dmhash_t) - Value of the property

Returns

  • PROPERTY_RESULT_OK - if the value could be set

SetPropertyFromHash

Type: FUNCTION Sets the value of a float property on a component.

Parameters

  • instance (HInstance) - Instance of the game object
  • component_id (dmhash_t) - Id of the component
  • property_id (dmhash_t) - Id of the property
  • value (float) - Value of the property

Returns

  • PROPERTY_RESULT_OK - if the value could be set

SetPropertyFromMatrix4

Type: FUNCTION Sets the value of a matrix4 property on a component.

Parameters

  • instance (HInstance) - Instance of the game object
  • component_id (dmhash_t) - Id of the component
  • property_id (dmhash_t) - Id of the property
  • value (dmVMath::Matrix4) - Value of the property

Returns

  • PROPERTY_RESULT_OK - if the value could be set

SetPropertyFromQuat

Type: FUNCTION Sets the value of a quaternion property on a component.

Parameters

  • instance (HInstance) - Instance of the game object
  • component_id (dmhash_t) - Id of the component
  • property_id (dmhash_t) - Id of the property
  • value (dmVMath::Quat) - Value of the property

Returns

  • PROPERTY_RESULT_OK - if the value could be set

SetPropertyFromText

Type: FUNCTION Sets the value of a text property on a component. The component must copy the value if it needs to retain it after this function returns.

Parameters

  • instance (HInstance) - Instance of the game object
  • component_id (dmhash_t) - Id of the component
  • property_id (dmhash_t) - Id of the property
  • value (const char*) - Null-terminated UTF-8 value of the property

Returns

  • PROPERTY_RESULT_OK - if the value could be set

SetPropertyFromURL

Type: FUNCTION Sets the value of a URL property on a component.

Parameters

  • instance (HInstance) - Instance of the game object
  • component_id (dmhash_t) - Id of the component
  • property_id (dmhash_t) - Id of the property
  • value (dmMessage::URL) - Value of the property

Returns

  • PROPERTY_RESULT_OK - if the value could be set

SetPropertyFromVector3

Type: FUNCTION Sets the value of a vector3 property on a component.

Parameters

  • instance (HInstance) - Instance of the game object
  • component_id (dmhash_t) - Id of the component
  • property_id (dmhash_t) - Id of the property
  • value (dmVMath::vector3) - Value of the property

Returns

  • PROPERTY_RESULT_OK - if the value could be set

SetPropertyFromVector4

Type: FUNCTION Sets the value of a vector4 property on a component.

Parameters

  • instance (HInstance) - Instance of the game object
  • component_id (dmhash_t) - Id of the component
  • property_id (dmhash_t) - Id of the property
  • value (dmVMath::Vector4) - Value of the property

Returns

  • PROPERTY_RESULT_OK - if the value could be set

SetRotation

Type: FUNCTION Set gameobject instance rotation

Parameters

  • instance (dmGameObject::HInstance) - Gameobject instance
  • rotation (dmVmath::Quat) - New rotation

SetScale

Type: FUNCTION Set gameobject instance uniform scale

Parameters

  • instance (dmGameObject::HInstance) - Gameobject instance
  • scale (float) - New uniform scale

SetScale

Type: FUNCTION Set gameobject instance non-uniform scale

Parameters

  • instance (dmGameObject::HInstance) - Gameobject instance
  • scale (dmVmath::Vector3) - New non-uniform scale

SetScaleXY

Type: FUNCTION Set gameobject instance x and y scale

Parameters

  • instance (dmGameObject::HInstance) - Gameobject instance
  • scale_x (float) - New x scale
  • scale_y (float) - New y scale

Spawn

Type: FUNCTION Spawns a new gameobject instance. The actual creation is performed after the update is completed.

Parameters

  • collection (HCollection) - Gameobject collection
  • prototype (HPrototype) - Prototype
  • prototype_name (const char*) - Prototype file name (.goc)
  • id (dmhash_t) - Id of the spawned instance
  • properties (HPropertyContainer) - Container with override properties
  • position (dmVMath::Vector3) - Position of the spawed object
  • rotation (dmVMath::Quat) - Rotation of the spawned object
  • scale (dmVMath::Vector3) - Scale of the spawned object return instance HInstance the spawned instance, 0 at failure

TraverseGetRoot

Type: FUNCTION Gets the top node of the whole game (the main collection)

Notes

  • The dmGameObject::HRegister is obtained from the dmEngine::GetGameObjectRegister(dmExtension::AppParams)
  • Traversing the scene like this is not efficient. These functions are here for inspection and testing purposes only.

Parameters

  • regist (dmGameObject::HRegister) - the full gameobject register
  • node (dmGameObject::HRegister) - the node to inspect

Returns

  • result (bool) - True if successful

Examples

The following examples show how to iterate over currently loaded scene graph

void OutputNode(dmGameObject::SceneNode* node) {
    dmGameObject::SceneNodeIterator it = dmGameObject::TraverseIterateChildren(node);
    while(dmGameObject::TraverseIterateNext(&it))
    {
        OutputProperties(&it.m_Node); // see dmGameObject::TraverseIterateProperties()
        OutputNode(&it.m_Node);
    }
}

bool OutputScene(HRegister regist) {
    dmGameObject::SceneNode root;
    if (!dmGameObject::TraverseGetRoot(regist, &root))
        return false;
    OutputNode(&node);
}

TraverseIterateChildren

Type: FUNCTION Get a scene node iterator for the nodes’ children

Parameters

  • node (dmGameObject::SceneNode*) - the parent node

Returns

  • iterator (dmGameObject::SceneNodeIterator) - the iterator

TraverseIterateNext

Type: FUNCTION Step a scene node iterator to the next sibling

Parameters

  • it (dmGameObject::SceneNodeIterator*) - the iterator

Returns

  • result (bool) - true if successful. false if the iterator is finished

TraverseIterateProperties

Type: FUNCTION Create a scene node traversal property iterator

Notes

  • Getting the properties like this is not efficient. These functions are here for inspection and testing purposes only.
  • Reverse hashes via dmHashReverseSafe64() isn’t available in release builds.

Parameters

  • node (dmGameObject::SceneNode*) - the node to inspect

Returns

  • iterator (dmGameObject::SceneNodePropertyIterator) - the property iterator

Examples

The following examples show how to iterate over the properties of a node

dmGameObject::SceneNodePropertyIterator pit = TraverseIterateProperties(node);
while(dmGameObject::TraverseIteratePropertiesNext(&pit))
{
    const char* name = dmHashReverseSafe64(pit.m_Property.m_NameHash);
    switch(pit.m_Property.m_Type)
    {
    case dmGameObject::SCENE_NODE_PROPERTY_TYPE_NUMBER: ...
    ...
    }
}

TraverseIteratePropertiesNext

Type: FUNCTION Steps the scene node traversal property iterator to the next property

Parameters

  • it (dmGameObject::SceneNodePropertyIterator*) - the iterator

Returns

  • finished (bool) - True if the iterator it valid, false if the iterator is finished.

UpdateContext

Type: STRUCT Update context

Members

  • m_TimeScale (float) - the scaling factor what was applied on the dt (i.e. the collection update time scale)
  • m_DT (float) - the delta time elapsed since last frame (seconds)
  • m_FixedUpdateFrequency (uint32_t) - Number of of calls per second to the FixedUpdate of each component

UpdateResult

Type: ENUM Update result enum.

Members

  • dmGameObject::UPDATE_RESULT_OK
  • dmGameObject::UPDATE_RESULT_UNKNOWN_ERROR