When two objects collide I delete both objects and I want to spawn a separate single object. My issue is it appears both objects are firing at the same time creating the new GO so that I get two instead of just one. Both objects are the same. Like if you drop two 50 cent pieces on to each other I want them both to disappear and create a single dollar bill.
I tried everything using ChatGPT for help from using a local variable to using a unique identifier. Nothing is working.
Here is the current code I tried:
local processed_collisions = {} -- Table to store processed collision pairs
function on_message(self, message_id, message, sender)
if message_id == hash("contact_point_response") then
-- Create a unique identifier for the collision pair
local collision_id = message.own_group .. "-" .. message.other_group
if not processed_collisions[collision_id] then
-- Check if the sender's group and the other object's group are the same
if message.own_group == message.other_group then
print("other id: ", message.other_id)
print("sender id: ", sender)
deleteVeggie(message.other_id)
deleteVeggie(sender)
print(message.own_group)
print(message.position)
spawnnewVeggie(message.own_group, message.position)
processed_collisions[collision_id] = true
end
end
end
end
function deleteVeggie(obj_ID)
go.delete(obj_ID)
end
function spawnnewVeggie(group, pos)
local sGroup = tostring(group)
if string.find(sGroup, "radish") then
factory.create("main:/factories#carrotfactory", pos)
elseif string.find(sGroup, "redOnion") then
factory.create("main:/factories#carrotfactory", pos)
elseif string.find(sGroup, "xnion") then
factory.create("main:/factories#carrotfactory", pos)
end
end