Persistent Data - Jaren Urey


Preface:

Sometimes it is good to have a bit of foresight when it comes to development. It sucks having to go back and fix things you have already made. But hey, hindsight is 20/20. Up until now many of the data being plugged into the HUD are being stored in the player object, which in most cases is fine. But with our latest feature "Enemy Possession", it becomes a problem. To break it down, this is how the possession system works:

  1. Every tick the player checks what it is looking at to see if it is an enemy.
  2. If it is an enemy, it gets plugged into a variable to be used later.
  3. While looking at an enemy the player can press the possess button.
  4. Doing so destroys the targeted enemy and the player object, while spawning a "possessed enemy" object for the player controller to take control over where the previous enemy was.
  5. The player is now where the enemy was with a new possessed enemy character to control.

De-possessing an enemy is similar, but in reverse. Where it destroys the possessed enemy and respawns the enemy and player objects.

Problem:

Destroying the player removes all of the variables and data that the HUD and other systems might be needing. Either not allowing them to update, or worst case causing runtime errors. This is most apparent in the player HUD, with some values either being frozen or set to null. 

Another issue is what when the player de-possesses an enemy and respawns the player, the begin play events triggers, adding another HUD to the viewport. Causing them to overlap on the screen.


Solution

The fix for the first problem I had to go to where every variable I wanted to persist to be moved from the player, to the game instance. Combing through all of my previous code to find where I have get/set them all. Then also adding anything that needed updating into the possessed enemy code. Such as keeping track of how long the player has been in the level.


To solve the other issue is a simple fix. Whenever the player possesses or de-possesses an enemy, the HUD is removed during that action, to then be added during the begin play for both objects. 

Leave a comment

Log in with itch.io to leave a comment.