C# - A little help with arrays please!

I need an array that represents a coordinate grid, I think. Basically I need it to store terrain types at various coordinates. I was thinking something like, wilderness[1][1] = “forest”, wilderness[5][10] = “mountain”, you know? Where the first number is the Y coordinate and the second is the X. But I’m not sure how to build such a grid in C#, or honestly where to put it in my unity project (I do have a persistent GameObject with DontDestroyOnLoad, I’m fairly sure that’s where I want this?).

Tbh, I’m not positive this is the best way to do what I want, either. I want to use this to take my current coordinates, receive a function with X,Y displacement, and determine the terrain type to use to create the space I’m stepping into. If there’s an easier way to do this, please let me know that too!

I’ve been looking around online but haven’t been able to find what I’m after. I’m brand-brand new to C# - like started learning yesterday new - so please treat me like a four year old!

That’s a jagged array, in your example. That sounds perfectly okay to me.

I want to say this as a note, though… if you’re brand, brand new… you will learn a lot (and benefit) from using some tutorials for Unity - examples of parts of Unity, scripting, and even some example games, too. :slight_smile:

Thanks for your help! I did do several of the tutorials last night and wanted to get into working through some of my own ideas today :slight_smile:

Using the magic words ‘jagged array’ I was able to come up with this:

    string[][] wilderness = new string[][] {
        new string[] {"grass", "dirt", "path"},
        new string[] {"dirt", "path", "grass"},
        new string[] {"path", "grass", "dirt"}
    };

I also made a second array (called tiles) in which to store existing tiles (so I don’t re-create ones that are already there). Tiles is a list of scene build index numbers with x,y coordinates stored for them. So tiles[1] = {1, 1} for example. Now I’m working on a function that is given a coordinate displacement and that I want to move me between tiles. So far I have:

    public void ChangeScene(int x, int y) {
        curscene = SceneManager.GetActiveScene ().buildIndex;
        oldx = tiles [curscene] [0];
        oldy = tiles [curscene] [1];
        newx = oldx + x;
        newy = oldy + y;
        ...
    }

Now I want to check if I have anything in the tiles list that matches my new coordinates (newx, newy). I ran into another hurdle here as I haven’t been able to find online anything about checking if one array contains exactly another array. In this case, if (newx, newy) is in tiles, but specifically in that order (I don’t want it to match (newy, newx) for example).

Try something like this, if it’s what you meant? Sample jagged array lengths., C# - rextester

I was basing that above sample on a guess that you wanted to be able to safely check if an index exists. Maybe I misunderstood, though…

Please clarify if I’m a little lost :slight_smile:

Hmm, it looks like this example is checking length. I’m storing scenes in the tiles array by buildIndex. Important related question - if I build, say, 6 scenes, and then delete one through the SceneManager while playing, does it change the buildIndex of the scenes built after it, or do they remain and the next scene created assumes the ‘lost’ index?

For example, if I have scenes 0, 1, 2, 3, 4 and I delete scene 1, do 2, 3, 4 become 1, 2, 3 respectively?

If this is the case, then your example is probably what I need. But if not, I think I need something like this, only nicer if at all possible:

        newScene = 0; // buildIndex 0 is the main menu, we can use this as a placeholder
        foreach (int element in tiles) {
            if (tiles [element] [0] == newy && tiles [element] [1] == newx) {
                newScene = element; // We found a match! Save the buildIndex
            }
        }
        if (newScene == 0) { // We didn't find an existing scene at these coordinates
            ...
        }

I see what you’re saying. I was wondering if I got that a little mixed up in my example. There’s nothing really wrong with your loop there to check the scenes, in my opinion. As for the behaviour of the scene index list, I’d suggest just trying it (I don’t know the answer). Try running a small bit of code that loads up a few scenes, and removes one while active and print them out and/or the index count… :slight_smile:

And yes, my code was to make sure you didn’t go out of bounds, but I was mixed up, as you’re not using those for the indexes.

Are you sure you want a jagged array? A 2D array is normally simpler to work with.

I’m not positive, no, but it’s working okay so far. I tried a bunch of different ways to fiddle around with the tiles array, and eventually settled on basically replicating the wilderness one and setting all the values to 0. So what I want this to do now is determine whether or not we currently have a scene at a given coordinate, then move to that scene if so, and create a new one to move to if not. Here’s what I have so far:

    public void ChangeScene(int x, int y) {
        myScene = SceneManager.GetActiveScene (); // Get the current scene
        curscene = myScene.buildIndex; // We'll need the buildIndex
        curname = myScene.name; // Scenes where this function might run are named 'x.y', so for example 1.2 or 5.3
        string[] coords = curname.Split('.'); // Convert the scene name to its x and y coordinates
        oldx = int.Parse (coords [0]);
        oldy = int.Parse (coords [1]);
        newx = oldx + x; // Determine where we're moving to
        newy = oldy + y;
        newScene = 0; // buildIndex 0 is the main menu, we can use this as a placeholder
        if (tiles[newx][newy] == 0) { // We didn't find an existing scene at these coordinates
            string xstr = newx.ToString ();
            string ystr = newy.ToString ();
            string sceneName = xstr + "." + ystr; // Piece together the scene name
            Scene createdScene = SceneManager.CreateScene (sceneName); // Make the new scene
            tiles [newx] [newy] = createdScene.buildIndex; // Record that we have one here now
            newScene = createdScene.buildIndex; // Ready to move to the new scene!
        }
        else { // We did find an existing scene, line it up!
            newScene = tiles[newx][newy];
        }
        SceneManager.LoadScene (newScene, LoadSceneMode.Single); // Move into the next scene, old or new
    }

This all works EXCEPT the part that makes new scenes! It can tell if I have an existing one, and moves me there. And it knows when it needs to make a new scene. But when it goes to do so, it throws an error about the scene name being an empty string and the build index being -1. I looked into the editor version of creating a new scene, but that one didn’t like running at all and said to use this one instead. So I’m a bit at a loss on what to do from here. Any help please?