Path manager
Now that we have a way of saving our level node data, path data and world data, we can start to work on our path manager script. Like I said before, this script will be used to manage all the paths in our world map.
The setup
First, we need to create an empty game object and name it "Path Manager" and make all the paths you have in your scene a child object of them. The end result should look something like this.
Next, create a script for the path manager and attach it to the game object and open up the script. You'll see the following.
Again, we can remove the "void Start" and "void Update" from this script, because we don't need to use them. The script should now look something like this.
First, we need to create a couple variables we need to use for this script. We need to create a list of the path layout script to store all our paths in our world in. Then a reference to our world data asset and lastly, a reference to our player navigator script. We don't need any of these values to be public, but we do need to set them in the inspector at the end so we need to serialize them.
Up next we need to create an on enable function, because we need to get our path references and check there unlocked and first time state, before executing the rest of the code.
Now we need to get all our path layout scripts that are in our world. We've already set this up Unity by putting our paths as a child object of this one. So we can start off by creating an array of path layout and get this component from all its children of this object.
After that, we need to empty our path layout list before we add all our found path layout scripts to this list. This is to prevent duplication.
The next step is to loop over all the path layout scripts we found and stored in our temporary array and add them to our path layout list.
Check already unlocked paths
Now we need to make a function that will check if we've already unlocked a path or not. We also need to call this function in the on enable function, after we're done adding all the paths to the list.
Inside our newly created function, we need to create a for loop that loops over all the paths in the world that we stored in our world data asset.
We then need to check if the path is unlocked or not.
When this is true, we then need to check if the first time boolean is either true or false.
When it is true, we want the path decoration to slowly appear over time. To do this we need to temporarily get our path decor from paths in world list on our path manager script and NOT via our world data asset.
Then, we need to set our first time and unlocked boolean of our selected path decor script equal to the value that is stored inside our world data asset before we start the coroutine to decorate the selected path.
Now for our else statement we only have to set our values of our selected path equal to the values inside our world data asset.
The next step is to create a boolean function that returns either true or false depending if a path is unlocked for the first time or not. We do this to prevent the player from being able to move across the path while it's currently in the process of displaying itself. We also need to call this boolean function inside our check already unlocked paths, underneath our for loop.
Check first time unlocked
Inside our newly made function, we need to create a for loop that goes over all the paths in world list.
Next, we need to check if our currently selected path is unlocked or not.
After that we need to check if our selected paths first time boolean is true or not. We do this by getting the path decorator script via get component.
When this is true, we need to set, in our world data asset, its selected path unlocked value to true and after that the booleans return value must be false.
Now if all of this was not true, it means that all the paths have been unlocked beforehand already and the booleans return value must be true.
Check allow movement
And with that, our boolean function is now ready to be used. Now we need a way to check if the player is allowed to move around or not. To do this we need to create a new function that checks if the player can move around or not. This has to be a public function, because we'll need to access this in another script later in the tutorial.
Now to set our path navigator can move boolean to the return value of our check first time unlocked boolean function.
And with that our check to see if the player can move around or not has been implemented.
Update unlocked status
We only have to add one last function to this path manager say it's complete. We need a way to update our unlocked level status after the player unlocked a level node for the first time.
To do this we need to create another function with two parameters, one takes in our path data asset reference and the other one a boolean to set our first time boolean value.
The only things we need to set in this function is our unlocked boolean inside our path data to true and our first time boolean equal to the boolean value we get passed through to this function. After that we call the function "CheckUnlockingMovement();" to see if we need to let the player move around or not.
Now we are done with our path manager script, but we still need to make a few adjustments in our path decorator script, because we still need to send this information we need in this function from that script. Let's go back to that script and add that functionality.
Adding path manager to path decorator
First, we need to add the path manager script as a variable reference.
Then, in our start function, we need get the path manager script from our parent object. To do this we need to use the get component in parent functionality provided by Unity.
Lastly, we need to go inside our "DecoratePath()" coroutine and inside our if statement where we check if first time is true, we need to add our update unlocked status function. We'll add this underneath our second yield return function. In here we pass through our path data that we can get from our path layout reference and our first time boolean inside this script.
And with that we've now completely set up our path manager script, but we still need to set our reference inside the inspector. Let's go back to Unity and do this now.
Unity editor setup
Simply set your world data reference to be your world data asset and your path navigator to your player object.
Now open your world data asset and you'll see the following in your inspector.
In our paths in world list we need to add all our path assets we've created for our project. For me, that will only be one, because I only have one path, but yours may vary depending on how many level nodes and paths you've got set up. We can leave our current level reference empty, we'll set up that part later in the tutorial.
And with this done, you've now successfully implemented your path manager. The next step will be creating a transition between scenes.
If you want to test this functionality, simply go to a path and uncheck "Unlocked" on your path layout script and "First Time" on your path decorator script. Then go to the corresponding path data asset and adjust the two booleans to your liking and press play to see your changes in action.
Last updated
