Disable/Enable Global Variables in scripts

Godot Version

v4.5.1 stable mono

Question

Hi,

I’m working on a Tactical-RPG, inspired by the Mega man games.

I wanted to add random enemy attacks in my game, but allow the player(s) to freely allow it or not.
Describe the feature / enhancement and how it helps to overcome the problem or limitation

I was wondering, if it could be possible to change the state of the global variables, for example: sounds in games, or allowing harder difficult game modes.

In the scripts, you can change the state, for example: Random_Battles.change_state(true).
If this enhancement will not be used often, can it be worked around with a few lines of script?

This will help in games that require global variable settings. For example, if the game was in the highest difficulty, and there was options to turn off random battles, or frequent enemies. It would make things easier.

Thank you.

Of course it is. So lets say you have a global script “game_settings.gd” called “GameSettings”

var game_difficulty: String = "Easy"
var random_battles_enabled: bool = true

Then you can change these like any other variable. In any other script:

# This might be set in a difficulty menu screen
GameSettings.game_difficulty = "Hard"

# This might be set in an options menu screen
GameSettings.random_battles_enabled = false

Then in your game:

if GameSettings.random_battles_enabled:
   generate_a_random_battle()

# or perhaps something like
match GameSettings.game_difficulty:
   "easy":
        number_of_enemies = 5
   "normal":
        number_of_enemies = 10
   "hard":
        number_of_enemies = 20

So for your random enemy attacks, in a settings menu somewhere you can have a checkbox so the player can set if the random enemy attacks is set to true of false. Then when they start your game, your game can just read the global setting for it, and enable or disable them as required.

Hope that helps in some way.

3 Likes

Thank you very much for your answer!

I was wondering, it is only for the global variables. I’m not sure if you can toggle them on or off, in the code. From the autoloader.

Or is there other ways on how to do this? I think maybe using Nodes? And strings.

Thank you very much!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.