How to get the collision component name?

Hello, I recently found Defold, and feels good about it so far.

Currently I’m go with the War battle tutorial , try to let the tanks fire at player. To do so, like “chase” action, I add another collision component, like this:
image

“attack-range”, and “aware-range” is in group “tank”, checking mask “player”, while the Player.go is in group “player”, checking mask “tank”.

My problem is, in on_message method, I listen “collision_response”, but cannot tell which component is colliding, so is there a way to get this?

I could think about other solution, maybe I should simply check distance between player and tank, or maybe I should give “attack-range” a new group, “tank_attack” for example. But, as the group size is limited to 16, I may run out of it soon. or perhaps I should attach a new go on tank, and let this dummy go hold “attack-range” and a new script to listen to on_message?

Or, do I in a completely wrong way?

Your on_message function should have a fourth argument, “sender”. This will be the full URL of the component that sent the message. So you can do something like this: (untested code)

function init(self)
	self.attack_range_sensor = msg.url("#attack-range")
end

function on_message(self, mesage_id, message, sender)
	if message_id == hash("trigger_response") then
		if sender == self.attack_range_sensor then
			if message.enter then
				-- Target enters attack range: do stuff.
			else
				-- Target exits attack range: do other stuff.
			end
		end
	end
end
2 Likes

The collision_response should contain what you need. Can’t you use other_id, other_group and own_group?

Thank you! I should have check the API more carefully.

Maybe not, I set them in same group as I said:

Thanks for replay, btw, @ross.grams gave the solution.