NOOB -- How to display score var in label?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By koigx

I have a basic function that increases the score when the player’s kinematicbody2d enters a coin’s area2d using a signal. How do I display the score in a label?

This is basic 2d platformer with 3 scenes - each with its own GD script: level, player scene, and coin scene. Both the player and coin scenes are instanced within the level scene.

My own instinct is that setget should be the way to go, but I get lost. Could you please help?

My score script is within coin.gd:

`extends Area2D
   var score = 0 
func _on_coin_body_entered( body ):
if body.is_in_group("Player"):
	self.queue_free()
	score += 50
:bust_in_silhouette: Reply From: Footurist

get_node("../..") ← goes up to levels in the hirarchy
$Label ← accesses a child node called “Label”
getnode("../Label") ← goes up one level in the hirarchy and accesses a child from that level
called “Label”

So, do this: get_node(nodepath).set_text(score)

That said, a better way would be to connect the Area2D's signal to the Label node. To do that, click on your Area2D node once, change from the Inspector tab to the Node tab. Double click on the signal called "body_entered". A window pops up, where you can double click on a node in the hirarchy, which will create a function in that node, which will be called everytime a body enters the Area2D.

When on the script tab in godot, you can click “Search Help” and search the documentation for such things. Works like a charm.

Thank you!!!

koigx | 2018-02-09 23:56