x


Loading with ArrayPrefs

what im trying to do is add a save load feature into my game, i want to save the location fo all the cubes in the game. i'm using the arrayprefs script from http://www.unifycommunity.com/wiki/index.php?title=ArrayPrefs. i have my save function working but i cant figure out how to load the game from my main menu scene. Here is my load function. this script is in my main menu.

function loadGame()
    {
        Application.LoadLevel("start");
        for (i=0 ; i < button2.cubes.length ; i++)
        {
        curName = button2.cubes[i];
        var go = Instantiate(cube2, PlayerPrefsX.GetVector3(curName), Quaternion.identity);
            go.name = curName;
            Debug.Log("lOADED!");
        }

    }

button2 is my saving script is in my first level, here is what the save function looks like for that level.

static var cubes = new Array();
var cubeCount = 0;
function Update()
{
if (Input.GetMouseButtonDown(0))
            {


            var go = Instantiate(cube2, transform.position, Quaternion.identity) as GameObject;
            go.name = "cube"+cubeCount;
            cubes.Add("cube"+cubeCount);
            cubeCount++;
            }   

if(Input.GetKey("m"))
            {
            saveGame();
            Debug.Log("saved");
            }

}

function saveGame()
    {


        for (i=0 ; i < cubes.length - 1 ; i++)
        {

        curName = cubes[i];
        PlayerPrefsX.SetVector3( curName, GameObject.Find(curName).transform.position);
        }

What am i doing worng?

EDIT**

here are my new scripts.

This is my CubeManager script that is attached to empty gameobjects in the first level and the main menu.

static var cubes = new List.<Transform>();
var cube2 : GameObject;

function CreateCube(position : Vector3) : Transform
{
    var cube : GameObject = Instantiate(cube2, position, Quaternion.identity);
    cube.name = "cube" + cubes.Count;
    cubes.Add(cube.transform);
}

function SaveCubes()
{
    PlayerPrefs.SetInt("button2.cubeCount", cubes.Count);
    for(var T : Transform in cubes)
    {
        PlayerPrefsX.SetVector3(T.name,T.position);
    }
}

function LoadCubes()
{

    Application.LoadLevel("start");
    cubes.Clear();
    var cubeCount = PlayerPrefs.GetInt("button2.cubeCount");
    for(var i = 0; i < cubeCount; i++)
    {
        CreateCube(PlayerPrefsX.GetVector3("cube"+i));
    }

Here is my script that is in my first level to save the cubes.

function Update()
    {

    var managerScript : CubeManager = FindObjectOfType(CubeManager);

        if (Input.GetMouseButtonDown(0))
            {managerScript.CreateCube(transform.position);
            }   
        if(Input.GetKey("m"))
            {

            managerScript.SaveCubes();
            Debug.Log("saved");
            }
}

Here is my script that is attached to my load button on the main menu to call the load function:

function OnMouseUp()
{

var managerScript : CubeManager = FindObjectOfType(CubeManager);
    if(isQuit)
        Application.Quit();
    else
        {

        managerScript.LoadCubes();

        }


}
more ▼

asked Apr 05, 2011 at 05:39 PM

austen 2 gravatar image

austen 2
1 12 8 12

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Are you saving your cubes array as well? If not how can you iterate through the array if it's empty ;)

Never used PlayerPrefsX yet, so i can't help you much but to get the cude count back you just have to save it. Since your cube names are just the index you can regenerate the pref-name with "cube" + i. If you want to restore the complete state, don't forget to push your cubes into your cubes array at loading.


edit

static var cubes = new List.<Transform>();

function CreateCube(position : Vector3) : Transform
{
    var cube : GameObject = Instantiate(cube2, position, Quaternion.identity);
    cube.name = "cube" + cubes.Count;
    cubes.Add(cube.transform);
}

function SaveCubes()
{
    PlayerPrefs.SetInt("cubeCount", cubes.Count);
    for(var T : Transform in cubes)
    {
        PlayerPrefsX.SetVector3(T.name,T.position);
    }
}

function LoadCubes()
{
    cubes.Clear();
    var cubeCount = PlayerPrefs.GetInt("cubeCount");
    for(var i = 0; i < cubeCount; i++)
    {
        CreateCube(PlayerPrefsX.GetVector3("cube"+i));
    }
}

!!not tested!!
I just wrote that from scratch.


edit2

If this script is called "CubeManager" you can use this from any other script as long as the CubeManager is attached to a GameObject in the scene:

var managerScript : CubeManager = FindObjectOfType(CubeManager);

managerScript.LoadCubes();

ps. fixed a typing mistake the second SaveCubes should be LoadCubes of course ;)

more ▼

answered Apr 05, 2011 at 06:27 PM

Bunny83 gravatar image

Bunny83
72.9k 23 90 315

do you know how to save to an XML file? i just need to be able to save my game

Apr 05, 2011 at 11:53 PM austen 2

Well, that depends on the platform. Web-buids can't write files, but in standalone you can use the xml classes that comes with Mono: http://www.go-mono.com/docs/index.aspx?link=T%3aSystem.Xml.XmlWriter I haven't used it yet so i can't go in detail here, but why don't you use the PlayerPrefs(X)? I can post an example if you want. You need only the positions of the cubes, right?

Apr 06, 2011 at 12:21 AM Bunny83

im trying to use the LoadCubes function outside of my scene (in the main menu), so i can load from the main menu. but when i try to make the createcube function static, it gives me an error because cube2 is not satic but if i make it static i cant assign a prefab to it. how do i load frorm the main menu?

Apr 06, 2011 at 01:52 PM austen 2

To what object is that script attached to? All that stuff like Createcube / saveing / loading and storing is a global task so you should place it on a manager object that is already in the scene

Apr 06, 2011 at 01:58 PM Bunny83

If you have this script somewhere in the scene you just have to find the GameObject and use GetComponent to get your script reference. Then you can call any public function. If there is only one instance of this script you can use FindObjectOfType to get the script reference directly. I can add an example to my answer.

Apr 06, 2011 at 02:05 PM Bunny83
(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:

x5846
x2046
x617
x470

asked: Apr 05, 2011 at 05:39 PM

Seen: 1170 times

Last Updated: Apr 06, 2011 at 03:49 PM