- Home /
Simple save/load scripting for multi level iOS game.
I understand (and have read) the many posts for saving and loading PlayerPrefs for iOS games but I think my problem is a little different especially because I have multiple levels in my game and I write in JavaScript.
I have 5 levels in my game and I'm trying to have a save game button during the course of the game and a load game button on the main menu. This is what I have so far: I created a small GUI Texture image of a small floppy disk in the upper right corner of all the level's screens and I use the OnMouseDown command to activate it but I don't know how to save the level and the position of the first person character when that icon button is pressed so that when the game is completely exited you can load it back to that spot when you select the load button on the main menu screen (of which I have the button working but I don't know how to script the load to get back to the last saved position).
I don't need to worry about anything else but the level and X,Y, and Z position of the player for that level.
Any help to write or suggestions as to where I can find the info to write the JavaScript I'm trying to write would be GREATLY appreciated.
Thanks so much in advance!!!
Tom
@asafsitner, I read the link that you posted but it doesn't give me enough info to write my script. I'm sure that the script would be really simple, it's just that I can't seem to get all the parts together that I need. For example, I'm sure that I need to make a variable for the level but which would be better; a string for the level name or a float for the level number? Then, is the load just the reverse of the save? Thanks for the suggestion.
Answer by Julien-Lynge · Feb 17, 2012 at 04:51 PM
You didn't specifically ask any questions, so I'm going to address some of the things you mentioned. playerTransform is the transform of the player, which you'll have to figure out how to get (check out the GameObject.Find* methods).
Save the level: PlayerPrefs.SetInt("SavedLevel", Application.loadedLevel);
Save the player position: PlayerPrefs.SetFloat("PlayerX", playerTransform.position.x);
Same for y and z
Then, to load your saved data:
if (PlayerPrefs.HasKey("SavedLevel"))
Application.LoadLevel(PlayerPrefs.GetInt("SavedLevel"));
etc.
I understand that I just stated that I had trouble and didn't ask a question. Sorry. Basically, my question is, How do I write a save and a load script. You gave me some ideas in your answer but they don't seem to be working for me. Here is my script for when the player touches the save icon on the screen:
function On$$anonymous$$ouseUp () { // This is for saving the level that the first person character is on. PlayerPrefs.SetInt("SavedLevel", Application.loadedLevel); // These three are the location as to where the first person character is on the above level. PlayerPrefs.SetFloat("PlayerX", playerTransform.position.x); PlayerPrefs.SetFloat("PlayerY", playerTransform.position.y); PlayerPrefs.SetFloat("PlayerZ", playerTransform.position.z); // This is just a dialog to be sure that I touched the save button. Debug.Log ("I touched the save icon"); }
And here is the script that I am trying to use for the Load button on the main menu when the player starts the game:
var touchRange: float = 100.0f;
function Update () { for (var touch : Touch in Input.touches) { if (touch.phase == TouchPhase.Began) { //Here you have the touch's position so, This is where you have to create a ray using ScreenPointToRay var userRay = Camera.main.ScreenPointToRay(touch.position); //This is to store the RayCasting results var info : RaycastHit; //[Physics.RayCast][2] actually casts the ray and if it returns true it "Hit" something if(Physics.Raycast(userRay, info, touchRange)) { //This tells us if the thing it hit was a "Door". if(info.transform.CompareTag("InstructionsTag")) { // This is where I am putting the change level info if (PlayerPrefs.Has$$anonymous$$ey("SavedLevel")) Application.LoadLevel(PlayerPrefs.GetInt("SavedLevel")); else //I'm using the following just in case the player taps the load button and nothing had been saved. Application.LoadLevel ("Level1"); Debug.Log ("I touched the Instructions Button!"); } } } } }
Could you please tell me what I am doing wrong and how I may fix it so that I may have a simple save and load for my iOS game.?
Thanks so much!!! I really appreciate any help you may offer.
$$anonymous$$
I understand that I just stated that I had trouble and didn't ask a question. Sorry. Basically, my question is, How do I write a save and a load script. You gave me some ideas in your answer but they don't seem to be working for me. Here is my script for when the player touches the save icon on the screen:
function On$$anonymous$$ouseUp () {
// This is for saving the level that the first person character is on.
PlayerPrefs.SetInt("SavedLevel", Application.loadedLevel);
// These three are the location as to where the first person character is on the above level.
PlayerPrefs.SetFloat("PlayerX", playerTransform.position.x);
PlayerPrefs.SetFloat("PlayerY", playerTransform.position.y);
PlayerPrefs.SetFloat("PlayerZ", playerTransform.position.z);
// This is just a dialog to be sure that I touched the save button.
Debug.Log ("I touched the save icon");
}
And here is the script that I am trying to use for the Load button on the main menu when the player starts the game:
var touchRange: float = 100.0f;
function Update () {
for (var touch : Touch in Input.touches) {
if (touch.phase == TouchPhase.Began) {
//Here you have the touch's position so, This is where
you have to create a ray using ScreenPointToRay
var userRay = Camera.main.ScreenPointToRay(touch.position);
//This is to store the RayCasting results
var info : RaycastHit;
//[Physics.RayCast][2] actually casts the ray and if it returns true it "Hit" something
if(Physics.Raycast(userRay, info, touchRange))
{
//This tells us if the thing it hit was a "Door".
if(info.transform.CompareTag("InstructionsTag"))
{
// This is where I am putting the change level info
if (PlayerPrefs.Has$$anonymous$$ey("SavedLevel"))
Application.LoadLevel(PlayerPrefs.GetInt("SavedLevel"));
else
//I'm using the following just in case the player taps the load button and nothing had been saved.
Application.LoadLevel ("Level1");
Debug.Log ("I touched the Instructions Button!");
}
}
}
}
}
Could you please tell me what I am doing wrong and how I may fix it so that I may have a simple save and load for my iOS game.?
Thanks so much!!! I really appreciate any help you may offer.
$$anonymous$$
$$anonymous$$,
I don't have time at the moment to look at your code, but a quick note:
playerTransform is something you need to define - it tells the code where to look to find the player position, rotation, etc. Perhaps define it in your Start function, something like this:
var playerTransform : Transform;
function Start()
{
//here is where you define the transform for the player
//look at the various find functions here (http://unity3d.com/support/documentation/ScriptReference/GameObject.html)
//and choose the one that works best for your case
//this should work if your player GameObject / Transform is name 'Player'
playerTransform = GameObject.Find("Player");
}
Apologies if that doesn't work out of the box - I'm a C# programmer and don't often work in JS.
alternatively, if you put the script within the actual player (attach it as a component), then you can just use 'transform' - that always means 'the transform I'm attached to', so for instance
PlayerPrefs.SetFloat("PlayerX", transform.position.x);
Here's a bit of reading on transforms: http://unity3d.com/support/documentation/Components/class-Transform.html http://unity3d.com/support/documentation/ScriptReference/Transform.html