Global Variables

Note: This is an automatic translation from Russian. It looks good enough, but there may be inaccuracies. We are sorry for the inconvenience.

When developing a game, there will definitely be a need to store any values or states and have access to them from anywhere in the game code. In TeravQuest, when processing all game events and calling the corresponding methods, an object of the Game class is passed to these methods. This was seen in the previous article when creating action handlers. And the main class of our game, MyGame, inherits from the Game class, so its fields, properties and methods can be used as global ones.

Suppose, for example, that by performing some actions in our game, the player is advancing through the quest. And from how far he has advanced, some events and locations will be available to him in different ways. Let's create a QuestStage property in the MyGame class:

using TeravQuest;

namespace MyGame
{
    [Serializable]
    public class MyGame : Game
    {
        public int QuestStage { get; set; }
        
        public MyGame()
        {
            Name = "Shortest quest";
            QuestStage = 0;
            AddLocation(new Home());
            AddLocation(new Forest());
            AddLocation(new ElvenHouse());
            GoTo("Home");
        }
    }
}

This creates a property of an integer type, and sets it to zero in the constructor. Now we can get and change the value of this property in all methods that receive an object of the Game class as a parameter.

Now change the ToCave method in the Forest class:

        private void ToCave(Game game)
        {
            if ((game as MyGame).QuestStage < 1) game.IO.Message("I need a key.");
            else game.GoTo("Cave");
        }

Now, while the value of the QuestStage property is less than one, the player will not be able to enter the cave, but instead will see a message that he needs a key.

Despite the fact that the MyGame class inherits from the Game class, according to the syntax of the C# language, we cannot directly access its properties through the game variable, which is represented by the Game type. Therefore, it is necessary to explicitly tell the compiler that we want to use this variable as the MyGame class: (game as MyGame). Or like this: ((MyGame) game).

To display messages to the player, you can use an object that implements the IIO interface, which can be obtained through the IO property of the game object and call one of its methods. In this case, the Message method is used, into which the message is passed. You can read more about the available methods in the IIO interface description .

Thus, you can store any information and have access to it anywhere in the game by simply adding new properties to the MyGame class.

Important! If you have written any of your own class and want to create a property in the MyGame class with the type of this class, and the information stored in this property is important for the game, be sure to mark your class with the [Serializable] attribute. This is necessary for the game save / load mechanisms to work in the game.