x


PlayerPrefs script problem

Hi I've been at this all day an have gotten nowhere. I have a script for a message system and I've decides to use PlayerPrefs to save and load messages so they remain after the game has been closed and reopened. I get no script errors but for some reason when I close and open the app, posted messages are not reloaded. If anyone can tell me what's wrong with what i've done I'd be so grateful.

Here's the javascript code I've been working on:

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];
    GetMessages();
}

function GetMessages(){
if (PlayerPrefs.HasKey(nameField) == true)
{
PlayerPrefs.GetString(nameField, "Name");
print("loaded");
}
}

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);
        SaveString();
    }
    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 SaveString(){
PlayerPrefs.SetString(nameField, "Name");
print("saved");
}

Thanks again!

more ▼

asked Aug 17 '11 at 07:37 PM

blacksuit_films gravatar image

blacksuit_films
1 7 7 8

Without looking at your code yet, you do know there is a limit to how much you can store prefs, right? Is it possible you've gone beyond that limit?

Aug 17 '11 at 07:41 PM DaveA

It's okay I' know that there is a 1MB limit. I have had no results as yet with this code. So far I have only used 4KB of that space

Aug 17 '11 at 07:53 PM blacksuit_films
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

PlayerPrefs.GetString returns the value from PlayerPrefs.

See http://unity3d.com/support/documentation/ScriptReference/PlayerPrefs.GetString.html

Fixed code:

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];
    GetMessages();
}

function GetMessages(){
if (PlayerPrefs.HasKey(nameField) == true)
{
nameField = PlayerPrefs.GetString("Name");
print("loaded");
}
}

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);
        SaveString();
    }
    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 SaveString(){
PlayerPrefs.SetString("Name",nameField);
print("saved");
}
more ▼

answered Aug 17 '11 at 07:57 PM

Mortennobel gravatar image

Mortennobel
1.8k 10 15 34

GetString is currently in my code already within function GetMessages.

Aug 17 '11 at 08:04 PM blacksuit_films

But you are not using what it returns. It should be something like: var result: String; result = PlayerPrefs.GetString(nameField);

Also - it does not make sence to use the second parameter (default value) when you check that the item do exist.

Aug 17 '11 at 08:09 PM Mortennobel

Another issue - you are always saving the constant "Name" - that looks like a bug. I think you have swapped the parameters. Take a look at the documentation. (Read what the parameter are called)

Aug 17 '11 at 08:13 PM Mortennobel

Okay, I sortof see what you mean. I think my main misunderstanding here is the var and string at the beginning of your proposed code. what would I put in as the result for the var and string?

Apologies but filling in the blanks isn't something I'm good at.

Aug 17 '11 at 08:14 PM blacksuit_films

I don't understand the documentation theres not enough info for a newbie. The only way for me to learn this properly is to see it done but there are no tutorials.

Aug 17 '11 at 08:19 PM blacksuit_films
(comments are locked)
10|3000 characters needed characters left

Gotcha... It says loadedName. I really appreciate you taking the time to help me here.

more ▼

answered Aug 17 '11 at 08:46 PM

blacksuit_films gravatar image

blacksuit_films
1 7 7 8

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5060
x436
x434
x418
x294

asked: Aug 17 '11 at 07:37 PM

Seen: 941 times

Last Updated: Aug 17 '11 at 09:00 PM