[Godot] Is it correct to check the collision through the class type?

In the Shooting Enemies & Collision Layers lesson it is shown that to check if the rocket collide with an enemy the 2D Physics Layers are used, which it might be the best solution to actually check if a specific object collide with another one.

However during the challenge I thought that to check if an object collide with another one I could simply check if the area entered is from a specific class and if it is then executed a specific method.

This is the code I have used on_area_entered signal:

func _on_hit(area:Area2D):
	# Remove the laser
	self.queue_free()

	if area is Enemy:
		area.destroy()

My question is if it is also ok to check which object collide the rocket, or laser in my case, by simply doing a check of which class is the area object collided?

1 Like

definitely another way of doing it, and you can then be quite specific then on your checks.

im not 100% sure, but i read somewhere that using the masks and layers can lower the physics engine overheads as its only got to check the mask/layer combo rather than each and every object.

but checking for the class type can definitely help readability and error checks.

and you can define a class at the top of the script according to docs so we can check against that as well

class_name MyNode
extends Node

suppose this could work almost like an interface in other languages. might have to test that one further tho

1 Like

Thank you very much for answering my question.

im not 100% sure, but i read somewhere that using the masks and layers can lower the physics engine overheads as its only got to check the mask/layer combo rather than each and every object.

I didn’t know about this but if it helps then it’s good that in the course it is explained what it does.

and you can define a class at the top of the script according to docs so we can check against that as well

I saw in the documentation an example of specify the class type and this help a lot also making comparison between objects.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms