How to get information about the body that entered the area?

I have a body that is included in an area, this body has a plusPoints variable, how can I get this variable in another scene?
its not working:

extends Node2D

var plusPoints = '123'
signal hit

func _on_sprite_hit_box_area_entered(area: Area2D) -> void:
	hit.emit()
func _on_score_hit_box_body_entered(body: Node2D) -> void:
	print(body.plusPoints)

please help me and don’t get too angry if the question seems stupid, I just started learning godot

Hi,

In the Godot doc, search for " Singletons (Autoload)", all of the answers are here.

Cheers

1 Like

It looks like I didn’t express it correctly, the plusPoints variable will change, or more precisely, I will create many scenes with this variable and all these scenes will come to the same territory. After they arrive, points will be added. But it’s also worth considering that they can all add a different number of points.

I stick to my answer, It would be easier for you to declare a Global variable you can access from every scene and script from anywhere in your project.

Read the “Singletons(Autoload)” page carefully. :wink:

1 Like

What is the error it gives you?

Keep in mind the script with plusPoints is not a Body, it extends Node2D so it cannot overlap the area by iteself. Also any StaticBody2D will overlap with the area, including the floor/walls which may be undesired and need a if statement.

You do not need a Singleton/Autoload/Global to interact with collided bodies.

1 Like

You’re 100% right and your answer is spot on. But the initial question was how to access a variable from outside of his script, hence my answer. Messing with globals will be useful down the road.

1 Like

Thank you! I finally figured it out, I knew about Singletons, but I didn’t think it could be used like that. Basically, I added the points update to _process

func _process(delta: float) -> void:
	$score.text = str(Data.points)

and updated the points variable in the Autoload script.

func _on_sprite_hit_box_area_entered(area: Area2D) -> void:
	hit.emit()
func _on_common_click_hit():
	Data.points += plusPoints
	queue_free()

Thanks again for your help!

1 Like

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