0 votes

NODE A "GlobalPlayerStatus.gd": #Autoload script

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

 func STAT_CHANGE(STAT, STATMAX, AMOUNT):
    STAT += AMOUNT

    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 

NODE B:

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.

asked Dec 8, 2019 in Engine by The_Black_Chess_King (281 points)
edited Dec 8, 2019 by The_Black_Chess_King

2 Answers

+1 vote
Best answer

Looks like a typo to me.

You have func STAT_CHANGE(STATE, STATMAX, AMOUNT):
then STAT += AMOUNT with the E missing

answered Dec 8, 2019 by Klagsam (140 points)
selected Dec 9, 2019 by The_Black_Chess_King

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.

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:

func EDIT_STAT(STAT,MAX,AMOUNT):
    STAT += AMOUNT

    if STAT > MAX: STAT = MAX
    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.

0 votes

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: https://docs.godotengine.org/en/3.1/getting_started/scripting/gdscript/gdscript_styleguide.html

answered Dec 8, 2019 by nwgdusr999 (117 points)
edited Dec 9, 2019 by nwgdusr999

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 GlobalPlayerStatus.gd 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?

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.