So I have a factory that creates a game object with script and sprite. Is there anyway to message the script as a whole instead of just the first instance created, if so how would I get the link/URL. When I do print(msg.url()) it gives . The script is in the bullets game object whilst the factory is in main.collection. I basically just want to know how to msg.post to all instances instead of just a specified single one.
You can’t. If you create bullets you need to put them into a list and then send a message to each of them:
local function send_to_bullets(self, message_id, message)
for i=1,#self.bullets do
local id = self.bullets[i]
msg.post(id, message_id, message)
end
end
function init(self)
self.bullets = {}
for i=1,100 do
self.bullets[i] = factory.create("#bulletfactory")
end
send_to_bullets(self, "increase_speed", { speed = 200 })
end
1 Like
Dispatcher might be an out-of-the-box solution for you to message all the interested objects without knowing their urls directly If the factored object has script in init() you would subscribe to a message and then use dispatcher to dispatch this message to all objects subscribed to this message.
But while deciding on this - consider if you even need a script for each factored object - if there will be a lot of bullets it might be a bottleneck regarding performance at some point. At this point solution by Bjorn should be better
1 Like