top of page

Voxel Engine V2 [#2] - Game flow

Now that our Database is ready, we can prepare the game system. We will start a framework for the scripts flow, the main game control and where some threads are managed.

This is the GameManager script.


Go to Unity :

Create a new folder in your scripts, called "System" and inside it, a new script called "GameManager".


Like i said in my Xml/Json project, this file has an unique icon to show that it is important and a common script between developers. But remember that it works like any other script.


Now add it to a new object called "GameManager", in the hierarchy.

(Sometimes, a new object can be created somewhere in the world instead of the 0,0,0 position. Simply right click on "transform" and "reset". It avoids Unity to add complexity to it's calculations when it updates objects)



Now open your "GameManager" script.

We will need to make sure this objects never changes through scenes. We want it to always be the same after it's first instance. So we don't generate databases again and again or clear global values.

To do that, we will add a new instance of this class when it is created :

"Awake" is called before "Start". It happens before Unity starts using the script.

We want to make sure the instance of this class doesn't exist. If it doesn't, this new one becomes the global instance. We also use "DontDestroyOnLoad" to put this class instance on side, so it won't be destroyed between scenes. But if we come back to this scene, with the GameManager already existing and the scene creating the new one, the new one will check if an instance already exists and destroy itself if it does exist.


Now let's create the game.

We need to do things one by one and have an order to follow. We want to control the game flow. To do that, we will add a state to each elements which is loaded and wait for each one to finish. Then we can start building the terrain.


We start by generating databases. We add a new bool called "st_dbBlocks".

st stands for "state". We make it static so the database instance can access it and confirm it is ready.


Now go to your "DB_Blocks" script, and after the database is generated, we change it's state in the GameManager :


Go back to the GameManager script and let's add another state. This one checks if the game is ready. Call it "st_gameReady" :


Then, create the Start method which will be called after Awake.

Inside it we use a while loop, waiting for the game to be ready. And it is ready only when every other state is also ready :

Here, just like the "st_dbBlocks", we will nest "if" conditions for every state to make sure each one is done. So the game will wait until it is ready. That's to avoid issues which are frequent, such as searching for a block in the database when the block database is not ready yet.


Now we can generate the Database on start.


 

Now, for the next part we will simply create a new class inside the script, called Globals.

It will contain every global values. To give you an example, when creating vertices, you can get their values from here instead of creating new ones every time which has the same results. It is faster to access than to create.


Here, the ticks are working just like in Minecraft, every 0.05 seconds, it adds one and blocks like growing crops are updated. But in my project, i will update every 0.1 second. It adds 1 inside a UInt, so it can go to 4 294 967 295 while adding 1 every 0.1 seconds. Then it returns to 0 and causes issues 13 years after creating the world and playing the game non stop.

4 294 967 295 * 0.1 = 429 496 729 seconds in total

Which is 13 years.

This ticks system will be added really late in the project so this is not a problem for now.



The game flow is now prepared, our game has a starting point and a direction!

116 vues0 commentaire

Posts récents

Voir tout
bottom of page