Table of Gameobjects Sprite Iterate (SOLVED)

Hi,

How would I iterate over my table and then change the sprite of each gameobject in my table?

local function glow_all_chips(self)
  for index, value in ipairs(self.chips) do
    msg.post("#sprite", "play_animation", {id = hash.ATLAS[4].sprite})
  end
end

function on_message(self, message_id, message, sender)
  if message_id == hash.MSG_CHIP_REPORT then
	table.insert(self.chips, sender)
  end
end

How does it post the message to each gameobject to change the sprite?

Sender will he the URL of the script that sent the message. You can use this URL to create a URL to the sprite:

local sprite_url = msg.url(sender.socket, sender.path, "sprite")
sprite.play_flipbook(sprite_url, "some_animation")
1 Like

Where would I place the code? In my gameobject script or my controller that receives the message? My controller script I think?

Like this:

if message_id == hash.MSG_CHIP_REPORT then
   table.insert(self.chips, sender)
   local sprite_url = msg.url(sender.socket, sender.path, "sprite")
   msg.post(sprite_url, "play_animation", {id = hash.ATLAS[4].sprite})
end

I applied the code as follows and it works: I hope I did it the correct way.

local function glow_all_chips(self)
   for index, value in ipairs(self.chips) do
      msg.post(value, "play_animation", {id = hash.ATLAS[4].sprite})
   end
end

function on_message(self, message_id, message, sender)
   if message_id == hash.MSG_CHIP_REPORT then
      local sprite_url = msg.url(sender.socket, sender.path, "sprite")
      table.insert(self.chips, sprite_url)

   elseif message_id == hash.MSG_GLOW_CHIPS then
      glow_all_chips(self)
  end
end