Game
Great Ape
6 years ago

In-game Level Editor: Saving & Loading


Welcome back!
It’s me, Oskar, the programmer of Great Ape, back again with another blog post!
Last time, before Christmas, I talked about making the groundwork for my in-game level editor for Great Ape. Now, it’s a new year and I’ve made progress since the last blog post!

I ended the post planning my next move in the project, Saving & Loading levels, and that is what you will get to read about this week, enjoy!

Finding (and customizing) the solution

In the last blog post I mentioned the problem with saving GameObjects in-game since Unity doesn’t have any options available for something like that. My solution would’ve been either to manually serialize all the types in Unity and make my own save and load system or to use an external solution. I had to choose the latter because of the short time-frame of the project’s development.
The solution I found was made by Cherno on the Unity Forums, Unity Save Load Utility.

5d0a908c34202.gif

I will not go over exactly how it works, Cherno does that himself a bit in his forum post.
But the basic idea is that you have a persistent GameObject in the scene that executes the save and load methods in the specified path of that object. You can either save everything in the scene or mark specific GameObjects by adding the ObjectIdentifier component to the ones you want to save.
You then choose to save what you have specified to a .sav file. When you later load the save the persistent GameObject will delete everything in the scene and replace it with the GameObjects from the scene. Simple as that.

The first thing I fixed was linking my own UI to the save and load methods instead of the, kinda ugly, Unity GUI seen above. I also had to overload some methods to fill my needs since I only wanted to specifically save only the level being edited and then load the chosen level.
Nothing that complicated, just took a bit of time to understand the code written by Cherno. Now I wanted to prompt the user about their unsaved changes.

The “YesNoDialog”

I had already made a custom dropdown window to easily choose between loading, saving etc. Now I have also created what I call a “YesNoDialog” that will prompt the user with something like unsaved changes where they can choose between three actions: yes, no and cancel.

5d0a908ecae9b.png

The solution was kinda simple where I created the prefab seen in the image above and use it in the scene when I want to prompt the user with a dialog. Creating a new YesNoDialog is really easy where you just have to call the method NewYesNoDialog with a text describing the intent of the dialog and also actions for the yes and no buttons.

		
			public void NewYesNoDialog(string description, UnityAction yesButtonCall, UnityAction noButtonCall)
		
	

The way I handle the dialog is that I bind the buttons with the specified UnityActions. When they are pressed, the code is executed and after whatever happened we check if it was the Yes-button was pressed. If the Yes-button was pressed that means the user saved or whatever and that also means they now want to do whatever their first intent was before being prompted about the unsaved changes.

This is probably one of the things I will have use of in later projects since Unity doesn’t really have something like this built-in in the engine.

A real boy dialog window!

As the title alludes to I wanted a “real” window for choosing and loading levels instead of creating my own. Not just because creating my own would take a long time but I thought it would be fun to use some classes that are not necessarily limited to game development.
What I mean by a real window is one in the actual operating system. I sadly had to limit the editor to windows only but I got to use Windows Forms classes to make a “real” save load dialog!

5d0a909114350.png

Through some problems with implementing .dlls and reading MSDN documentation I managed to use the SaveFileDialog and OpenFileDialog classes.
I will not go into how they actually work since you can easily read the documentation I linked above.

Ending notes

I now had everything working except for some unintended behaviour when saving that was easily fixed after a couple of days of confusion. NOW I had what I wanted and I can easily create, save and load levels however I want! I will return in some time with tales of my Custom Inspector I’m making for the level editor!

See you next time!

5d0a9093a4001.gif


0 comments

Loading...

Next up

Did you know that today is #InternationalJazzDay? 🎷😎

No? 😯 Well, we couldn't pass up this date!

And what better to celebrate this day than #Pecaminosa #OST? 🎷

Let us know what you think about it and have a great weekend!

Shoobies leave a sticky trail of mucus in their wake, which can impede movement for any creature that steps in it.

Another house i made long time ago.

I have added rewards for quests so that villagers can give you something in return for your hard work helping them. 🥳

I want to know - what's your favourite quest reward?

Hi everyone! I started to create some environment props for my new video game, here is a cozy house in the forest. I'm planning to switch from Unity to Unreal... Let's see what new challenges I have to face. Made in Blender and Substance Painter.

Horror WIP A track that will be featured in a future horror game soundtrack! Stay tuned!

Likes appreciated ✌️

#gamedev #composer #horrorgame #indiegame #IndieGameDev #soundtrack

Levels' maps.

2d lighting experiment

it happens