Lost in classes

Okay i have this save function (just classic save function)

    List<ushort> list = new List<ushort>();

    void Save()
    {
        string savefile = Application.persistentDataPath + "/bf.txt";
        int xlength = WC.world.GetLength(1);
        int ylength = WC.world.GetLength(0);

        BinaryFormatter bf = new BinaryFormatter();
        FileStream file = File.Create(savefile);

        Debug.Log(list.Count);
        for (int xpos = 0; xpos < xlength; xpos++)
        {
            for (int ypos = 0; ypos < ylength; ypos++)
            {
                if (WC.world[xpos, xpos] != 0)
                {
                    byte x = System.Convert.ToByte(xpos);
                    ushort y = System.Convert.ToUInt16(ypos);
                    byte b = System.Convert.ToByte(WC.world[xpos, xpos]);

                    list.Add(x);
                    list.Add(y);
                    list.Add(b);
                }
                else
                {
                    Debug.Log("its 0");
                    break;
                }
            }
        }
        Debug.Log(list.Count);

        int position = 0;
        int xa = 0;
        int ya = 0;
        int ba = 0;
        for (int i = 0; i < list.Count; i++)
        {
            if (position == 0)
            {
                xa = list[i];
                position++;
            }
            else if (position == 1)
            {
                ya = list[i];
                position++;
            }
            else if (position == 2)
            {
                ba = list[i];
                Debug.Log("Saved at [" + xa + "," + ya + "] = " + ba);
                position = 0;
            }

        }
        bf.Serialize(file, list);
        file.Close();
        Debug.Log("Saved");
    }

and save list is always empty because every position in int[,] world is 0 aka not set to some number even tho i run this before saving

here is function when creating world

void Draw(int xaxis, int yaxis, int xpos, int ypos)
    {
        if (WC.displayed[xpos, ypos] == null)
        {
            switch (WC.world[xpos, ypos])
            {
                case 0:
                    if (xaxis > -2 && xaxis < 2 && yaxis > -2 && yaxis < 2)
                    {
                        int oreType = WC.GenerateOre(ypos);
                        currentBlock = Instantiate(blocks[oreType], new Vector3(xpos, ypos * -1, 0), Quaternion.identity) as GameObject;
                        WC.world[xpos, ypos] = oreType;
                        WC.displayed[xpos, ypos] = currentBlock;
                    }
                    else
                    {
                        currentBlock = Instantiate(blocks[0], new Vector3(xpos, ypos * -1, 0), Quaternion.identity) as GameObject;
                        WC.displayed[xpos, ypos] = currentBlock;
                    }
                    break;
                case 1:
                    currentBlock = Instantiate(blocks[1], new Vector3(xpos, ypos * -1, 0), Quaternion.identity) as GameObject;
                    WC.displayed[xpos, ypos] = currentBlock;
                    break;
                case 2:
                    currentBlock = Instantiate(blocks[2], new Vector3(xpos, ypos * -1, 0), Quaternion.identity) as GameObject;
                    WC.displayed[xpos, ypos] = currentBlock;
                    break;
                default:
                    break;
            }
        }
}

the script for storing these is here positions

using UnityEngine;
using System.Collections;

public class WC{

    //World size
    static int wheight = 2000;
    static int wwidth = 100;
    public static int[,] world = new int[wheight, wwidth];
    public static GameObject[,] displayed = new GameObject[wheight, wwidth];

    public static int GenerateOre(int ypos)
    {
        if (ypos < 200)
        {
            if (Random.value < 0.50f)
            {
                return 2;
            }
            else
            {
                return 1;
            }
        }
        return 1;
    }

}

I tried with public, public static, static with referncing script and all kinds of things but everytime world is empty aka every field is 0, in second script when setting up world i put debog and it clearly prints that at x,y is block b

iam totaly confused why it so

P.S. I tried runing save function in player script and in saveload script (where it should go)

Man, this seems way off base, it’s partly because I am so confused what you are trying to do. But I would suggest you build all your arrays in the [x, y, z] format. It seems as though you switched xpos, and ypos in a few places. Then your for loop on line 13 in the first script has a few… weirdness things to them. Why are you checking whether [xpos, xpos] is equal to 0? dont you want [x, y] or in your case [y, x]?

The next problem is with the for loop again. The first time the code is ran x is 0 and y is 0. The next time it is ran, x is 1 and y is 1, what about [0,1] and [1,0] you are now just saving in a diagonal?

1 Like