PlayerPrefs for saving strings

I’m trying out the PlayerPrefs Class with my code for a bulletin board now and I must be doing something wrong. I’m trying to load previously saved strings with OnLevelWasLoaded and then save new strings when the submit button is pressed. I can’t seem to get it to work. I’d really appreciate it if someone could take a look at this.

var stringToEdit: String = "What's The News?";
var customSkin: GUISkin;
var maxMessage = 10;
var messagesTyped: String[];
var messageSpacing: float = 10;
var maxChar : int = 30;
var scrollPosition : Vector2 = Vector2.zero;
var messageAlert : String = "New Message From ";
var nameField: String = "Name";
private var msgCounter: int;

function Start()
{
    maxMessage--;
    messagesTyped = new String[maxMessage];
   
}

function OnLevelWasLoaded(){
    //get input
    loadLevel();
}

function loadLevel(){
    if(PlayerPrefs.HasKey(nameField+ stringToEdit)){
    Application.LoadLevel(PlayerPrefs.GetString(nameField, stringToEdit));
    }   
}

function OnGUI()
{
	GUI.skin = customSkin;	
	
    stringToEdit = GUI.TextField(Rect(270, 100, 450, 80), stringToEdit, maxChar);
    nameField = GUI.TextField(Rect(80, 100, 180, 30), nameField, maxChar);
    if (GUI.Button(Rect(730, 100, 60, 30), "Submit"))
    {
        SubmitMsg( messageAlert + nameField + ":  " + stringToEdit);
        saveMessage();
        
    }
    var x: float = 270;
    var y: float = 180;
    for (var i: int;
    i < messagesTyped.length;
    i++)
    {
        y += messageSpacing;
        //print("Here");
        GUI.Label(Rect(x, y, 500, 60), messagesTyped[i]);
    }
    }
function SubmitMsg(submittedMsg: String)
{
    if (msgCounter == maxMessage)
    {
        msgCounter = 0;
    }
    messagesTyped[msgCounter] = submittedMsg;
    print(messagesTyped[0]);
    msgCounter++;
}

function saveMessage(){
PlayerPrefs.SetString(nameField, stringToEdit);
print("saved");
}
if(PlayerPrefs.HasKey(nameField+ stringToEdit))

probably needs to be

if(PlayerPrefs.HasKey(nameField))

However, you’ll likely get stuck on an infinite level load because you’ll load the level again as soon as it’s loaded.

Damn this is tricky. I just tried your suggestion and now i get an error message.

Level ‘What’s The News?’ (-1) couldn’t be loaded because it has not been added to the build settings.
To add a level to the build settings use the menu File->Build Settings…
UnityEngine.Application:LoadLevel(String)
Status Field:loadLevel() (at Assets/scripts/Status Field.js:26)
Status Field:OnLevelWasLoaded() (at Assets/scripts/Status Field.js:21)

I havent heard anything about adding PlayerPrefs to build settings…or am I not supposed to call ‘Application.LoadLevel’ as part of loading in these strings?

You’re doing two different things here.

First, you’re storing and later retrieving a string from PlayerPrefs. You do that if you want to remember it even when the app is terminated. Seems you got that working now.

Second, you’re loading levels (scenes). You need to explicitly include the scenes you need in the project, otherwise they won’t be exported. You apparently haven’t added the “What’s The News?” scene yet. You include them via File / Build Settings, like the error message tells you.

I see it. Thanks for that.

I was confusing the Application.LoadLevel call with the Player prefs load() function. “whats the news” isn’t a level its a string, but I was mistakenly asking the program to load the string as a level…doh!

Now I’ve got the code without any errors and am getting ‘save’ and ‘load’ printed at the right moments…but the problem remains. After the app is terminated the strings don’t come back though it says thy’ve been loaded!

Confused again haha

http://unity3d.com/support/documentation/ScriptReference/PlayerPrefs.html
Although it should work out of the box

save

cheers but the support documentation was the first place I looked for assistance. I only use the forums when I need things explained properly.
You’d think that out of all of the thousands of posts there would be one decent example of this script in action.

stringToEdit = PlayerPrefs.GetString(nameField, “”);

The above should do it just fine, assuming you’re using nameField with the same value to save it.

This doesn’t seem to be working either. I’m trying to make it so that the nameFIeld and stringToEdit remain after the app has been closed and reopened. So essentially the both strings have to be saved and reloaded. Not sure why I can’t see anything happening even though it is printing ‘saved’ and ‘loaded’ and I have no error messages.

i was actualy pointing you to this page :
http://unity3d.com/support/documentation/ScriptReference/PlayerPrefs.Save.html

Okay, sorry about that. I’m trying this now. I’m creating a save function with PlayerPrefs.Save so I can call it from within the OnGUI but unity says ‘Save’ is not a member of Unity Engine.PlayerPrefs…erm really?

How do you use the command ?

Heres what I have with just the save command.

if (GUI.Button(Rect(730, 100, 60, 30), “Submit”))
{
SubmitMsg( messageAlert + nameField + ": " + stringToEdit);
save();

function Save(){
PlayerPrefs.Save;
}

Still no sure about how loading works with this.

It’s a function : PlayerPrefs.Save ()

if the scene name is saved into playerprefs ( the name like it’s in the build settings) then you simply load a the scene with :
Application.LoadLevel(PlayerPrefs.GetString (“myReference”));

Okay, can you give me an example of how to write the ‘PlayerPrefs.Save’ function because this is all just trial and error here. No matter what I try Unity just doesn’t recognise the function.

tried writing: function PlayerPrefs.save()
but I still get the error from earlier.

The loading makes more sense though, cheers for that.

	private string save1 = "";
	private string read1 = "";
	
	void OnGUI () {
		save1 = GUILayout.TextField (save1, GUILayout.Width (100));
		if (GUILayout.Button ("Save")) {
			PlayerPrefs.SetString ("save1", save1);
			PlayerPrefs.Save ();
		}
		if (GUILayout.Button ("Read")) {
			read1 = PlayerPrefs.GetString ("save1");
		}
		GUILayout.Label (read1);
		if (GUILayout.Button ("Delete key")) {
			PlayerPrefs.DeleteKey ("save1");
		}
		if (GUILayout.Button ("Delete all keys")) {
			PlayerPrefs.DeleteAll ();
		}
	}

You can easily translate to UnityScript

Ah, I’m coding in javascript. Never used C#

well you’ll have to put some effort in it.

Okay PlayerPrefs.Save(); wont work in javascript and I can’t find any reference to an alternative.Back to square one now trying to make ‘SetString’ and ‘GetString’ work. Cheers for trying.