Im trying to figure out how to make a button visible at a certain var

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

Here is the script

func ButtonVisible():
if phraseNum <= 3:
$“…/Next”.visible = 1
else:
$“…/Next”.visible = 0

:bust_in_silhouette: Reply From: godot_dev_

If I understand the question correctly, you want a button’s visibility to be conditional based on some variable’s value?

You could achieve it as follows.

  1. add a script to your button
  2. export a property, let’s name it visibilityCondition
  3. connect the button to a signal, say visibility_value_changed, where that signal is emitted any time your variable is changed
  4. use a function named _on_visibility_value_changed in your button script that will perform the visibility check to decide whether your button should be visible or invisible

See example code below:

#Script, button.gd
export (int) var visibilityCondition=3

func signalConnect(your_emitter):
    your_emitter.connect("visibility_value_changed",self,"_on_visibility_value_changed")

func _on_visibility_value_changed(phraseNum):
    if phraseNum <= visibilityCondition:
        self.visible=true
    else:
        self.visible=false

Make sure a node higher in the scene tree connects the node/script that emits the visibility_value_changed, lets name the node/script node1 to the button by calling button.signalConnect(node1), where button is the button you want to toggle the visibility