ChatGPT gave up and told me to come here (solved)

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

What are the print outputs?

I’m wondering if each object has it’s own copy of processed_collisions. If you print (not pprint) processed_collisions, is the value (something like table: 0x0219ba3f3930) the same or different?

Well I may need to go back and try everything ChatGPT had me do again. When it had me make a collisionhandler script and put it on a GO so it can track all collisions.

Well…my dumbself didn’t remove my previous script from the other objects. Which it appears I was using two scripts at the same time. So let me test out some stuff and come back with an answer (hopefully).|

BTW: print(processed_collisions) returns nil. Maybe I put it in the wrong place?

Well, this kind of works, but not really. By setting the flag it does create only one object, but then I have no way to reset the flag back to false so that the next collision that is a match fires. So this will work one time (the first time) then nothing works after that. If I set the collision_processed = false after I spawn the new GO…it continues to make two objects.

local collision_processed = false -- Flag to track if collision is processed

function on_message(self, message_id, message, sender)
	if message_id == hash("contact_point_response") and not collision_processed then
		if message.own_group == message.other_group then
			collision_processed = true -- Set the flag to prevent further processing
			deleteVeggie(message.other_id)
			deleteVeggie(sender)
			pprint(message)
			spawnNewVeggie(message.own_group, message.position)	
		end
	end
end

function deleteVeggie(obj_ID)
	go.delete(obj_ID)
end

function spawnNewVeggie(group, pos)
	--local sGroup = tostring(group)
	print(group)
	if group == hash("radish") then
		factory.create("main:/factories#carrotfactory", pos)
	elseif group == hash("redOnion") then
		factory.create("main:/factories#carrotfactory", pos)
	elseif group == hash("onion") then
		factory.create("main:/factories#carrotfactory", pos)
	end	
end

What if you set the flag to false after you’re blocked from spawning an object? :slight_smile:

Like this?
If so this doesn’t work. it starts making two objects again.

function spawnNewVeggie(group, pos)
	if group == hash("radish") then
		factory.create("main:/factories#carrotfactory", pos)
	elseif group == hash("redOnion") then
		factory.create("main:/factories#carrotfactory", pos)
	elseif group == hash("onion") then
		factory.create("main:/factories#carrotfactory", pos)
	end	
	collision_processed = false -- Reset the flag to allow processing of the next collision
end
local wait_time = 1-- Time to wait in seconds

function init(self)
	-- Start the timer when the game starts
	timer.delay(wait_time, false, function()
		-- This function will be called after the specified wait_time
		collision_processed = false
		print("Variable changed to:", collision_processed)
	end)
end

Well this is odd. This “fixes” it by delaying setting the variable back to false. I don’t like this way as I think it may cause issues later, but for now it’s good enough (I hope).

No, I meant like this:

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
		else
			processed_collisions[collision_id] = false --the duplicate has been prevented, flag can be removed
		end
	end
end

Your original version has:

This is true for both. I think it will work if you add the key reversed:

processed_collisions[message.other_group.."-"..message.own_group] = true