Jump to content
  • Advertisement
  • 03/03/19 12:19 PM

    Building a save system for level objects in Unity Pt1 - Checkpoints

    General and Gameplay Programming

    Christin Kalina
    As the player moves along in a game and interacts with different objects in the world, the aim is to reflect his actions in the game by changing the state of objects permanently. For example, a door is opened or some first experience guidance does not need to be displayed anymore. 

    Objectives

    • Save and restore states of objects in the scene according to the progress of the player.
    • These objects may be are interconnected. (A door opens when pulling a level/ defeating an enemy)
    • Let level design decide and modify which objects need to have their state changed when.

    Used programming concepts

     

     

    Checkpoints

    Step 1 - Checkpoints

    First, we need something that defines what a state change in the game is and when it happens. Let's call this a Checkpoint. When the player does an action to activate the checkpoint let's call that reached the checkpoint.
    1. This checkpoint needs an ID to be identifiable for saving and restoring. 
    2. This checkpoint also needs something that can be fired off when the checkpoint was reached.
     
    In my case, for the ID, I created an enum because its nicely readable in the inspector. On top of being readable by humans it also is robust against typos and other string mischief.
    For firing off the state changes, I picked UnityEvents which have the advantage that your level designer can put anything in there and debug and modify it by himself.
     

    Step 2 - Reaching Checkpoints

    So, we have a MonoBehavior called Checkpoint, that has an ID and a UnityEvent.
    Now, you can attach this to any gameObject in your scene. What will happen? Nothing, because we are not reaching the Checkpoint yet.
     
    To reach the Checkpoint, we add a public method that calls the UnityEvent. Now we can call this function from anywhere in the code or from another UnityEvent
     

    Step 3 - CheckpointController

    Great! But wait, when I restart the scene it does not remember that I reached the Checkpoint
    To save and restore the state of the Checkpoint I created a CheckpointController that knows 
    1. all the checkpoints in the scene and
    2. who to talk to make the state persistent on the disk.
     
    For the first part, I created a function that registers the Checkpoint in the CheckpointController. This can be called in Awake() for example. Now this controller can store all the Checkpoint gameObjects in the current scene. 
    For the second part, I let the Checkpoint inform the CheckpointController when a it was reached. The controller that is hooked up to any kind of saving like a JSON file reader/writer can then write the ID of the Checkpoint that was reached to the disk. On top of saving, I also inform all the Checkpoints with the ID that was just reached to fire off their UnityEvent. This way, I do not need to fire off the UnityEvent on the individual Checkpoint anymore but let it backfire through the CheckpointController .
     

    Great success!

    Now, when Unity starts a new game, the CheckpointController needs to receive the persisted list of Checkpoint IDs that where reached before. When the scene has loaded successfully, it will notify all Checkpoints that have the IDs that were saved before. (Remember that the Checkpoints register themselves to the CheckpointController on their Awake()). 
    So that the Checkpoints that were reached before now also fire off their UnityEvent when the level was loaded. 
     
     
     
    image.thumb.png.decd6ab3789f8ea019569c0271bbe6b2.png
     
     
     
    image.thumb.png.bc41df32eb1d24bc225721c22ea45c41.png
     
    Thank you for your interest, let me know what you thought or if you have any questions :)
     
     


      Report Article


    User Feedback


    Thanks for the idea! I was having a hard time thinking about how to save object states throughout a scene where it will only save objects that were interacted on. I used a traditional saving system where I serialized the whole class and saving some variables.

    This reminded me that I can just save some checkpoints and compare the combination of reached ID's and then make a scene load in a certain way.

    Share this comment


    Link to comment
    Share on other sites
    On 3/18/2019 at 12:37 PM, iRyu said:

    Thanks for the idea! I was having a hard time thinking about how to save object states throughout a scene where it will only save objects that were interacted on. I used a traditional saving system where I serialized the whole class and saving some variables.

    This reminded me that I can just save some checkpoints and compare the combination of reached ID's and then make a scene load in a certain way.

    Cool! Happy that it actually helped someone :)

    Share this comment


    Link to comment
    Share on other sites


    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now

  • Advertisement
  • Advertisement
  • Latest Featured Articles

  • Featured Blogs

  • Advertisement
  • Popular Now

  • Similar Content

    • By exzizt
      Hey everyone! I just wanted to announce that after eight months of hard work, we're approaching the launch of our second BETA! Everyone who signs up gets to be the first to play.
      Signup here: https://www.playbirdie.com/

    • By ThomasG_art
      A complete scene and prefabs created in unity. allow you to get you started rapidly.
      Mountains, ruins and cave areas are all included:

      http://u3d.as/1ypw 

      The cliffs/rocks and ruins feature a trilanar shader, you can customize the colors/textures and overlays. The water and vegetation shader are also customisable. 
      The trees/vegetation prefabs are using unity's terrain system and are already set-up for you. The ruins prefabs are modular and fit on a grid. 
      The cloths prefabs are using unity cloth's system and can be adjusted. 
      Particle effects, air particles, clouds, mist , butterflies, etc...

      Trailer:
       




    • By horror_man
      Hello, I'm currently searching for additional talented and passionate members for our team that's creating a small horror game.
      About the game: The game would be a small sci-fi/post-apocalyptic survival horror 3D game with FPS (First person shooter) mechanics and an original setting and story based in a book (which I'm writing) scene, where a group of prisoners are left behind in an abandoned underground facility. It would play similar to Dead Space combined with Penumbra and SCP: Secret Laboratory, with the option of playing solo or multiplayer.
      Engine that'd be used to create the game: Unity
      About me: I'm a music composer with more than 4 years of experience and I'm currently leading the team that's creating this beautiful and horrifying game. I decided that making the book which I'm writing into a game would be really cool, and I got more motivated about doing so some time ago when I got a bunch of expensive Unity assets for a very low price. However, I researched about how to do things right in game development so I reduced the scope of it as much as I could, so that's why this game is really based in a scene of the book and not the entire thing. Also I'm currently learning how to use Unity and learning how to program.
      Our team right now consists of: Me (Game Designer, Creator, Music Composer, Writer), 3 3D Artists, 4 Game Programmers, 1 Sound Effect Designer, 2 Concept Artists, 1 3D Animator.
      Who am I looking for: We are looking for a passionated and talented 3D Environment Artist that's experienced in hard surface models and the creating of closed/underground scenes with a realistic feeling to them. Familiarity with the sci-fi/horror genre is a plus.
      Right now the game is in mid-early development and you can see more information about it and follow our progress in our game jolt page here: https://gamejolt.com/games/devilspunishment/391190 . We expect to finish some sort of prototype in 2 months from now.
      This is a contract rev-share position
      If you are interested in joining, contributing or have questions about the project then let's talk. You can message me in Discord: world_creator#9524
    • By Homeship
      A 3D shooter requires 3d artists, animators and sound engineer.
       
      Good day.

      The project is unequivocally non-profit and applicable to such tasks:
      - go through the entire sequence of the project’s release in Steam,
      - get the project in the portfolio
      - Practice low-budget indie marketing.
      - game development skills upgrade )
      And all this - with a minimum budget.
      The game is done on Unity.
      Some free content assets from the asset store. Many of them are highly modified.
       All code is mine. 
      The plot and setting of the shooter is based on the comic strip by Alexadr Dyakov "Owl - is an effective manager.
      The Hare from the comic strip will act as the protagonist.
      His opponents: jerboas, raccoons, bats, wolves (as the most guards and security forces of the owl) and owls themselves.

      Duration of the game - 5 locations (Forest, Plant, Offices, Snow-capped mountains, Desert).
       2-3 hours of gameplay.
      The emotional goal of the game is to respond to the experiences that arise when reading a comic book.))
      The use of characters and several pages of the comic book has already been agreed with the author of the comic book.
      Game Features: 
      - "dismembered" - you can shoot \ tear off an enemy limb, tail or head (which is lethal, of course). Also, the "fifth point" (ass) is vulnerable for some enemies. 
      - several types of weapons for making "meat": fork, axe, chainsaw, flamethrower, grenade launcher, machine gun, double-barreled shotgun and one futuristic weapon avaliable close to the end of the game 
      - a lot of blood and violence
      - high game dynamics (not DOOM but... ))
      - the possibility of attacks from the back, which provides the mechanics of "silent killings"
      - enemies have a limited view, and respond to sounds 
      - weather system (snow, rain, fog, thunderstorm, wind), change of time of day 
      - localization of hits and change in the behavior of opponents, depending on their condition.
      We trying to make it cheap (in development), simple and fun as possible.
      At the moment, alpha version of game is already ready. All described features of the game are implemented already.
      Current development status:
      - game mechanics (moving, interacting, shooting, damage models) - READY
      - UI - READY
      - metagaming (purchase of weapons and ammunition between locations, briefing and debriefing) - READY
      - AI of opponents (attacks, evasion, player search, reactions to changes in the situation, state of stunning, etc.) - READY
      - first location (passage scenario, content, level design, cut scenes) - READY
      - rest of locations - WIP
      the software part is 95% complete. Polishing gameplay, optimizing and bug fixing in progress.
      The following specialists are invited to the project enthusiastically:
      1. Sound-designer
      2. 3D artists (modelers) - enviroment props
      3. 3D Animators - there are 4 characters need to be animated
      Planned Steam release  -  september-october 2019.







    • By RoKabium Games
      For this Screenshot Saturday we captured an image during one of the Earthquake events that occurs on Tartarus. During this time the underground turns into a fire inferno with Monodons spawning in big numbers so be prepared to defend yourself.
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!