Msg.post to an object created by a factory

Hi, my game is a 2D game in wich yo have to sort 3 different items who spawn in a random order. The things that i want to do is to post a message to the factory-created object when he collide with a trigger to tell him where to go.

I think you should be able to message a factory created object like this:

local new_go = factory.create(...)
msg.post(new_go, "hello_from_the_other_side")

But regarding what you actually want to do; "post a message to the factory-created object when he collide with a trigger to tell him where to go"

Wouldn’t it make sense to have a script attached to the newly created object that listens for trigger_response in it’s on_message? Given that the game object has a collision object component that can be triggered. :slight_smile:

2 Likes

This is the look of my game, the factory created object will spawn and come on the travolator and when he hit the little square on the middle he gonna be moved on the left/right/mid depending on where the player have touched the screen

the script of my factory created obejct is :

function update(self, dt)
	self.pos = go.get_position()
	
	if self.pos.y < -105 then
		go.delete()
	end
	
	if self.direction == 1 then
		self.pos.x = self.pos.x + self.speed
	elseif self.direction == 2 then
		self.pos.x = self.pos.x - self.speed
	end
	
	self.pos.y = self.pos.y - self.speed
	go.set_position(self.pos)
end

function on_message(self, message_id, message, sender)
	if message.group == hash("poubelle") then
		go.delete()
		msg.post("/HUD#gui", "lose")
	elseif message.group == hash("gauche") then
		go.delete()
		msg.post("/HUD#gui", "lose")
	elseif message.group == hash("droite") then
		go.delete()
		msg.post("/HUD#gui", "score+")
	elseif message.group == hash("trieur") then
		if self.dir == 1 then
			self.direction = 1
		elseif self.dir == 2 then
			self.direction = 2
		elseif self.dir == 0 then
			self.speed = self.speed * 2
		end
	elseif message_id == hash("droite") then
		self.dir = 1
	elseif message_id == hash("gauche") then
		self.dir = 2
	elseif message_id == hash ("mid") then
		self.dir = 0
	end
end

and i have a script in my gui.script wich gonna send a message to my factory created object to tell if it’s right/left/mid

i don’t know if my problem is more clear now ? by the way sorry for my English if i have done some mystake

It’s perhaps not necessary to store the id of the spawned object. Instead, you can create a script in the game object containing the trigger collision object and have it send messages to whomever enters the trigger:

function on_message(self, message_id, message, sender)
    if message_id == hash("trigger_response") and message.enter then
        msg.post(message.other_id, "you_hit_me")
    end
end
3 Likes