Unity3D game development-from "Resurrection" and "Pause/restore"-about game data configuration management and unity3d Game Development
With the continuous development of game production technology, after the evolution from 2D to 3D, from stand-alone to online games, from PC games to mobile games, gamers are increasingly demanding on the quality of the game, the difficulty of game production increases accordingly, and the entire game R & D system becomes huge and complex, resulting in problems related to game data configuration and management. This article will talk about how to manage and configure game data from the perspectives of "Resurrection" and "Pause/resume" in game development.
Why do we need to configure and manage game data?
I don't know if everyone has the same idea as the bloggers. When you look back at game development, you often find that, if you ignore the visual items in the game, such as pictures, plots, and special effects, the game is essentially a large-scale finite state machine (FSM ), what we usually do is to maintain various states in this finite state machine, from loading the game to starting the game, from starting the game to the occurrence of various events in the game, to the status of various events that affect the entire finite state machine, what we usually do is to maintain various States. This feeling may be more obvious in RPG games, in RPG, players may walk or run in the scene, may be talking to an NPC in the scene, may be fighting with the enemy in front of the scene, or may be at the grocery store. bargaining by the boss ...... It can be said that the game status is switched throughout the game, so what is the most important when switching between different states? The answer is data. What is data? The player's life value, magic value, combat power, defensive power, item usage, price, quantity, game plots, conversations, music, and so on are all data. When we switch between states, what actually changes is the data. It can be seen that in the face of complicated and huge game systems, how to configure and manage game data is a question worth thinking about.
Game Data configuration and management from the perspective of application scenarios
First, let's look at the importance of game data configuration and management from two common game application scenarios: "Resurrection" and "Pause/restore.
Here we take a blogger's parkour game as an example:
Application Scenario-"Resurrection"
"Resurrection" is a particularly common feature in games. The benefit of "Resurrection" is that you can return to the game without restarting the game, of course, this is just one of our most intuitive feelings. The more profound reason is that gamers cleverly use the player's psychology when the game task fails. Everyone in today's life enjoys victory. This kind of mentality is also applicable to the gaming world, because the purpose of the game is nothing more than to give players a sense of accomplishment for happiness. However, when a game task fails, the player will do his best to defeat the Boss to win the game. Therefore, there is such a setting in the game, it can guide players to form consumption habits in the game so that the game can make profits from the players. Now, let's look at a basic "Resurrection" logic!
Private void the Update ()
{
// if the player's health is greater than 0, the game will continue normally
If (Player. Hp > 0)
{
// the game status is Normal
GameManager. Instance. GameState = GameStateEnum. Normal;
// execute normal game logic
DoNormalEvent ();
} the else
{
// the game status is Over
GameManager. The Instance. The GameState = GameStateEnum. Over;
/ / display GameOver
ShowGameOver ();
// player resurrected
ReLive ()
}
}
Player Resurrection requires two things:
1. Adjust the game status from Over to Normal
2. Adjust the player status from death to normal
It is very easy to adjust the game status, because GameManager is a typical Singleton mode, so we can directly change the GameState from Over to Noral. However, we have encountered difficulties in adjusting the player status. What is the problem? The problem is that we write a series of attributes such as the player's life value in the PlayerController class. If we set all the player attributes to Private, we cannot adjust these attributes from the outside. For example, we want players to be revived with full blood, but because these attributes are private, we cannot access them from outside, so when we restore the player's life, the current and maximum life values of players cannot be obtained. However, if we set all the player attributes to Public, we may have to assign values to each attribute in the editor window, because once we try to adjust the balance between the power of both players, this will be a problem we have to deal with. The more deadly player attributes are not always the same, for example, attributes such as the player's lifecycle in an RPG Game will increase with the increase of the role level. Therefore, no matter whether we set these attributes to Public or Private, we cannot guarantee that the data accessed each time is the latest data. In other words, we cannot assume that the player attribute is a constant value in the script, because the data is changing at any time, of course, if the values such as the enemy and Boss are relatively stable, we can directly write them as a fixed value in the script, but I do not recommend this. It can be seen that an important role of Data configuration and management in the game is to maintain normal switching between States. Is the Resurrection interface in the mirage. Each resurrection consumes a resurrection JADE:
So how did the blogger make the resurrection in this parkfun game? Because the blogger did not take weeks into consideration when designing the game and directly wrote the player's life value as 100, when the player was revived, the game's status was adjusted first, then, hide the relevant GUI window, set the player's life value to 100, and generate a new player. It is because of the lack of a good game architecture for games during this period of time that every time the game is played, it is the end of the game that forces itself to break the road, leaving yourself a mess that you do not want to maintain, this is obviously not good, so you need to make a good plan before writing code. I believe this will ensure the quality of the game! Everything will encounter a bottleneck when it reaches a certain stage of learning. Although the process of breaking this bottleneck is painful, if you do not break it, you will always have to stay here.
Application Scenario-"Pause/resume"
Like "Resurrection", "Pause/resume" is also a common function in the game. This function is a choice for players to temporarily leave the game, it can ensure that the player will not affect the game process when doing other things. For example, in games such as legend of the legend, in this scenario, the enemy will not take the initiative to attack the player because the Player looks at the system settings interface, because in this case the game is suspended. After the player exits the system settings page, the game returns to normal. In the mobile Internet era, "Pause/recovery" is more common in games. This is determined by the nature that people play games in the mobile Internet era pay more attention to leisure and entertainment. I remember the time when Cool started to go online every day. Many of my colleagues were playing in class, but the game couldn't be stopped as soon as it ran, therefore, it is often the end of a game session. Bloggers do not advocate this. Playing games is a matter of playing games, but there must be a level in everything. Otherwise, it will become a waste of things. Well, the purpose of analyzing this case is to tell everyone that it is necessary to add such a "Pause/resume" function in the game. Now, let's analyze the data involved in the state transition in this application scenario!
First, after the game is suspended, all objects in the scene will stop moving. At this time, the status of each object in the game has changed, however, in Unity3D, the main reason for controlling the pause/restoration of the game is to adjust the Time. timeScale value. When the Time. timeScale value is 0, the game is paused. When the Time. timeScale value is 1, the game returns to normal. However, note that Time. timeScale may affect all the time in Unity3D, such as FixedUpdate (), coroutine, Destroy (), and animation components. Therefore, if you have special requirements on the paused game status, we recommend that you use other methods! Update () and LaterUpdate () are not mentioned here because these two methods will not be affected. Let's look at the following code:
// whether the game is paused or not
Private bool isPause = false;
// methods to pause/resume the game
Private void Resume ()
{
If (! IsPause) {
Time. The timeScale = 0;
IsPause = true;
} else {
Time. The timeScale = 1;
IsPause = false;
}
}
With this code, we can implement a basic "Pause/resume" function for the game. In the game management class GameManager, we define a player's score. Under normal circumstances, when a player does not die, the score of the player is updated in the GUI, and the score of the player is directly accumulated in Update, therefore, the player's score will be updated after the game is paused, which of course does not meet the actual situation, so you can multiply the Time before this increment. deltaTime can solve this problem. Here is an example to show you how to use this method to suspend a game. I hope you will pay attention to it later!
How to configure and manage game data
Since today's article focuses on game data configuration and management, the following describes common ideas and methods for game data configuration and management. Based on the relative size of game data changes, we divide game data into static data and dynamic data.
Static Data
Static data is basically unchanged in the game or does not need to be changed. For example, in the game, the Boss Level and life value are generally determined, so this type of data can be called static data. Similarly, the content of the NPC conversation in the game is a static data, because the content of the NPC conversation is designed when the plot is designed, and no modification is required. For static data, consider the following methods:
Defining static data as constants in a class does not require modifications to every script.
The advantage of storing static data in files is that you can manage the data. The disadvantage is that you need to write resolution interfaces for different files. Common Data Storage Methods in game development include: json, Xml, Excel, CSV, etc.
Static data is stored in a database, such as SQLIite. However, reading from a local database consumes a lot of resources, and once the database files are lost, the entire game cannot run.
Dynamic Data
Dynamic Data refers to the constantly changing data in the game, such as the score of the player, the player's life value, and the player's experience value. Dynamic data is processed in the same way as static data except as constants in the class.
Summary
This article may seem a bit nagging today. From a technical point of view, this article does not discuss any valuable technical points. However, in the opinion of bloggers, no matter how great a technology is, if there is no good architecture or structure, then when the scale of this project reaches a certain level, this project will have problems. Because according to the broken window theory, when you see the broken windows and do not repair them in time, the whole house will be broken for a long time. Looking back at the long-time game development of bloggers, many of the games that have been done have not been completed yet, this is because the project is basically out of control and becomes a project that you are not willing to maintain. This situation is terrible. If you work on a project by yourself, you may feel that there is nothing to do with it. However, when you complete such a project with others, your problems will become problems of the entire team. Bloggers always want to know what is the difference between playing games with their teams, because they feel that they are not very good at this part. Although you have more experience with architecture, but now you find the problem, why not change it now? The architecture is really important. To those projects that die because of the architecture, the real project should die in practice. The architecture problem will eventually become unmanageable, and this is shameful. Okay, that's all today.
Welcome everyone to pay attention to the independent blog, my blog address is http://www.qinyuanpei.com, get the latest technical blog in time, it depends on you, haha!
There is no doubt about the daily rumors that good things will always come. When it comes late, it is also a pleasant surprise. -- The secret of the city