Collection factory

In my collection, that Im changing I have label. How can I change its text from script? (I cant find correct url to this object) And where can I read about Collection factory?

Thanks, I found API reference (collectionfactory) (defold.com)

Okay, I still don’t understand how to change a banal text

You use label.set_text(label_url, text) to change the text (documentation). I guess it is the first part, the label_url which is confusing to you?

Two examples:

-- if the script and label are on the same game object
-- assumes that the id of the label component is "label_id"
label.set_text("#label_id", "Hello there!")

-- if the script and the label are on different game objects
-- assumes that the game object with the label has id "foobar"
label.set_text("/foobar#label_id", "Hello there")

To learn more about addressing of components read here:

I think he’s asking how to address the label of the game object created dynamically by a collection factory .

1 Like

Ah, ok, yes, that makes sense. Ok, take a look at the example in the API reference:

The collectionfactory.create() will return a Lua table with mapping from original ids in your collection to the generated ids of the created game objects.

Let’s say you have a gameobject with if “foobar” with a label with id “mylabel” and you spawn an instance of it using a collection factory:

-- ids will contain a mapping of original game object ids to generated ones
local ids = collectionfactory.create("#myfactory")

-- here's what the lookup table looks like:
pprint(ids)
-- DEBUG:SCRIPT:
-- {
--   hash: [/foobar] = hash: [/collection0/foobar],
-- }

-- get the id of the spawned foobar game object
local foobar_id = ids[hash("/foobar")]

-- create a url pointing to the label in the spawned game object
local label_url = msg.url(nil, foobar_id, "mylabel")

-- use the url to set the text
label.set_text(label_url, "Hello world")
1 Like