Practical lesson 1:
* How to use the "Private Variable"
Practical lesson 2:
* *How to use the Global Variable*
* How to use the "IO battle gameplay - Play Variables in one game
After reading through the whole section of “Variable”, you can master the following points:
1. The concept of Variable
2. Variables Function: Why we use variable
3. The type of value stored by the variable
4. Types of variable
5. How to use different types of variable in the game
- Use cases for private variable
- Use cases of global variable
6. Variable practice class: IO battle gameplay
The Concept of Variable
Deriving from mathematics, Variables is an abstract concept in computer language that can store calculation results or represent values. In the game, we can simply understand that a variable is an object used to store a variable value, which is convenient for quickly finding and using the value in the game logic.
What is Variable made of?
A variable consists of Variable Name = Value. For example, the picture below shows: Attack = 999. It means we store the value “999” in the variable “Attack”; Greeting = Hello! Adventurer, we store the text “Hello! Adventurer” in the variable “Greeting”.
Why do we use variables?
1. It is convenient to search and use values.
For example: I have five weapon: knives, guns, swords, staffs and bows. The attacker value of each weapon is 22, 33, 55, 77 and 99. If the player is attacked by a gun one time, then we can directly use: player HP - gun attack value = remaining HP, without remembering the gun's attack value.
2. Flexibly change the value of game properties.
In the above example, our knife has gained an attack power increase. We only need to reassign a value to the \\variable: knife attack value = knife attack value + attack power increase.
3. Standardize development, double our development efficiency.
As we gradually grow into high-level developers, our maps will become bigger and bigger, and the system will become more and more complex. At this time, variables can help us manage various values and improve development efficiency.
Variable Naming Principles
With the powerful support of Mini World's editor, setting variables becomes very simple. When we name the variables, there are not too many restrictions. It can be a string of characters, characters + numbers, or even some random symbols. No matter how you name it, as long as it is easy to understand, it is fine.
The type of value stored by Variable
In the Mini World, the types of variable storage values are very rich. As long as it is the type of object you can think of, you can set a variable to save it. The picture below shows the variable types provided by Mini World.
Two main types of Variable
In the game, variables are divided into two types: Global Variables and Private Variables.
There are many differences between them when used in Lua scripts. But if we just use the triggers of the Mini World to write the game logic, we only need to know and understand one point: The lifetime of the global variable is the same as that of the main program, and it will be destroyed when the program is destroyed. Global Variable is the data shared by all players, while the private variable is the private data of the player, which will be destroyed when the player exits.
Take a look at the following analysis questions to help you better understand the difference between Global Variables and Private Variables.
1. For each player's attack power, life value, and other personal attributes, is it better to set global variables or private variables? (The answer is a private variable, because it is the player's private attributes) 2. For the game's progress time, the boss refresh countdown, the game's weather conditions, the rank, is it better to set global variables or private variables? (The answer is global variables, because this data should be shared with all players, as long as the game is not destroyed, this data will always be used in this game)
Does it get more esoteric the more you research? But don't worry, practice makes perfect. More practice will make you naturally master the skills! So, let us follow the next tutorial to practice more!
Practical lesson 1:
Practical lesson 2: