Make "LABEL" show values and itam saving.

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

Can anyone help me finding the solution for the 2 questions below;
1 : how to make the label show the life value decreasing to 0
enter image description here
here is the code for Area 2d , i need to make the label show the values of life points
Here is the path of label
enter image description here
Here Life_2 is the label i need

2 : How to add the amount of coins collected in a level to the current amount of coins already present in the game ?

Can you help me to solve those two problems?
[Please add some videos or code for reference]

:bust_in_silhouette: Reply From: RenenerG

Hello THE HELIX,

first a few things I see reviewing your code.

  1. Your Label needs a reference to the Area2D in order to work properly with signals. The connection call in the _ready() function of your Label should be something like this:
    reference_to_area2D.connect("life_changed", self, "_on_life_changed")

You will allways emiting a signal from where you declare it. So your Area2D declares a signal and it want to send this signal to the Label. So the Area2D needs to be conntected to the Label. Think about it, or read the docs about Signals :-).

You can get the reference of a node with the get_node() method. For example in your scene: var life_2 = get_node("/root/???/Control/Life_2"). (“???”, because I cant identify the top node on your uploaded picture)

I would suggest you to look at the docs. There is a nice little intro for scripting a scene.

  1. Your Label needs the corresponding function you are trying to connected to: "_on_life_changed"
    put it in the script of the Label like you do with _on_body_entered() in the Area2D script. Otherwise, it will never be called.

1 : how to make the label show the life value decreasing to 0

You can access the Label’s property text and manipulate it.

A good way is to send the current life points as variable within your signal, like emit_signal("life_changed", life). Make sure your signal declaration looks like something like this signal life_changed(your_variable)

So, lets say you have a function _on_life_changed(your_variable) in your Label’s script. Then you can easily manipulate the text with set_text(str(your_variable)).
str() will convert your number into a string :slight_smile:


2 : How to add the amount of coins collected in a level to the current amount of coins already present in the game ?

Sorry, i do not understand what you are trying to archieve :frowning:


I hope I could help you a little bit :slight_smile: