Hello I’m new here. I have years of experience in programming (C, C++, Java, etc) but I’m new to Unity. I want to make a JRPG battle (for now) but before start scripting the battle code I thought that I must script the code to enter the battle.
I tried many ways and I found many old thread in this forum but… I don’t really get it.
What I want to do is load the battle scene and, after the battle, return to the old scene (world, town, dungeon, depends on the moment).
I don’t like the “DontDestroyOnLoad” method, I prefer a Singleton “GameController” that controls everything but if this is not the best way I can use the DDOL.
This is my code so far:
class GameController{
var player : Vector3;
var oldScene : String;
private static var instance : GameController;
private function GameController(){}
static function getInstance() : GameController{
if(instance == null)
instance = new GameController();
return instance;
}
function enterBattle(battleScene : String){
player = GameObject.FindWithTag("Player").transform.position;
oldScene = Application.loadedLevelName;
Application.LoadLevel(battleScene);
}
function returnFromBattle(){
Application.LoadLevel(oldScene);
GameObject.FindWithTag("Player").transform.position = player;
}
}
I know the error but I don’t know how to resolve it. The code execute the position change in “returnFromBattle” before the change of the scene.
This is the question: I know that this is not the real way to make a JRPG game because the battle reset not only the player position and in this way it’s impossible (in easy way) to make the boss (or precalculed) battle. Must I find the way to use “LoadLevelAdditive” to achieve a good scripted game or can I continue this way (solving my error)?
Hello. I don’t really understand what your program does wrong, if it does anything wrong.
Try explain a little more what it is, and i may be possible to help you.
function returnFromBattle(){
Application.LoadLevel(oldScene);
GameObject.FindWithTag("Player").transform.position = player;
}
When the game return from battle to the old scene it wont copy the old position to the player. Probably because it makes the assignation before che complete load of the scene, but I don’t know how to resolve this issue.
Plus the question I made is about the best choice of the script method for the battle transition: a permanent player with “DontDestroyOnLoad”, a GameController singleton (my way) or “LoadLevelAdditive”?
It really depends on what you prefer, as all are viable ways.
A thing you can do is to load levels as you do now in the singleton class, and then save whatever position and other data the player needs in the different loaded levels as PlayerPrefs, XML or SQL. Done this for a game before because the unit needed to enter the same level at different positions, worked well.
Uhm… the entire game switch between scene (town1, town2, townN, worldmap, house1, etc), I don’t want to duplicate the battle scenery in every single Scene, I think it’s… too much work for the computer/device that run the game ^^
Thanks for the advice, probably I’ll keep this configuration using your tips!
I thought you had a different battle scene per world scene, cause that’s usually that case.
But even if it’s the same battle scene everywhere, then you could do something similar:
have the battle scene be loaded at the beginning and have it entirely beeing don’t destroy on load. That way it works just like if it was in every scene.
I do something similar, and it works great IMO