Setting another object property?

Hi,

I’m pretty new to defold and I’m creating a little asteroids clone to get the grasp of it. I come from a strong OOP background so, sometimes I struggle with some defold patterns but I have been able to overcome them until now :).

I have a starship that has weapons. The starship can can have several weapons active at the same time. The weapon creates the bullets and track and move them iterating through a table.

So, when a bullet hits a target, it must communicate to the weapon that it is done and that it can be removed/deleted from the scene. This, that sounds pretty simple is what I’m struggling with.

My question would be, how can I make the bullet know about the weapon it has created it to be able to message the weapon about collision or call the needed script function directly from the bullet?

I have tried to use property.set on the bullet from the weapon, but it needs an url to the object and I have an id (the id that is returned when the bullet is created with a factory).

Thanks in advance!

Hey, I’m brand new to Defold myself (~2 weeks), but I’m finding Defold patterns quite similar to way back when I used to use Unity, so maybe my suggestion could still be helpful…

I had a quick look at the Side scroller tutorial included with the editor. If the bullet is simply a projectile that flies in a straight line, maybe you could decouple it from the weapon?

Have the weapon create the bullet from the factory and then stop caring.

Have a script on the bullet to take care of making it fly. Here’s the contents of the bonus_star.script from the tutorial for an idea of what the script on the bullet could look like (probably you need a bit more complex vector math for an asteroid clone, but the principles should hold):

local speed = -240
local score = 5000

function update(self, dt)
	local p = go.get_position()
	p.x = p.x + speed * dt
	if p.x < -32 then
		go.delete()
	end
	go.set_position(p)
end

function on_message(self, message_id, message, sender)
	if message_id == hash("collision_response") then
		msg.post("main#gui", "add_score", {amount = score})
		go.delete()
	end
end

Then there’s no more need to communicate from bullet to weapon.

Hey!,

Yes, of course I could do that but… I want to know how I could do it that way. I’m really curious.

On the other hand your solution doesn’t scale well if you want a lot of bullets. This is because you are registering an update for every bullet. Imagine that in a bullet hell :slight_smile:

Thanks for trying to help. Really appreciate it.

1 Like

I found this thread may give you the answer
How to get url instead of id from factory.create() (SOLVED) - Questions - Defold game engine forum

I think you should store its weapon in somewhere (eg. a lua table), so that you can refer it to get all info you need

1 Like

Thanks for the link. It is not that part the one I was looking for but just a bit below that part is the answer and in the end I’m happy it goes in the lines I was thinking for. I mean, create a property in the bullet, set the property from the weapon and send a msg from the bullet. Thaks a lot!

For anyone searching for something like this I leave here the direct link to handle the parent/child creation process the question was about: Factory component manual

Cheers!

1 Like