This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

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
in Engine by (59 points)

1 Answer

+1 vote

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.

by (846 points)
edited by

Thank you!!!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.