score in canvas

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

I have Node2D (main scene), and a child node Canvas layer. In cavas layer I have another Node2D named HUD with a label to storage the score. I have an object Area2D named “coin”.

My doubt: how to incremente the score when my player colide with the coin
I created on body entered. But how to associate these codes?

:bust_in_silhouette: Reply From: Bishop

If main scene is game level then use Node instead of Node2D.
Why do you want to have HUD as a child of Canvas layer, is there any reason?
…I would use Control node and label node as his child.
…then in Node( main scene) create script then get label in script by

var score_count = $label

or in your case var score_count = $CanvasLayer/Control(use Control node!)/label
Then with Area2D coin you created a signal body_entered…connect this signal
function _on_Area2D_body_entered() will be created in main_scene(root node) script.
script for Node (

extends Node

onready var score_count  = $label
var coin = int(0)
var coin_hit = bool (false)

func _ready():
      score_count.set("text", str(coin))

func _process(delta):
      if coin_hit == true:
          coin = +1
          score_count.set("text", str(coin))

func _on_Area2D_body_enter():
      coin_hit = true

…the solutions is more…download the documentation in pdf and search there

Bishop, thanks for your help

an error was occured

“indentifier not found: coin_hit”

on body entered that I’m using is player

and the variable coin_hit was declared in game (main scene)

CB | 2019-01-30 22:15

Well,…so,it might be better to use and create two areas one in the player node and of course the coin area and using groups…in coin area go to groups and
name it for example “coin”…and in the player node and his area connect
signal in player script…signal - area_entered(Area2D area)…then you have a function
_on_player_area_area_entered(area): in the player script.
and because you have now group “coin” we can get this group when player area entered coin area.
in script

func _on_player_area_area_entered(area):
       if area.is_in_group("coin"):
            coin += 1
            score_count.set("text", str(coin))

Bishop | 2019-01-31 00:47

I created a test scene
player - KinematicBody 2D
coin - Area2D …group “coin”
CanvasLayer/hud (Control node)/coin_count(Label mode)
This is my script

extends KinematicBody2D

var vel_x = Vector2(1,0)
var speed = int(50)

var coin = int(0)
onready var coin_count = $"../CanvasLayer/hud/coin_count"
func _ready():
       coin_count.set("text", str(coin))
func _physics_process(delta):
       if Input.is_action_pressed("ui_right"):
            translate(vel_x * speed * delta)
       if Input.is_action_pressed ("ui_left"):
            translate (-vel_x * speed * delta)
func _on_player_area_area_entered(area):
       if area.is_in_group("coin"):
             coin += 1
             coin_count.set("text", str(coin))

Bishop | 2019-01-31 01:57