- Home /
How can I make this script save and or load what I do in my game??
I need help, I am trying to just make a single player RPG very low quality and kinda similar to the older runescape. I can't seem to figure something out although. Right now I wrote a script so if I type in the AccountID field, "admin". And the Password field, "admin". And click "LogIn" Then it will do what I tell it. It works, I tested it and when I click Log in it replys to the console "ID & Password Are Correct." (I know how to Application.LoadLevel and all that) But I don't know how to make it to where while playing the game it's saving everything that changes when my XP goes up, if I pick up some new items in the game. So when I go back to the game, type in my info and click log in it loads the players saved/stored information ie; items, experience, etc. How would I go about doing this? Here is my current code for the part mentioned above, i left out a couple other parts like the other gui buttons, this is just an example.
PS. I don't care if i have to make more scripts than just one (Im sure I can't get what im wanting to do into just a single script.
var stringToEdit : String = "AccountID";
var passwordToEdit : String = "My Password";
function OnGUI
{
// Make a text field that modifies stringToEdit.
stringToEdit = GUI.TextField (Rect (260, 240, 136, 20), stringToEdit, 25);
// Make a password field that modifies passwordToEdit.
passwordToEdit = GUI.PasswordField (Rect (266, 262, 138, 20), passwordToEdit, "*"[0], 25);
if (GUI.Button (new Rect(288,340,98,22), "Log In")) //If "Log In" button is pressed.
{
if(stringToEdit == "admin" && passwordToEdit == "admin") //If The text field is equal to "admin" and the password field is equal to "admin".
{
print("ID & Password Are Corect.");
//DoSomethingAlso
}
}
}
Answer by BLarentis · Aug 23, 2012 at 03:54 PM
Hello again, it'll be something like this:
using UnityEngine;
using System.Collections;
public class Inventory : MonoBehaviour {
public string[] itens = new string[20];
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void saveInventory(){
//Lets consider that you have all your inventory listed inside a array (created at the start of the class)
//The array size is the size of slots of your inventory
//What you will have to do is to save all those itens inside the player prefs so you can access them every time that
//need
//It will be something like this
for (int i = 0; i < itens.Length; i++) {
//Here i create the name of the key
string inventorySlot = "InventorySlot";
//And here i change the name so it will access all the positions
inventorySlot = inventorySlot + i.ToString();
//And here i save to PlayerPrefs
PlayerPrefs.SetString(inventorySlot,itens[i]);
//Just remember that your array start to count on 0.
//So if you have 20 positions, those positions will be 0-19
//So the first slot will be InventorySlot0
//The second will be InventorySlot1, and so on untill InventorySlot19
}
}
}
Oh okay ( i use javascript but you explained it really good so It makes just as much since. Thanks for the help
Answer by BLarentis · Aug 22, 2012 at 09:56 PM
Hello, my advice for you is to study this:
http://docs.unity3d.com/Documentation/ScriptReference/PlayerPrefs.html
This will create a file inside your application files, that will never be erased, unless you erase it by code or manually. Because of that you can use those files to save all your characters infos. Take care.
Yes, those help alot I see how I can use them to store experience but how can I use them to save the Items in my inventory or bank in the game tho?
You can use the SetString method. And use a key like: PlayerPrefs.SetString("InventorySlot1","Item1"); PlayerPrefs.SetString("InventorySlot2","Item2"); and so on ... Then when you load your game you use a for to check all the Inventory $$anonymous$$eys. So you don't need to check player prefs all the time you open your inventory.
Can someone write me an example script? I see that there are a couple different ones, but one is "Save" in order for them to work do they need the Save included in the script?
As soon as i finish the sample script i´ll post in here.
Your answer
Follow this Question
Related Questions
Save and Load a Game 1 Answer
Loading a list into unity 0 Answers
Is it possible to interact with a USB Drive? 0 Answers
saving data 1 Answer
How do you copy PersistentDataFolder between devices? 2 Answers