0 votes

Hi everyone! I desperately need your help. I have three scenes: a player, a coin, and a Main, which contains both. 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! Anybody could help me, please? :-(

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

could you share us the code?

What code? Currently, the only code I've added is the one of the answer below.

2 Answers

+1 vote
Best answer

coin.gd

onready var score = $"../CanvasLayer/Score"

func _on_body_entered(body): 
    if body == player:
        score.add_score(1)

score.gd

var score = 0

func add_score(val):
    score += 1
    text = score
answered Dec 8, 2018 by shield (200 points)
selected Dec 8, 2018 by Rob1980

Thank you! ;-)

+1 vote

Assuming you have a scene looking something like this?
- Main
-- Label
-- Player
-- Coin
--- Area2D
you could do a score like so

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
answered Dec 3, 2018 by Squatnet (200 points)

It doesn’t work! :_(

My scene is something like this:

  • Main

-- Player

-- Coin (Area2D)

-- CanvasLayer

--- 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?

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.