RPG - How to approach having a persistent town scene and a dungeon scene that is generated at will?

TL;DR: If you wanted to make a game where you have a main scene in which every change should be saved and then also a secondary scene where you don’t care about changes and the player should be allowed to travel freely between the two scenes, how would you approach it? Would you load the secondary scene additively on top of the main scene or would you treat it in much the same way you would a save system? Or would you do something completely different?

I put RPG in the title because I believe that makes this easier to explain. And I know RPG’s are populare with noob developers such as myself. :smile:

What I have at the moment is one scene with a town where my player starts. In this scene he should be able to receive quests, trade with vendors and most importantly anything he changes, items he puts on the ground or whatever else he does here should be saved throughout the game. In addition to the town scene I also have a scene with a dungeon which is generated randomly every time the scene is loaded. The idea is for the player to go into the dungeon, slay the monsters and then return to the town to sell etc. and then load a new random dungeon, rinse and repeat. Think any Diablo game or Diablo clone.

But how do you approach something like this? Currently I have very little experience with games spanning multiple scenes and maybe this doesn’t have to involve multiple scenes at all? I don’t know yet.

I’ve gotten the basics down so far. When the game starts my player spawns into my main scene and my player and everything connected to him is marked as DontDestroyOnLoad so it persists when I go back and fourth between my dungeon scene and my town scene. His inventory, his stats and everything he picks up or changes about himself persists the way I want it to. But the town scene is loaded fresh every time the player returns to it so all vendor inventories are reset, any items on the ground are gone etc. etc.

Currently I have two different alternatives in mind for approaching this:

Alternative A) I’ve noticed I can load scenes additively. But then I need to manually take care of hiding everything in the town scene when the dungeon scene is loaded, and any scene specific settings like scene lighting are not loaded from the dungeon scene, so I guess I need to create a system to handle those as well? And this approach feels wrong to me. To me loading scenes additively feels like something you use for a large streaming world or as a way of loading complex prefabs, like in a roguelike for loading new rooms as you walk through the level. Not something you’d use when you actually want to replace your current scene like I do. And I don’t like the idea of having thousands of game objects (my town) just hanging around deactivated while I’m in my dungeon. Not sure if this would be a performance problem or not, but I just don’t like the idea of it. :stuck_out_tongue:

Alternative B) Another alternative is handling it in much the same way you would a save system, I guess (I have never written one of those either so I don’t know :p). Basically I’m thinking that when I load the dungeon scene and destroy the town scene I save everything that has changed and then when the town scene is created again I add back all those changes. I have to create a save system sooner or later to be able to save my game between play sessions so I would have to tackle such a system no matter what. I’ve been thinking about how other games, Unity or otherwise, handles this and reusing the save system seems very logical to me. For the player there is no difference whether he puts some items on the ground and quits the game or whether he enters the dungeon and returns to the town, he expects those same items to be there on the ground in either scenario. Which is why I’m thinking you would use basically the exact same system for both of these things.

What do you think? Any other ways to approach this? And are there any guides/tutorials/articles/projects on the topic that you can point me to? I’ve googled quite a bit, but I haven’t found anything yet.

Note: Almost every Diablo game and clone is multiplayer and I will probably try to add multiplayer to this game at some point as well, but that’s not a priority at the moment. I’m sure having multiplayer means I need to handle things in a specific way, but I won’t worry about that at this time, I just hope to hear about some different approaches towards accomplishing something like this.

I think the relevant questions here are:

  1. Are you planning to allow the player to save the game, including the state of the town? If so, use the save save-game style, no question. You get it practically for free in terms of work :slight_smile:
  2. Are you planning to deploy to a platform with limited memory? In this case, that means mostly mobile and web. If so, you need to aggressively remove stuff from memory, which means you need to use the save-like system and destroy the town as soon as it’s not visible (and probably before you load much from the dungeon, as well).

The best tutorial for saving/loading that I’ve found. And it’d be perfect for this problem, too.

1 Like

You should look into scriptable objects. These can be used to create data storage objects that are not instantiated into any scene. They exist independent of that and thus can be accessed throughout your game by any scene. You can create objects for the various vendor inventories, as well as items dropped on the ground. Then when your load into your town scene just check if these objects have data and populate with the saved data rather than random data.

