Proper way to load 2d array?

Easy Save 2 has been replaced by Easy Save 3, so is no longer supported.
Locked
ShaneS429
Posts: 3
Joined: Sun Feb 10, 2013 12:24 am

Proper way to load 2d array?

Post by ShaneS429 »

I posted a while back about using playmaker and how to properly save large amounts of data.

Recently I've decided to drop playmaker just due to the lag it was causing on mobile devices. I'm learning how to code everything by hand now and have just gotten to the point where I need to set up ES2.

As of right now, my game can be thought of as a rpg. There are various characters, weapons, and items. Each of these have various stats, unlock/upgrade costs, etc. I've been toying with saving/loading these into a 2D array as shown on the documentation. While this works fine, I'm just not sure on my execution.

On my Weapon Loadout scene for example, I made an empty object that holds a script to load a saved 2d array and stores it into a public string array called UnlockStatus. The array is something like:

[Pistol] [Unlocked]
[Minigun] [Locked]
[Sword] [Locked]

The menu is made up of various sprites that needs to read the saved data and check if it is locked or unlocked to display the proper graphic.

Right now, in the code for each of my menu items I have it like this:

Code: Select all

public class WeaponCardUpdater : MonoBehaviour 
{
	private tk2dSprite mySprite;
	public string Weapon;
   //Default Value is Locked
	private string UnlockStatus = "Locked";
	public GameObject DataObject;
	
	// Use this for initialization
	void Start () 
	{
		mySprite = GetComponent<tk2dSprite>();
		Weapon = DataObject.GetComponent<SaveLoadGameData>().stringArray[0,0];
      UnlockStatus = DataObject.GetComponent<SaveLoadGameData>().stringArray[0,1];
	}
	
	// Update is called once per frame
	void Update () 
	{
		mySprite.SetSprite(Weapon + " " + UnlockStatus);
	}
}
I'm using 2D toolkit which is what some of that code is. But basically, I made a GameObject variable, set it to the empty object in the scene that Saves/Loads the ES2 files, get the component of that object that is doing the save/load, and then get the appropriate array index to check for the weapon name and unlock status.

I have no idea if this is how I should be going about doing this.

Is there a way to directly load a certain index of an array from a file, or do I have to load said array into a variable first, and then get the proper index from the variable.

Also is referencing another gameobject that is doing all the saving/loading correct? Another way I could think of doing this is having each object on the scene load the file in order to get that array, but then that would mean I would have 20+ objects all calling the load function at once. I'm not sure if that would be a good route to go.

Sorry for the newbish questions :(


Edit:

Another way I thought of is to just have each weapon object save and load individual variables all with different tags instead of using an array. Although that seems more wasteful maybe?
User avatar
Joel
Moodkie Staff
Posts: 4852
Joined: Wed Nov 07, 2012 10:32 pm

Re: Proper way to load 2d array?

Post by Joel »

The simplest way in your case is indeed to have a separate scripts which handles saving and loading, rather than getting each individual object. Also you are correct in saying that you will have to save and load the entire array. Easy Save 2 is very fast, so I wouldn't worry about performance.

Something which might be useful to you are static variables and methods. However, because this isn't to do with ES2 and is more to do with coding in general, I can't provide any more help than that.

All the best,
Joel
ShaneS429
Posts: 3
Joined: Sun Feb 10, 2013 12:24 am

Re: Proper way to load 2d array?

Post by ShaneS429 »

Ok thanks, sounds like I'm going in the right direction.

I just need to make sure I'm going about referencing another object correctly and I'm good to go.

Thanks again!
Locked