#Question about updating variables
1 messages · Page 1 of 1 (latest)
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.
What is increasing energy?
How does your game works ?
Need more information to figure out a fitting solution
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.
so once the energy reaches ten it should always add one to amount ?
yes
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
yes
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
we could also add adition check if variable doesnt equal 0 so we are not doing amount + 0 every frame
see the problem is once i reach 10 it just keeps adding 1 every frame. I need to just add 1 once, not every frame
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.
so we add amount every frame untill we reach ten, and then you want add one to amount and stop ?
i want to add 1 to amount and stop. Thats it
i guess a timer ?
func _on_timer_timeout() -> void:
if energy >= 10:
amount += 1
else:
amount += 1
%Timer.stop()
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
dont think this is what he wanted
Then i misunderstood 🤔
its confusing me too but i THINK it should add amount untill 10 is reached and then one extra is added then it quits
I thought it should just add 1 to amount after you reach 10 and when energy increases nothing happens to amount
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
we´ll see 😅 but we gonna find a solution im sure!
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.