Back:
* Variable
Introduction:
In the previous lesson, we learned the concept of “Variable”, naming rules, variable function, and a simple understanding of the difference between private variable and global variable.:
Previous lesson portal: Variable
Among the “Two main types of Variables” part , which we have mentioned, “we can generally set personal attributes as private variables, such as Attack, HP, Speed”.:
Guide:
In this class, we will take a closer look at how to use Private Variable through the case [body size growing by eating fruit]. The knowledge we will learn is as follows::
1. Create a private variable:
2. Variable assignment and calculation ideas:
3. Use triggers to perform trigger assignment operations:
4. Use triggers to initialize and modify the player's size attributes:
Create Private Variables:
Set a private variable to change the size of the entering player:
Open the trigger → click the variable library in the lower right corner → select private variable → create a new variable → select the variable type as “value” → set the variable name as “player size” → enter the value as 0.5
Set a private variable to change the player's size:
Continue to create a new private variable → select a value variable → variable name “grow bigger” → default value 0.2
Set Trigger to Initial Size:
Apply a private variable to initialize the size of the new player:
Step 1: Set a trigger and name it “Change the size of the new player”:
Step 2: Detect the new player, and use the private variable “player size” to change the player's initial size:
! Note: When we select variables in the trigger, only global variables can be seen in the variable library, and private variables need to be found in function library → player → private variables.:
:
Variable Assignment Logic and Implementation:
Change the value of the private variable (player size) through the logical operation of the trigger, which manipulates the resizing of players in the game:
We have already set the two private variables of the player, “Player Size” and “Grow Bigger”.:
In the game, if the player eats fruit once, the player's size will become bigger. The calculation logic is as follows: player size = player size + grow bigger:
Suppose the player eats the fruit, then the new body size = 0.5 + 0.2 = 0.7, and the player's body size at this moment = 0.7.:
Next, let's take a look at how to set the trigger!:
Step 1: Set up a new trigger named: Change Player Size. The event is that players consume props, and the type of the prop is fruit.:
Step 2: In the trigger action, add the logical operation of player size increase: private variable “player size” new value = player size + grow bigger:
Step 3: Assign the value of the private variable “Player Size” to the player. After that, the player can become bigger by eating fruits in Play mode.:
Let's switch to Play Mode to see the final effect!:
In the previous section “Variables”, flexibly changing the value of game attributes is one of the reasons why we use Variable which we metioned. So, in this case, what is the fastest way if we want the player to grow more obviously after eating fruit?:
The fastest way is to directly change the value of the private variable “Grow bigger”. Let's see how to do it?:
→Change the value of private variable “Grow bigger” to 1.5:
Did you immediately realize the essence of variables? It is recommended that developers who are exposed to variables for the first time do more real practice, try to apply variables to implement different gameplay, so that they can understand variables more deeply and lay a solid foundation for developing larger games in the future!:
Exercise:
Exercise 1: The player who just entered the field has an Attack Damage 10. They can get a 10 Attack Damage increase by killing one animal in the game.:
Exercise 2: The player who has just entered the field has a speed of 1, picks up a fruit, and the speed increases by 0.1. If he accidentally touches the grass, the speed will be reduced by 0.1.:
If you have mastered this class, you can continue to learn the application of Global Variable. The following is the portal::
Practical lesson 2: *How to use the Global Variable*