Onwards
|
Saving in Unity!! This is going to be easy, no problems at all! Right? Surely it could be easy Since I mentioned it in the last post I just had to continue with creating an saving function, it seemed like a very intriguing task especially since I had never done such a thing in Unity before and not really in any other kind of engine (self-made ones included). So making this would be a great step in the process of creating games, if one wants to make a life creating games understanding this is quite essential. Most game needs some way to keep track of its progress, be it a player and its stats or a story and the parts of its narrative, so having some sort of basic understanding of what is required to make it work would be very helpful. Yes lets gain some experience in this field.
OK, our game would eventually need a way to save progress across levels and containing stats, skills and choices (since it an rpg, interaction with npcs should be considered), though that is in the future so lets focus at the current goal of the game. It is going one level in our demo version so making a system that saves your level progress seems a bit unnecessary, and though it would have been fun to make it will have to wait. For now it is going to be a saving system that utilises save-points across the level where if the player enters one it will save the players data and that of the enemies, would look pretty weird if entities that can be interacted with is not saved, enemies that had not been killed yet are gone or suddenly chasing the player even though not inside a distance that would aggravate them. So I decided to start of with copying the players data into another gameobject, that way I could simply switch between the two when loading and saving (copy from and to), this when a player entered a saving point, Ahh we need to make some saving points as well.
Hmm, a basic saving point in this case is just a hit box that the player can walk into, though it needs a gameobject so it can have a transform and have scripts attach to it, so that it can be moved around, have different rotations and scales as well as having a simple way to distinguish each area both in the editor and in scripts. Creating a gameobject with a trigger collider and a script that is meant to communicate with the main saving script, when the player enters an area a callback function is called which is connected to the main script and it can decide what to do next, I wanted to simply copy the player information into another player object or empty object. This was not quite possible in the way I wanted since if setting another object to the current just makes it point to the same memory address (though in c# that is not really visible or controllable) so it is still connected to the player instance and will change with it which was not quite what I wanted, another alternative would be to have another instantiated player object and just copying the values that are needed, but if doing that I might as well get started with a real saving function that saves the data outside of the engine, in a text document (which allows one to avoid having an extra instantiated player and considering data involved it is a good thing). Moving on to researching how to save in Unity a solution was eventually produced (seems to be a lot of ways to do it and a bit complicated). To save data in unity one has to use a class that is serializable, which makes it so that Unity can process the data of the class and transfer it to and from a file (a serializable, class or object could as such be saved as an asset), besides this each type of data used needs to be serializable (obviously, though a bit unclear which data types are since it says that vector3 and the like should be in unity5 but that does not seem to work for me) so all the basic types are available but others are not certain, but since one can do practically anything with the basic types that is alright (though would be simpler if it worked immediately with vector3 than writing your own version). So I wrote a simple class containing what I wanted to save from the player character, hp, name, and ID, any more is unnecessary at the moment since all other is data held by the prefab and current object that would still be the same and not something the file need to contain. The name and Id are just to check if the player is really the right one. When loaded the player will be put at the centre of the current spawn point, with its rotation, and in an idle mode so things like speed and rotation is not needed (in a system where one can save whenever one wants it is needed along with other variables). This concludes the first part of the saving system, a couple of zones and some of the players data being saved is a working system though one that is incomplete. It will appear a bit weird if it is only the player that is re-spawned, there should be other options. |

