- Home /
RPG Battle Transition
Can anyone tell me how to swap scenes like in an RPG when a battle take place, and reload the previous screen in the same location?
Basically, how do I reload a previous level in the same position as before after swapping to the battle scene?
Answer by Statement · Dec 17, 2010 at 04:59 PM
You need to keep track of the game state and restore the game objects when returning to the previous scene (referring to scene as level). This can become complicated depending the scope of your game. As far as I know, Unity has no way to automatically store the state of a level so it becomes easily reloaded at a later point in time.
You'll have to either
- Use third party libraries that deal with this problem and use them. Search for it. I don't know any.
- Write custom code that allow you to restore the state before you went into battle.
- Find another approach to your battle system.
Another approach would be to have a different place within your scene where battle takes place. Briefly disable the characters in the "main world" while you're in the battle. This might be easier for you to implement. In the end you'll have to make the decision which would fit your game best.
Yes, but it is quite complicated. You have to consider so many different states. It's not only a question about restoring positions, rotations, forces, materials, textures, colors (or what have you) but you also need to restore destroyed items, remove created items. The list grows large. I think a simple solution would be to have your battle take place inside the same scene/level. You could create the "battle" using prefabs if you need to. It would probably be easier than to load different levels and restore state.
You could use several approaches. You could use "reflection". You could use "memento pattern". These in itself are complex enough not to be covered in my answer - use google and find a wealth of information about them. I wrote such a system for Bob Came in Pieces where we needed to store data for each checkpoint. The result wasn't pretty and it was hard work getting it right. Basically every member variable had to be tracked and co-routines were out of question since they held a state I couldn't re/store.
I think I finally worked something out by string the location before the jump.
Answer by Dominicbg · Apr 04, 2015 at 08:48 PM
My solution was to make 2 scene, one for the map and the other for the battle. At the loading of the game we spawn both. Each one have one have their own camera.
I did a Battle Manager, when I do the collision I call this and SetRender(true) the battle/camera and vice-versa.
I also did a SetPreference (with int since bool doenst really work) which save if I'm in battle or not. Now in all my overworld script there's a condition if the Battle is on or not, if yes, nothing work.
//To set my battle
InBatteBool = 1; PlayerPrefs.SetInt("InBatteBool", InBatteBool); PlayerPrefs.Save();
//To test if the battle is on
if (PlayerPrefs.GetInt
("InBatteBool") == 0){}
I don't know if its clear, it's my first time haha, don't hesitate if you have more question!