How to check the Classname (with a if function)

Godot Version

4.2

Question

Where is the mistake in my Code, it print only this:

PHASE 1
PARENT: Crystal :<Node#416343412968>
PHASE 1
PARENT: Crystal :<Node#416343412968>

I believe is a syntax mistake? Because it finds the Area and the Area is also a Crystal. But it doesn’t call the function

Code form my Hitbox

func _on_area_entered(area):
	print("PHASE 1")
	var parent = area.get_parent()
	print("PARENT: " + str(parent))
	if parent is Crystal :
		parent.hit(damage)
		print("FOUND")

And the Code in my Crystal

class_name Crystal 

func hit(damage:int):
	var tween_time:float = 0.4
	
	monitoring = false
	get_tree().create_tween().tween_property(self, "scale", Vector2(0,0), tween_time).set_trans(Tween.TRANS_BACK)
	await get_tree().create_tween().tween_property(self, "modulate", Color(1, 1, 1, 0), tween_time).finished
	call_deferred("queue_free")

And another Question do you know how to “short” this tween lines, for readability?

Put this before your “if”-statement and check if it prints true:

print("Is Crystal: ", parent is Crystal)

you can only extend the tween-lines for more readability, e.g.:

var scaling_tween = get_tree().create_tween()
scaling_tween.tween_property(self, "scale", Vector2(0,0), tween_time)
scaling_tween.set_trans(Tween.TRANS_BACK)

var modulate_tween = get_tree().create_tween()
modulate_tween.tween_property(self, "modulate", Color(1, 1, 1, 0), tween_time)
await modulate_tween .finished
1 Like

Thank you already, it prints out false. But i don’t know why. The Parent should in that class, maybe is because of my level tree ?

Screenshot 2024-08-13 181054

Please ignore my misspelling for Crystal haha

Where is this hitbox-script? in the player script?

kinda

Screenshot 2024-08-13 182108

Try this:

func _on_area_entered(area):
	print("PHASE 1")
	if area is Crystal :
		area.hit(damage)
		print("FOUND")

You dont need to get the parent because the area is already your crystal

The reason why it prints Crystal is, because the name of the Parent-Node is Crystal, but its just a normal Node

2 Likes

it was so easy haha thank you!!
i dont need to get any parent or child

func _on_area_entered(area):
	print("Area: " + str(area))
	if area is Crystal:
		area.hit(damage)
		print("AREA FOUND")
1 Like

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