#Question about updating variables

1 messages · Page 1 of 1 (latest)

noble knot
#

do you want to cap it at 11? in that case you could do:
if GlobalVar.energy >= 10 and GlobalVar.energy < 11:

#

and then the rest of your statement. that might not be formatted correctly since i’m
on my phone but something like that. but if you want it to just be slower or only happen sometimes you would probably just have to write a specific function and call it at certain times.

fathom trail
#

What is increasing energy?

#

How does your game works ?

#

Need more information to figure out a fitting solution

proud totem
# fathom trail What is increasing energy?

I have a idle clicker game. I'm making a script that adds 1 to the var amount when var energy exceeds ten. I doesn't matter weather energy goes back below ten or over twenty. Once var energy has exceeded 10 i wish to add one to that variable, even if it dips below 10 again. Only 1.

fathom trail
#

so once the energy reaches ten it should always add one to amount ?

proud totem
#

yes

fathom trail
#

so right now the way you have it, the code block in the if doesn't get executed when we are not equal or above 10

proud totem
#

yes

fathom trail
#

so the way i would go about it (someone smarter might have better idea)

but instead
var variable := 0

func _process(_delta):
  if globalvar.energy >= 10:
    variable = 1
  if variable != 0:
    globalvar.amount += variable
#

the if sets the amount we add to the global amount and it stay set once we are not equal or above 10

proud totem
#

ok

#

ill try that one sec

fathom trail
#

we could also add adition check if variable doesnt equal 0 so we are not doing amount + 0 every frame

proud totem
fathom trail
#

then i misunderstood

#

what do you mean by once ?

proud totem
# fathom trail what do you mean by once ?

basically amount gets tapped into by a couple other scripts. It being used and displayed in a label like this as this. Amount = globalvar.amount /10. All these differnt scripts, once reaching a certain number (10 in this script) shoudl add one to that avrible so you can see your amount out of ten. Tis script, once reaching 120 should add 1 to this counter leaving it at 1/10 but instead once surpassing ten it starts adding 1's infinitely. so it look something like 330/10 and keeps going up.

fathom trail
#

so we add amount every frame untill we reach ten, and then you want add one to amount and stop ?

proud totem
fathom trail
#

i guess a timer ?

#
func _on_timer_timeout() -> void:
    if energy >= 10:
        amount += 1
    else:
        amount += 1
        %Timer.stop()
vale vapor
#

Hm you could just use a bool to check if you reached that number.
And instead of polling for it you could use a setter and check each time when you changed the energy variable for the amount to update:

var is_energy_amount_reached:bool = false
var energy:int = 0:
    set(value):
        energy = value
        if energy >= 10 and not is_energy_amount_reached:
            amount += 1
            is_energy_amount_reached = true
var amount:int = 0

fathom trail
#

dont think this is what he wanted

vale vapor
#

Then i misunderstood 🤔

fathom trail
#

its confusing me too but i THINK it should add amount untill 10 is reached and then one extra is added then it quits

vale vapor
#

I thought it should just add 1 to amount after you reach 10 and when energy increases nothing happens to amount

fathom trail
#

maybe i mistunderstood idk anyome 😅

#

i guess iam confused how the game works but i think yours is the correct thing rereading all the messages

vale vapor
#

we´ll see 😅 but we gonna find a solution im sure!

proud totem
#

I have found a way around it. I used set_process(false) to stop the function after adding 1 so that it didn't keep adding 1. Thanks so much for you guys help, sorry I was a bit vague.