I can't start the score! Help me, please! :_(

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

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 _on_Area2D_area_entered(): # 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? :frowning:

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?

p7f | 2018-12-06 17:50

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

Rob1980 | 2018-12-06 20:07

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.

p7f | 2018-12-06 20:46

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.

Rob1980 | 2018-12-06 22:20

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.

p7f | 2018-12-07 10:33

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

Rob1980 | 2018-12-07 19:05

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?

p7f | 2018-12-07 19:18

Sure, bro! :wink:

Rob1980 | 2018-12-07 19:41

:bust_in_silhouette: Reply From: p7f

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