Skip to content
This repository has been archived by the owner on Apr 2, 2022. It is now read-only.

Game Controller and Persistence

Will Molloy edited this page Oct 26, 2017 · 3 revisions

The Game Controller is an extensive, well documented library for maintaining and retrieving static game state for several game features: scenes grouped by level, any specified object location, inventory, items and levers. As well as extension methods for resetting game state per scene or per level.
Because the game controller is used by every scene had has code running every frame it needed to be efficient and a lot of time was spent testing it.

Grouping Scenes with Attributes

A Public Enum is included in the game controller to keep a record of scenes. Attributes such as level and filename are attached to the scenes. This Enum saved a lot of headache in development as it adds a drop down when selecting things such as the scene to load when activating portals. Previously you had to manually type the file name of the scene to load and we had issues with members mistyping the filename and wondering why their implementation didn't work. Also this meant queries such as retrieving scenes per level could be dynamic: previously a hard coded implementation where the file name for each scene in a level had to be checked was in several places, now the hard coding is only in one place.

Saving Object Positions

To persist object positions per scene you simply drag the objects you want to persist as children of a scene-persistence component (e.g. players, boxes, planks). This works because a recursive method retrieves all child-components at startup. Two Dictionaries (hash tables) are used to maintain both initial and the latest position of these objects. When restoring scenes the 'savedPosition' dictionary is queried and when resetting the 'initialPosition' dictionary is queried.

Maintaining Item RNG and Global Inventory

The RNG element of our game is to randomly generate items on a per level basis but not per scene. To resolve this the generation of items is maintained per scene in an ordered dictionary which is only cleared when levels are reset. This means items are only generated once for a scene so the position of items are maintained when returning to scenes.

The inventory is also global and persisted throughout the entire game (including levels). This was not easy to implement. Initially we kept a store of items the player had picked up and on loading a new scene located the inventory and placed these items into the inventory. This didn't work as the item image was not rendered because Unity requires the game object to be re-created. To fix this persisted items are re-spawned (again) and dropped at the player location and therefore immediately picked up.

Maintaining Lever State

It was trivial to maintain the plate location as the object persistence was already setup. However to maintain the plate direction and lever state two additional dictionaries were required. The use of many dictionaries meant different queries could be made for various configurations: Resetting a scene resets all levers in that scene but not in other scenes, while resetting a level resets all levers in all scenes in that level and dying in a scene means only the player state is reset but not the lever state.

Clone this wiki locally