About classes and groups // Cannot find class in other script

Godot Version

4.3

Question

I already read the Docs on this topic, but cannot figure out how this works.

All i have is Player(Body2D) with a Child-Area2D (The Hitbox).
Screenshot 2024-09-20 164805

And now if i enter the killzone_scene (whitch is just a Area2D) i want to make sure, the player is the only one, who can trigger it.

My Code in my Player

extends CharacterBody2D

class_name player

My Code in Hitbox

extends Area2D

class_name bird_player

Both the Player and the Hitbox itself are in the (Global) Group “Bird Player”

So how do i make my script work. I think its maybe another syntax mistake

Code in my Killzone


func _on_area_entered(area: Area2D) -> void:
	print(area.get_class() + str(area.is_class("bird_player"))) 
    #print out - Area2Dfalse

	if area.get_class() == "bird_player":
		pass
	if area.is_class("bird_player") == true:
		print("THATS IT")


          #Rest of the function 
		if Gobal.player_can_die == true:
			Gobal.dying()
			set_deferred("monitoring", false)
			print("You Fall of the World")

So why is my area.get_class() not bird_player ??
Is Classes the right way to do this?
idk i think there must be a easy solution, but i don’t get it

is_class and get_class only retrieves base class name. Since it’s a string instead of a Type it’s difficult to implement.

Note: This method ignores class_name declarations in the object’s script.

Use keyword is

func _on_area_entered(area: Area2D) -> void:
    if area is bird_player:
        print("is bird")
1 Like

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