Msg.post to the object we collided with (SOLVED)

Hello, in my game i have a character that can launch a fireball (called “power” in my game) at enemies. My problem is that i have tried many way to msg.post from my “power” object to my “enemie” object, but the “enemie” object never receive the message. I know the collision between the two do happend because my “power” object play his explosion animation and delete itself, but the enemie object didn’t react.

There’s my “power” object script :

go.property("degat", 20)

function init(self)
	self.t = 2 --temps de vie
	self.explode = 0 --exploser ou pas
	self.enter = 0 --contact
	self.velocity = vmath.vector3(0, 0, 0)
end

function update(self, dt)
	self.t = self.t - dt

	if self.t < -(8/12) then --animation explosion finis, suppression
		go.delete()
	elseif self.t <= 0 and self.explode == 0 then --distance max atteinte ou contact, animation explosion
		msg.post("#sprite", "play_animation", {id = hash("explosion4")})
		self.explode = 1
	elseif self.t < 2-(8/12) and self.explode == 0 then --animation de charge finis, boule de feu lancer
		self.velocity = vmath.rotate(go.get_rotation(), vmath.vector3(0,-15,0))
		go.set_position(go.get_position() + self.velocity)
	end
end

function on_message(self, message_id, message, sender)
	if message_id == hash ("contact_point_response") then
		msg.post(message.other_id, "damage", {degat = self.degat})
		if self.enter == 0 then
			self.t = 0.05 --contact, declenche explosion prématuré
			self.enter = 1
		end
	end
end

and there’s my “enemie” object script (for the moment he don’t move or anything but that doesn’t change anything) :

go.property("niveau", 1)

function init(self)
	self.live = math.floor(150 * 1.309 ^ (self.niveau - 1)) --augmente vie en fonction du niveau (25 000 max au niveau 20)
	self.degat = math.floor(15 * 1.225 ^ (self.niveau - 1))  --augmente degat en fonction du niveau (710 max au niveau 20)
	self.speed = math.floor(300 * 1.016 ^ (self.niveau - 1))  --augmente vitesse en fonction du niveau (405 max au niveau 20)
end

function update(self, dt)
	if self.live <= 0 then
		go.delete()
	end
end

function on_message(self, message_id, message, sender)
	if message_id == hash("damage") then
		self.live = self.live - message.degat
	end
end

That’s strange. Have you tried printing when the message is posted? Are you sure it’s sent? Have you tried setting breakpoints in the enemy script’s on_message function? Do you receive any messages at all?

I tried to go.delete() the “enemie” object directly when he receive the message but it didn’t happend, so i assume the message was never send. But it’s strange because the collision do happen so it’s not a Mask/Group issue. But what can be the reason for my message not to be send, because i don’t see anything wrong ?

Ok, so a few things to try/check:

  1. Did you add the script to the enemy?
  2. Did you add the script to the player?
  3. Do you see any errors in the console? Specifically around sending of messages.
  4. What if you add a print(message_id) just at the start of on_message in both scripts? What do you see in the console?
  5. What if you add a print(“foobar”) just before the line where you do msg.post()? Do you see “foobar” in the console?
1 Like

I re-checked all this and the problem was in my “power” object the script thas was afiliated with was “P.power1” instead of “M.power1” (P.power1 is for a different world in my game where the “power” doesn’t act totaly like in the other) Thanks for the help, and sorry for the time i made you lose because of my inatention.

99% of my problems come from something I’ve written incorrectly. It’s a pain in the ass, isn’t it? But it’s important to learn (for me anyway) that the ENGINE IS NEVER WRONG*

*actually it sometimes is, but hardly ever

2 Likes