Here are some links into the Unity tutorial sections about them:

I think you can search and find even more tutorials from Non-Unity sites dealing with using them for things like inventory.
.

1 Like

Switching between scenes wouldn’t require you to save in the same way as exiting the game (provided you have the memory to store the data, of course). While the game is still active, you can store the items’ positions and such in RAM.

Between your A and B options, I’d say ‘B’ is the right call, as you suspected.

1 Like
  1. Yes

  2. No, desktop ftw. And for the time being my own PC only which I will upgrade to fit my game. :smile:

But great, that was reassuring. In fact, I just finished watching that video. I was watching it while I wrote this post. :stuck_out_tongue:

Cool. I’ve seen them mentioned on numerous occasions, but I’ve never looked at them myself. The first real interaction I’ve had with one is probably the new post processing stack where you save your settings to an asset rather than a component. I will definitely look at this as well. My inventory system needs an overhaul anyway as both logic and data storage is tied to the UI at the moment.

But how would one do that without writing something that is very similar to a save system? I was thinking that either Unity has a system I can use similar to additive scene loading or I would have to write something myself where I designate which objects I want to save and which I don’t care about. And then in the end the only difference between loading a scene and saving the game to disk would be the saving to disk part. At least that’s how I’m envisioning it at the moment.

Basically a save system requires me to start coding something tailored to my game and that made me hesitant. I didn’t want to go down that path unless I had explored everything Unity offers first. I would feel stupid if there was an obvious alternative approach that I had overlooked. :slight_smile:

But thanks. These are great answers and all in less time than it took me to write the original post. :stuck_out_tongue:

To do what methos5k suggests (storing this stuff in RAM rather than writing it to disk) requires some variables to hold that data. I’d probably wrapper all those variables up in a class… and what the hell… I’d make that class inherit ScriptableObject :slight_smile:

And at that point, you’re like 80% of the way to a save/load system as described on the linked video! Win-win.

For sure, the video (or any other disk saving method) is still required. I only meant that between scenes, actually writing to the disk isn’t required, of course. DontDestroyOnLoad, as mentioned in the first post - but for the scene’s objects - is what I was thinking.
I saw that video, too, a year ago or so. Good video :slight_smile: heh.

It’s working great so far. I feel like I’m using the whole spectrum of tools Unity provide for me now. :stuck_out_tongue:

I’m not using scriptable objects though and I didn’t really understand how or where I would use them from watching the linked video. The first thing he said in that video was that they can’t be used at runtime.

Currently I have all my item data in excel text files and then I generate an item database from that when I start the game, or it’s not really a database, but rather just templates for the various item types which I store in lists, and then the item generator can use those when it generates random items. I may need to expand upon this when I want unique, hand crafted items, but I’m thinking it should work for that as well.

But I’ve managed to add save/load functionality now. It’s very rough and hard coded at the moment, but I like to just throw in features to try and get the result that I want as fast as possible and then refactor it later. That way it feels like 2 separate successes, one when I see it working and one when the code is actually nice. :smile:

I started with the binary serializer that was shown in the Unity video, but wasn’t able to serialize vectors etc. with it so I switched to the XML serializer, and the fact that I can open the save files and see all the values is a huge benefit for me while I’m learning this stuff, so win-win, I guess.

I also ended up using additive scene loading for my dungeon. I’m making each room of the dungeon in a separate scene and using them basically like more complex prefabs, basically exactly how I thought this feature would be used when I wrote the initial post. It’s working great as well and using asynchronous loading makes it look really cool when the level builds itself from nothing around you. :stuck_out_tongue:

But anyway, I can freely move between my town and dungeon now and I can save and load my character. I’m also saving my character when I’m switching scenes because why not? It’s basically like auto-saving. I don’t save items on the ground or vendor inventories etc. yet though. That’s where I have to refactor my current code first, I think.

Thanks for everything. I feel like I’ve expanded my knowledge on how to make a real game quite a bit these past few days. Usually all my projects end well before this stage. I will link between this thread and my WIP thread when I upload my next build so anyone who comes here later can see where I applied this newfound knowledge.