I have a game object with a factory and I would like to get the size of the object that this factory creates. I need that size because that’s how I’m going to position the object once it gets created.
How can I access that?
Game objects don’t really have a size on itself (they have position/rotation/scale) but a component attached to a game object might have a size, for example a sprite.
Can you describe a bit more about your setup?
Have a read here about properties on game objects/components and how to get/set them; https://www.defold.com/manuals/properties/
Yes, I would like to get the width of a sprite attached to gameobject that the factory that is attached to the current object should create.
This is how I get the width:
go.get("#sprite", "size.x")
But how to reference the desired gameobject?
If created with a factory, the call tofactory.create
returns the id of the created gameobject
local gameobject_id = factory.create(...)
Combining the last two posts would mean something like this:
local go_id = factory.create(...)
local sprite_url = msg.url(nil, go_id, "sprite")
print(go.get(sprite_url, "size.x"))
Thanks this worked. But, one last thing, could you please explain this line:
local sprite_url = msg.url(nil, go_id, "sprite")
I understand that we are creating a “url” for the sprite component, but what is supposed to be the first argument that we have as “nil”? I tried reading the “Message” section on the API page but I couldn’t find the answer to this question.
The documentation of msg.url():
The format of the string must be
[socket:][path][#fragment]
, which is similar to a HTTP URL. When addressing instances:
socket
is the name of a valid world (a collection)
path
is the id of the instance, which can either be relative the instance of the calling script or global
fragment
would be the id of the desired component