2
\$\begingroup\$

I am very new to Godot and coding in general, so I apologize in advance for any simple mistakes. I am trying to have my text display the variable ammodisplay on my object Marine. When I launch the game, the text sets to 7 (the correct value) but as I play the game and the variable changes, the text does not update with it. Any insight as to how I can fix this? Thanks in advance!

extends Label

var NODE = load("Marine.tscn")
var ammo = NODE.instance()
var ammod = ammo.ammodisplay


func _process(delta):
    text = (str(ammod))
\$\endgroup\$
1
  • \$\begingroup\$ How are you displaying the text? \$\endgroup\$
    – Pikalek
    Jan 4, 2021 at 22:41

3 Answers 3

1
\$\begingroup\$

Don't copy the value

In the code you are taking a copy of the value ammo and store it in the variable ammod:

extends Label

var NODE = load("Marine.tscn")
var ammo = NODE.instance()
var ammod = ammo.ammodisplay

Then you display the copy, which does not get updated.

func _process(delta):
    text = (str(ammod))

Read the value again instead:

func _process(delta):
    text = (str(ammo.ammodisplay))

Getting the correct node

Besides, it is unclear from the example if you actually want to instantiate a new instance with this script, or you want to access one you have in the scene tree. For example, your declarations could look like this:

extends Label

onready var ammo = $Marine;

Which assumes you have a node Marine as child of the Label in the scene tree. The type of ammo will be that of the node Marine. The code above is roughly equivalent to (except the version below has worst IDE integration, and worse typing information):

extends Label

onready var ammo = get_node("Marine");

The type of ammo will be Node.

In case you are not familiar with onready, let me tell you that the code above is roughly equivalent to (except the version below has typing information lost):

extends Label

var ammo = null;

func _ready():
    ammo = get_node("Marine");

The type of ammo will be Variant.

It is important to get the node in ready or afterwards because initialization of the node happens before it is added to the scene tree (and thus can't get nodes from it).


Chances are that Marine should not be a child of the Label. For that situation I suggest to export a node path.

By exporting a node path you can specify in the editor when the node you want to access. For example:

extends Label

export(NodePath) var ammo_path;
onready var ammo = get_node(ammo_path);

Then in the inspector panel you will see a new property "Ammo Path" (Godot auto capitalizes the name) where you can specify where in the scene tree the node you want to access is.

\$\endgroup\$
0
\$\begingroup\$

I'm new to GDscript myself, but I suspect that it should be self.text = ..., otherwise you just create a local variable that is not used.

\$\endgroup\$
2
  • \$\begingroup\$ Those solutions did not work for me unfortunately, but I did find a workaround! Thank you for the assistance. \$\endgroup\$
    – Aaron
    Dec 27, 2020 at 3:00
  • \$\begingroup\$ And what was that workaround? In case someone runs into the same problem and finds this thread. :) \$\endgroup\$
    – bytepool
    Dec 27, 2020 at 9:33
0
\$\begingroup\$

It seems that you retrieve an new instance of marine, rather than accessing the one you would like to change the value on.

Godot uses scene trees and the Marine.tscn should be a child of a another scene.

For example let's say you have a world scene with a textlabel called Speed and you want to update the text.

Godot Scene viewer

Then you would access the speed textlabel in your world scene via getNode()

get_node("Speed").text = "Speed: %s" % getSpeed()
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .