Help with identifying GO in a collision

Ok, my game was nearly ready, but when I added all the objects I want to track in a collision I learned that you can’t have more than 16 groups for colliders. Unfortunately, I only needed about 5 more, but it is what it is.

I can’t figure out how to properly set this up. At first I saw a post about saying check the URL of the objects by naming each collision group something unique. The problem is only the sender provides the URL. The other is just something like /Instance1 since it was made in a factory. By far, this would be the preferred solution for me.

Then I tried setting a custom property. But then I think I need to do more messaging and even then I couldn’t figure it out on how to check the custom property of both GOs during a collision.

So now I read where I need to do a LUA module and setup my various types there. And with that I have no idea what to do. Is there an example, somewhere? I know I’m not the only one that needs to track two objects that collide and they use the same group name, but you still need to figure out what type of GO it is.

Would something like this work for what I’m trying to do?
rgrams/defold_entity_manager: A simple Lua module for Defold to keep track of existing entities of various types. (github.com)

To check a custom property of the game object that just collided with you, you need to use msg.url to construct a URL to the script component, and the go.get to get the property.

For example, let’s say the script that has the custom property on your enemy object has the Id “script”, and it defines a custom property like go.property("enemy_type", 1)

Then you can set that to a different number for each enemy, and do something like this in your collision checking code:

function on_message(self, message_id, message, sender)
	if message.enter then
		local enemy_url = msg.url(nil, message.other_id, "script")
		local enemy_type = go.get(enemy_url, "enemy_type")
		if enemy_type == 1 then
			print(" => do the thing for enemy type 1")
		elseif enemy_type == 2 then
			print(" => do the thing for enemy type 2")
		end
	end
end
3 Likes

Thanks. But I’m still a little confused. How does this work for testing on collision_response or contact_point_response?

And the part that says “script” is that generic for type script? Or is it supposed to be the name of the script?

I tried this and it says “’/instance1#collisionhandler’ does not have any property called ‘veggie_type’”

function on_message(self, message_id, message, sender)
	if message_id == hash("contact_point_response") and not collision_processed then
		local veggie_url = msg.url(nil, message.other_id, "script")
		local veggie_type = go.get(enemy_url, "veggie_type")
		print(veggie_type)
	end
end

Is the issue that I’m using a collisionhandler script on the main.collection to respond to collisions? Do I need to detect collisions in a script and attach that script to all GOs?

Well now it doesn’t matter because I redcuced the # of objects I need colliders on so I’m back under the limit of 16.

But I will want to figure this out later as I really want to have a great grasp of defold best practices.

It’s the ID of the script component that is attached to the “enemy” game object you want the player to collide with. So you would set it up like this, for example, and set the ID of the script to “script”:
image

In the above, the “Enemy Type” is a script property, defined in the enemy.script via go.property("enemy_type", 1)

2 Likes

Also, when creating objects via a factory, you can set their script properties dynamically as documented here: Script component properties

2 Likes

Thank you very much.