The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

+2 votes

Hi All,

I'm a total beginner for both coding and game dev. I'm trying to create a simple child book with with various selection options on the way as a first project.

I create pages as different scenes and change them according to selection.

In a particular spot, I have 2 answers (buttons) and I want to hide or show them in certain condition. (condition is visit another scene previously or not).

Could you please help how can I do it?

Your help is really appreciated.

in Engine by (17 points)

1 Answer

+4 votes
Best answer
func _ready():
     $Button1.visible = should_show_button_1()
     $Button2.visible = should_show_button_2()

func should_show_button_1() -> bool:
     return some_condition

func should_show_button_2() -> bool:
    return some_other_condition

should_show_button_1() and should_show_button_2() just need to return true or false depending on if the button should show (i don't know how you are determining it). The key here is the button's .visible property.

by (1,663 points)
selected by

Thank you for your answer.

I believe this is the solution I'm looking for.

Thing is, I still don't know how to place the conditions.

The conditions are;

Button1 is visible if a certain page (scene) -lets call it Page00X- is already visited (opened) before.
Button2 is visible if not

Could you please assist how can I add these conditions to the code?

If you're not saving that state somewhere, you'll have to start. One option is to add an autoloaded script. This is a script that is loaded... wait for it... automatically - and is available from every other script. You could call it GameState.gd or something. In that script you can have something like:

# GameState.gd
extends Node

var visited_levels = {
    "level_1": false,
    "level_2": false,
}

Once that is registered as an autoloaded script, you can use it like this:

#Level1

func _ready():
    GameState.visited_levels.level_1 = true

Then, back to the original question, you could do something like:

func _ready():
     $Button1.visible = GameState.visited_levels.level_1
     $Button2.visible = GameState.visited_levels.level_2

Here you wouldn't need to create those extra functions because GameState.gd already holds the boolean value you are looking for.

You just solve 2 of my problems, and 1 I don't even know it exists!

So many thanks again.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.