How to find the receiver of a message

I am implementing combat into my game and whenever the player attacks every enemy is taking damage because the message is sent to all the enemies, how to you find the object that receives a post?

How do you detect that an enemy is hit?

if message_id == hash("trigger_response") and sender.fragment == hash("AttackBox_Player") then
	if message.enter then
		if message.group == group_enemy then
			handle_enemy_damage(self, message.normal, message.distance)

		end
	end

end

Is this code snippet from a script attached to every enemy?

yes

Please check that the funcion should be:

function on_message(self, message_id, message, sender)

it is just didnt send it in the previous message

I think this is the issue and that i have to identify which one have been hit

“Is this code snippet from a script attached to every enemy?”

I don’t understand why every enemy is affected… and also I don’t understand why you check if message.group is group_enemy… that code is running in a script attached to an enemy…

this is the code from the player, sorry for the confusion

I would say that there is somethin wrong in your enemy collection / game objects…

if message_id == hash("trigger_response") and sender.fragment == hash("AttackBox_Player") then
	handle_damage_of_enemy()
end

this is the enemy code

Aaaaaaah Then now it is clear!

You should use message.other_id and pass it to handle_enemy_damage, please take a look here : Collision messages in Defold

Or, better in my opinion, that code should be in the enemy script. The damage box of the enemy should collide only with player_hit_box and when an enemy gets a trigger_response then it is hit.

I hope this makes sense.

Ciao!

1 Like

thank you i am going to look over this now