Please help I was stuck for 2 hrs.. Version 4.72
extends Button
func _pressed():
var Cookies = 0.5
var cookies = 0
cookies = Cookies + Cookies
print(cookies)
Cookies = Cookies + Cookies
cookies = Cookies + Cookies
print(cookies)
return
Please help I was stuck for 2 hrs.. Version 4.72
extends Button
func _pressed():
var Cookies = 0.5
var cookies = 0
cookies = Cookies + Cookies
print(cookies)
Cookies = Cookies + Cookies
cookies = Cookies + Cookies
print(cookies)
return
I think we need more information before we can help. When you say you can’t save your cookies, do you mean the value doesn’t save between presses? So while the first print would output 1.0, it only ever outputs 1.0 there, which I’m presuming is the issue, you want it to go to 2.0 on the second press?
If so, then the issue is that every time you press the button, you’re actually remaking the whole variable because it’s in a local scope (within a function, which means it can’t be accessed outside that function)
So the fix would be to put the variables you want to persist outside of the function right beneath the class name (the part that says extends Button) here’s the code (also changing variable names for better readability)
extends Button
var cookie_increment_value: float = 0.5
var current_cookie_amount: float = 0.0
func _pressed() -> void:
current_cookie_amount += cookie_increment_value
print(current_cookie_amount)
I kind of optomized your code too but it’s all very simple, I’ll walk you through what it all means. current_cookie_amount += cookie_increment_value is the same as writing out current_cookie_amount = current_cookie_amount + cookie_increment_value, difference being we are adding to the variable, not setting the variable to the variable plus the increment value
Then there’s the static typing I did which is just for clarity and efficiency’s sake, where if you break a rule the editor will give you a warning. So for func _pressed() -> void: just means there is no return value, and with var cookie_increment_value: float = 0.5 the : float = part just tells the game “Hey! this value is a floating point number and can take decimals”
All of that is basically the same as writing out the following code in a cleaner way (i kept changes minimal here only fixing what you wanted to better isolate why it wouldn’t work)
extends Button
var Cookies = 0.5
var cookies = 0
func _pressed():
cookies = cookies + Cookies
print(cookies)
You’re declaring your variable inside of the function, which makes it a local variable. Local variables don’t “remember” their values between runs:
func _ready():
do_thing()
do_thing()
do_thing()
func do_thing():
var counter = 0
counter += 1
print("Counter is ", counter)
Counter is 1
Counter is 1
Counter is 1
If you want the value to be “remembered”, you need to declare the value outside of any function:
func _ready():
do_thing()
do_thing()
do_thing()
var counter = 0 # !! Declared outside of the function !!
func do_thing():
counter += 1
print("Counter is ", counter)
Counter is 1
Counter is 2
Counter is 3
Thank you guys so much I really did learn a lot from this.