Get ID of game object from ID of script (SOLVED)

Hi folks! Newbie and his question here :slight_smile:

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? :thinking:

Thanks in advance :wink:

I don’t think sending the game object id in the “done” message is a terrible solution.

You might want to take a look at the addressing documentation, specifically the URLs section. If I’ve got it right, the following should make your sender match an id in your table:

msg.url(sender).path
4 Likes

I think sender already is a url. You can also do this to get the id:

go.get_id(sender)
3 Likes

@Alex_8BitSkull Ah, I missed that part documentation :sweat_smile: Using msg.url(sender).path or sender.path as well did the trick!

Now delete_instance is:

local function delete_instance(self, sender)
    local i
    local id_to_remove = sender.path
    for index, id in pairs(self.instances) do
        if id == id_to_remove then 
            i = index
            break
        end
    end
    table.remove(self.instances, i)
end

Thanks guys a lot! :slight_smile:

3 Likes

@britzl go.get_id(sender) did not work for me, I’m afraid :frowning: I got error:
bad argument #1 to 'get_id' (string expected, got userdata)

Ah, my bad. go.get_id() only accepts strings. Using sender.path should be enough then.

Ok, thanks for clarification :slight_smile: Case is closed :wink: