Send a message to all objects?

I’m trying to figure out a way to send a “kill” command to all objects in a scene. Is there a way to broadcast to all different objects?

If not, how else can I achieve this? I want to be able to kill everything in the level at a specific event.

The way we have done it is having a controller script that keeps track of all objects. You can either do

msg.post("#controller", "report_in")

in init for every object that should be tracked, and then in the controller:

function init(self)
  self.objects={}
end

function on_message(self, message_id, message, sender)
  if message_id==hash("report_in") then
    table.insert(self.objects, sender)
  end
end

To broadcast message you would just send a message to the controller that would iterate over the table. Another approach could be to create the objects in the controllers as well, thus removing the need for the individual objects to report in, but wether that is better depends on your use case

8 Likes