Counter and Signals

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

Hi guys.

I have this issue: I have created a spawner that creates infinities objects(KinematicBodies2D). When an object touches an Aread2D then the object disappears(i use signal from Aread2D to object).What i want, is when the object touches the Area2D then a GUI counter high on the screen to show me the score (counter=counter+1). i did a try with a signal from Area2D to a Label but a Label accept only strings.I searched on YouTube but there is no an appropriate video about this case. I show some videos that some guys that were using labels and that was weird…Is there an appropriate node that accepts integers?Any idea please?

Thank you.

try keep a var score in the area2D’s script and then just display it on the label

rustyStriker | 2019-05-18 13:05

First of all thank you very much about your reply.Basically i used the “text” Label’s property and then “set_text(str(score))”. So, i managed to show the score on the Label but now the problem is that every number is written on the previous one …

Nick888 | 2019-05-18 18:59

:bust_in_silhouette: Reply From: Jowan-Spooner

Hey Nick888.

A script like this should work:

extends Area2D
var counter = 0
onready var Label = get_node("Container/Label") # path to your label

func _on_area2d_body_entered(body): # connect the signal to this 
    if body.is_in_group("Enemy"): # you can also use another way
        counter += 1
        Label.text = str(counter)

if that’s what you’ve done I don’t know what you mean with ‘written on the previous one’. Hope it helps to make things clearer

Thank you so much about your reply!

The issue was that because of the Area2D was together with the Label in a scene 2, the scene1(main scene) was running all the time the scene 2.So,thats why the string-numbers was one over the other.
So, i applied a personal trick and i solved the problem:I created in main scene an Aread2D over the Aread2D of scene2 and i deleted the Label of scene2.Then, i created a personal signal and i send it from the Ared2D of scene1(main secene) to the new Label of scene1(main scene).

Thank you again for your concern and i am sorry about my english.Its not my native language.

Nick888 | 2019-05-20 07:43