i’m working on a simple game in which the player needs to dodge falling objects while collecting others.
i created a factory that handles spawning multiple game objects based on drop_rate etc’
now i’m not sure how to approach collision response with the player (player is at the bottom of the screen, moves left or right)
tried to assign “type” property to spawned game objects but unsuccessfully (which will then be checked upon collision with the player to determine effect; might also be unnecessary..)
would love someone to share with me/us how he would approach this seemingly basic issue.
is it even possible to set & get properties from CHILD objects? setting property in create.factory() does not work for me.
i can solve by messaging back and forth using stored id but it feels very messy. any ideas?
Yes, you can set type of objects after spawning them. You can do it with many different ways. What I usually do is to save the factory creation on an id variable and send a message to it. After the message is received by the created object, I set its type using the variables sent with the message. Here is how I did something similar on my Defold tutorial
(you can find the project file and video link here: Make A Roguelike Game With Defold by Asatte Games)
--save the created id to a variable
local enemy_id = factory.create("#factory_address_here", spawn_pos, nil, {}, 1)
-- Send variables (like type in this example) as a message to the created object
msg.post(enemy_id, "set_type", { type = "Ghost"})
Then, on the created object’s on_message function take the necessary action:
function on_message(self, message_id, message, sender)
-- When collided with enemy, you can check if you've hit the player and has the correct type to damage him
if message_id == hash("trigger_response") and self.type == "Bat" then
if message.group == hash("player") then
msg.post(message.other_id, "change_hp", { value = -self.attack })
go.delete()
end
--When received the set_type message we sent after spawning the object, we set our type to the type sen with the message
elseif message_id == hash("set_type") then
self.type = message.type
end
Thanks a lot for your detailed answer. This is what I meant by “back and forth messaging” I was trying to avoid.
The solution I was looking for is as follows:
have a lua table which holds all spawns data (spawn_rate, spawn_cooldown, actions/messages etc’)
pass data as props when factory-creating new spawn
factory.create(factory_url, pos, nil, props, 1)
when spawn collides with player get url >> props, then do ‘actions/message’ in props (which I haven’t implemented yet but should work). *What’s important is that instanced properties can be retrieved this way
if message_id == hash("collision_response") then
local spawn_url = msg.url(nil, message.other_id, "drops")
local type = go.get(spawn_url, "type")
print(type)
end