0 votes

Hi everyone! I desperately need your help. I have a scene like this:

  • Main

-- Player

-- Coin (Area2D)

-- CanvasLayer

--- Score

My problem is to add a label that scores the points when the player collects the coin. I tried in every way, looking also in this section, but there is nothing to do! I just can’t start the score!
In a previous answer, someone told me to add this script:

var score = 0 #hold the score
func addScore(val): # function to add the score
score += val # add val to score
$Label.set_text(score) # Set the text of label to score

func onArea2Dareaentered(): # when something enters area2D
addScore(1) # add 1 to score

I tried to insert the function in the Score script, and addScore () first in the coin script, then in the Main script, but nothing happens. In which scripts should I enter the code?

Anybody could help me, please? :-(

asked Dec 6, 2018 in Engine by Rob1980 (70 points)

Hi,
First, could you format your code like this?

var score = 0 #hold the score
func addScore(val): # function to add the score
    score += val # add val to score
    $Label.set_text(score) # Set the text of label to score

func onArea2Dareaentered(): # when something enters area2D
    addScore(1) # add 1 to score

For this formating, you should leave a blank line before code and indent it with four spaces. All lines must be at least indented one time this way.

Where is the label placed in yout tree structure? Is also child of Main?

Hi! The label is child of CanvasLayer, which is child of Main. But in what script should I enter the code?

It really depends of the full project. Here is what i would do based on what i understood from your question.

Put on_Area2D_area_entered function in the coin. Put also a signal called addScore in the coin.

Put addScore function and score variable in main scene, and connect coin addScore signal with main's addScore function.

In addScore funtion, refer to $CanvasLayer/Label, as Label is child of CanvasLayer.

Perhaps i could help more if you share the project.

Hi! I can’t connect the Coin signal to the Main functions, because it doesn’t allow me to connect to nodes of higher hierarchy.
About the project, it’s not so complicated. The hierarchy of nodes is the one I mentioned above, and the scripts are practically empty.

Hi,
Yo can connect coin signal to main function. Have you tried the Your First Game tutorial? There, it connects the player node signal "hit" to main scene function "game_over". Have you already done that? I think is the best place to start.

If coin is child of main, just click it and in the Node tab, find for the recenctly created signal and click connect.

About the projec, it is more than scripts ans tree structure. I cannot se connection or properties with that.

IT WORKS! Thank you so much, my friend! You saved my life! :-D

You are welcome! Glad to be helpful. Ill post the comment as answer so other can see its resolved. Would you select it as correct answer please?

Sure, bro! ;-)

1 Answer

+1 vote
Best answer

It really depends of the full project. Here is what i would do based on what i understood from your question.

Put on_Area2D_area_entered function in the coin. Put also a signal called addScore in the coin.

Put addScore function and score variable in main scene, and connect coin addScore signal with main's addScore function.

In addScore funtion, refer to $CanvasLayer/Label, as Label is child of CanvasLayer.

Perhaps i could help more if you share the project

answered Dec 7, 2018 by p7f (2,666 points)
selected Dec 7, 2018 by Rob1980
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 How to use this Q&A? before posting your first questions.