Getting Invalid Operand 'Nil' in custom function which retrieves autoload variables, why?

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

NODE A “”: #Autoload script

onready var Health = 100 #Start at max for test
onready var HealthMax = 100


    if STAT > STATMAX:STAT = STATMAX #Limit Stat max
    elif STAT < 0:STAT = 0 #Limit Stat min

    emit_signal("UPDATE_STATUS")#for labels and progres bars.
    return STAT 


func _on_GlobalDecay_timeout():
    var GPS = GlobalPlayerStatus
    DecayHealth = 33
    GPS.Health = GPS.STAT_CHANGE(GPS.Health,GPS.HealthMax,self.DecayHealth)

Debugger returns error: “Invalid operands ‘Nil’ and ‘int’ in operator ‘+’.” On the line STAT += AMOUNT.

Why? I don’t understand… I only found that the ‘Nil’ operand is GPS.Health, while the others(STATMAX and AMOUNT) it could retrieve the value just fine. Transporting the func STAT_CHANGE to the node B also resulted in the same error.

:bust_in_silhouette: Reply From: Klagsam

Looks like a typo to me.

then STAT += AMOUNT with the E missing

Aham, I missed that re-writing the question, there are not any typos in my code already checked, was actually the first thing I thought. Strangely enough, if I do:

GPS.Health = GPS.Health - DecayHealth

It works, but I don’t get the custom func limiting effects which I want.

The_Black_Chess_King | 2019-12-08 19:57

Heh, would you look at that, I don’t know how I have produced that error to begin with, so I am giving you the best answer because I believe to be a typo that I overlooked, I re-writed the func to almost the same thing:

	elif STAT < 0: STAT = 0
	return STAT

Worked now! Well, I really couldn’t reproduce the error I was getting, even on my backup versions of the project I am working in, all in all, very weird.

The_Black_Chess_King | 2019-12-09 02:47

:bust_in_silhouette: Reply From: nwgdusr999

What does your GPS object look like? “Invalid operands ‘Nil’ and ‘int’ in operator ‘+’.” seems to indicate that either the GPS.Health value or DecayHealth that you pass is null.

Could it be your that your DecayHealth value is in your function, but you refer to it as “self.DecayHealth”? Do you really need the “self.”? Are both the GPS.Health & DecayHealth set as ‘int’ everywhere in your code?

Btw, your naming is way way off and it’ll surely come back to haunt you later on, read this: GDScript style guide — Godot Engine (3.1) documentation in English

My GPS is just a script not a node, the self.DecayHealth is an old habit from another programming language I got, and so far it did not do any harm in my codes, and yes, I don’t need to call self on that, it belongs to the node calling the function.

GPS.Health is defined on the ready of the as = 100, so it automatically becomes an integer right? Which is autoloaded, so I don’t know how it becomes null?

And DecayHealth is also defined as variable onready of Node B same way, defined as = 0, my guess is that my function cannot retrieve the Health from the GlobalPlayerStatus for some reason.

My idea was simply to create a function to alter values from the GlobalPlayerStatus, I plan to use this to control Stamina, Hunger, Thirsty and Oxygen bars. Maybe even later I can create buffs and debuffs.

What else could I try?

The_Black_Chess_King | 2019-12-09 01:14