Hello, dear Defold community! I have a problem, again
I have ten different GO (call them “items”) with one item.script. Also, I have factories that spawn these GO.
Every “item” has a unique sprite and label but the same script. I need to compare spawned “items” with the "reference"
I tried to do it by id, I wrote some code into the item.script
function init(self)
item_id = go.get_id()
print(item_id .. ' spawned')
end
but every item that spawned from the factory has id like “[/instance9]” where “9” just a serial number.
I the factory spawns one item every 5 sec it will be “[/instance0]” every time
Can you help me and explain how I can make the setup that I want?
I have solved this problem by adding a unique group ID to each “item” object. And detect collisions with this code
if message.enter then
if message.other_group == hash('item_1') then
print('I caught the 1 ')
elseif message.other_group == hash('item_2') then
print('I caught the 2 ')
end
end
First, I’d recommend you read up on the local keyword from Lua, and it’s use in the Defold context.
The code item_id = go.get_id() is prone to be overwritten if there are multiple instances of that script.
Can I add some questions to this topic?
I’m trying to read the property “item_id” on the “item” from another collider
I use this code
if message_id == hash("trigger_response") then
if message.enter then
if message.other_group == hash('item') then
local item_property = go.get(message.other_id, 'item_id')
print(item_property)
-- msg.post('GUI#level_ui', 'increase_soup_score')
end
end
end
And I got an error
ERROR:SCRIPT: assets/scripts/cauldron.script:32: '/instance0' does not have any property called 'item_id'