Game Development Reference
In-Depth Information
Tip
Lists and dynamic arrays were covered in Chapter 3, when considering event handling.
Lines 26-60.
The
Load
function searches through all Enemies in the
GameStateData
enemies list, and finds a matching Enemy entry by ID number to
reload the appropriate Enemy data. This functionality therefore depends on each
Enemy in the scene having a unique ID. If two Enemies have a matching ID, then
confusion will arise when restoring Enemies. Notice that to restore the Enemy
position in the scene, the function
Agent.Warp
is used instead of
transform.
position
, which was used for the Player. Doing this is consistent with any agent
that relies on pathfinding, as opposed to nonagent objects like the Player, whose
position is “free” and unconstrained by navigation or physics. Chapter 7 covered
pathfinding in more detail. More information on the
Warp
function can be found
in the online Unity documentation at
https://docs.unity3d.com/Documentation/
ScriptReference
/
NavMeshAgent.Warp.html
.
Testing Save and Load Functionality
Now we've got enough for everything to work together seamlessly for loading and saving games
in CMOD. In sum, we've created a LoadSaveManager that integrates with the
GameManager
class,
and we also amended the
PlayerController
and
Enemy
classes to respond directly to load and save
events through the NotificationsManager. Let's test this now from Unity. Simply press the Play button
on the editor toolbar, make some changes in-game, and then display the main menu using the
Escape key on the keyboard. Finally, click Save and exit the game (see Figure
9-5
).
Figure 9-5.
Saving a game from the main menu