Where does a factory spawns a collection? (SOLVED)

Hello everybody!
I have another (probably pretty easy) question.

I have created a 2d character and I want to spawn a “helmet.collection” which contains a “helmet.go”. This “helmet.go” should be placed on top of my characters head. So I created the following code in my “character_controller.script”:

function on_input(self, action_id, action)
    	
    	if action_id == hash("helmet") then
			helmet = collectionfactory.create("#factory", go.get_position("head/head"), nil, {}, nil)
    	end
end

The actual spawning works just fine and the helmet gets placed right on top of the head.go .

But now I want to update the helmets position so that it sticks to the players head, which is moving.
So I tried the following in a script for the helmet object:

function update(self, dt) 
	go.set_position(go.get_position("head/head"))
end

But the compiler doesn’t seem to get the path of the head object.

Ive read the factories Documentaion but I still doesn’t know where the factory is spawning my helmet collection and how to get the position of the head object?

Thanks in advance!
Luca

If you have a script inside a GO then you can get that object simply by omitting the id/url parameter

Example from the manual:
local id = go.get_id() – no path, defaults to the instance of the calling script

For future reference though: If you are spawning several objects from a factory and want to keep track of them, store them into a table upon creation.

2 Likes

For your particular use case you might want to consider just childing the helmet to the character head, then it will stick automatically.

2 Likes

Childing the helmet sounds like a much better approach than setting it’s position each frame.

1 Like

okay makes sense! But is there a way to spawn the content of a collection as a child of an already existing object?

for example I created a new object factory inside my head.go
But when it spawns a new helmet object it just throws it into my main collection and not at the location where the actual factory is.

You can set the parent with a message “set_parent”. See http://www.defold.com/ref/go/#set_parent

3 Likes

thank you sir! :smile:

I should browse through the documentation more frequently!

1 Like

It is really worth reading from start to finish a few times. The manuals and the refs! If you have the time read all of the changelogs too as they detail some things not current int he docs. http://www.defold.com/release-notes/

3 Likes

Thanks again for your help!

I just have one last question. How important is it to prevent sending messages every frame?
I’m just a bit worried about performance. For example I will have some objects that need the current “state” of the player (e.g. the direction he is facing, if he is running or in the idle state, etc.).

My plan was to send a message like “get_player_state” that will return a table each frame.
Is this safe or is there a better way to treat this?

1 Like

It feels a bit awkward to post a message and reply to the sender with this kind of information every frame. It feels like you’re trying to replace a function call with message passing and doing so many times each frame, which really isn’t the intended use of messages. I see messages more like a way of communicating changes in the state of things.

I would maybe either store this player state in a Lua module and let other interested parties require() and read from this state directly. Or perhaps turn it around and let other game objects post a message to the player to subscribe for changes in player state and have the player post to registered listeners when a state changes (going from idle to running or when changing facing).

2 Likes