How do you disable/enable a button node depending on a variables value?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By arbitrarybarry

Hi, I am brand new to Godot and still obviously have much to learn about GDscript.

I am trying to make an idle clicker game, and I want to disable a button node if the player does not have enough money (grass in this case) to cover the cost of the optimize button.
So far the player can click on a “Get Grass” button which increases the grass value by 1 each click. The optimize button increases the amount of grass earned in a single click. I entered the following code:

extends Panel

export var grass = 0
export var optimize = 1
export onready var timer = get_node(“Timer”)
export var optimize_cost = 5

func _ready():

if grass < optimize_cost:
	get_node ("Optimize").disabled = true
elif grass >= optimize_cost:
	get_node ("Optimize").disabled = false
	UpdateUI()

But when I run the game the Optimize button stays enabled even though the player has less grass than optimize_cost.

Any help or even a recommendation of a different method would be much appreciated, Thanks!

whats updateui

supper_raptor | 2020-04-27 08:47

your code can be converted into
get_node ("Optimize").disabled = (grass < optimize_cost)

this is same as your code but shorter

supper_raptor | 2020-04-27 08:50

Thank you! That’s much cleaner. I’ll be using that from now on. Cheers!

arbitrarybarry | 2020-04-27 15:11

:bust_in_silhouette: Reply From: Asthmar

Try putting your code under a process delta function instead of the ready function. I tried your code in Godot and I didn’t get anything when it was in a function ready, but under a process function it works for me. This also seems like something you should be checking for constantly anyway. Hope that fixes it.

Yep. Works like a charm now. Thanks so much for going out of your way to test this out on your own. Thank you!!!

arbitrarybarry | 2020-04-27 15:08