- Home /
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();
}
}
Answer by Bunny83 · Apr 05, 2011 at 06:27 PM
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 ;)
do you know how to save to an X$$anonymous$$L file? i just need to be able to save my game
Well, that depends on the platform. Web-buids can't write files, but in standalone you can use the xml classes that comes with $$anonymous$$ono: 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?
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?
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
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.
Your answer
Follow this Question
Related Questions
How do to you save time for each player and display it? 2 Answers
How to get WebGL PlayerPref values out of Indexeddb? 0 Answers
displaying playerprefs 1 Answer
playerprefs problem 0 Answers