Collection factory API documentation

Version: stable

Functions for controlling collection factory components which are used to dynamically spawn collections into the runtime.

FUNCTION
collectionfactory.create()

Spawn a new instance of a collection into the existing collection.

collectionfactory.get_status()

Get collection factory status

collectionfactory.load()

Load resources of a collection factory prototype.

collectionfactory.unload()

Unload resources previously loaded using collectionfactory.load

CONSTANT
collectionfactory.STATUS_LOADED

loaded

collectionfactory.STATUS_LOADING

loading

collectionfactory.STATUS_UNLOADED

unloaded

Functions

collectionfactory.create()

collectionfactory.create(url,[position],[rotation],[properties],[scale])

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.

PARAMETERS

url

string | hash | url the collection factory component to be used

[position]

vector3 position to assign to the newly spawned collection

[rotation]

quaternion rotation to assign to the newly spawned collection

[properties]

table table of script properties to propagate to any new game object instances

[scale]

number uniform scaling to apply to the newly spawned collection (must be greater than 0).

RETURNS

ids

table a table mapping the id:s from the collection to the new instance id:s

EXAMPLES

How to spawn a collection of game objects:

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:

go.delete(self.enemy_ids)

collectionfactory.get_status()

collectionfactory.get_status([url])

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.

PARAMETERS

[url]

string | hash | url the collection factory component to get status from

RETURNS

status

constant status of the collection factory component

  • collectionfactory.STATUS_UNLOADED
  • collectionfactory.STATUS_LOADING
  • collectionfactory.STATUS_LOADED

collectionfactory.load()

collectionfactory.load([url],[complete_function])

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.

PARAMETERS

[url]

string | hash | url the collection factory component to load

[complete_function]

function(self, url, result)) 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

EXAMPLES

How to load resources of a collection factory prototype.

collectionfactory.load("#factory", function(self, url, result) end)

collectionfactory.unload()

collectionfactory.unload([url])

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.

PARAMETERS

[url]

string | hash | url the collection factory component to unload

EXAMPLES

How to unload resources of a collection factory prototype loaded with collectionfactory.load

collectionfactory.unload("#factory")

Constants

collectionfactory.STATUS_LOADED

loaded


collectionfactory.STATUS_LOADING

loading


collectionfactory.STATUS_UNLOADED

unloaded