Hi folks! Newbie and his question here
I have table with IDs of instances created by factory. Instances will eventually delete themself, so their ID in table will be out-of-date. I’d like to keep table valid because from time to time I have to send msg to all instances.
When instance deletes itself, it sends message “done”. So then I could delete ID from table. But in table I store game objects ID gained from factory and sender ID is ID of script attached to GO…
So, how can I get ID of game object from ID of script?
Here it’s reduced version of script written be me:
function init(self)
self.instances = {}
end
function final(self)
go.delete_all(self.instances)
end
local function create_instance(self, message, sender)
local props = message
props.superior = sender
local id = factory.create("#instance-factory", nil, nil, props)
msg.post(id, "run") -- instance will do its stuff and eventually send msg "done" to superior and this GO
table.insert(self.instances, id)
end
local function delete_instance(self, sender)
-- I have to update self.instances
local i
for index, id in pairs(self.instances) do
print(id, sender) -- I store "/instanceX" but sender is "main:/instanceX#logic" :<
if id == sender then -- this will never be true
i = index
break
end
end
table.remove(self.instances, i) -- for now, nothing is removed :<
end
function on_message(self, message_id, message, sender)
if message_id == hash("create-instance") then
create_instance(self, message, sender)
elseif message_id == hash("done") then
delete_instance(self, sender)
elseif message_id == hash("do-sth-fancy-with-all-instances") then
-- ... - here I would like to send msg to all instances so all ID in self.instances must be valid
end
end
PS
When I was writing this question I came up with some ‘hack’ - script can send its GO ID in message “done”. But is there better solution?
Thanks in advance