why can´t i update a variable in a function?

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

my problem is that I try to make a score where every time you eat an enemy a point is added to the punctuation but when I add it the score value is not updated and it remains with the one that defined the variable

Show your code. No one can tell you what you’re doing wrong if they can’t see it.

kidscancode | 2019-08-15 06:40

Hi,
Showing your code will help people figure out what may be wrong with it.

By just reading your problem, it might be the case that your score variable is updating, but your score display ( a label I assume?) is not updating. You could test this by printing your score to the console.
I had a similar problem where elements of my UI were not updating when variables were, so you need to make sure you call an update for the UI when the score needs updating.

Something like (from my phone, so psuedo);

var score = 0
var score_display = get_node("score_label")

func _ready():
score_display.text = str(score)

func add_points(points):
var new_score
new_score = score + points 
update_score(new_score)

func update_score(score_new):
score = score_new
score_display.text = str(score)

But this is just a guess based on my experience, like kidscancode said, showing your code will help a lot.

Hope this helps.

DamonR | 2019-08-15 08:09

Wouldn’t score += points be suffice?

Magso | 2019-08-15 17:55

Hi Magso,
Yes you could write it that way instead. You can also merge the points and score update functions, I sometimes write things a different way to separate where different things are happening, I find it’s easier to follow code that way, but it isn’t always the fastest option.

DamonR | 2019-08-15 22:42