The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

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.

in Engine by (381 points)
edited by

2 Answers

+2 votes
Best answer

Looks like a typo to me.

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

by (144 points)
selected by

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

by (131 points)
edited by

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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.