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?
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.
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/
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?
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).