Home Technical Talk

Scripting help

Hey guys, I know this isn't technically a programming forum, but I think what I'm looking at is pretty generic in scripting terms; also pretty basic stuff... needless to say I'm struggling to grasp the concept right now.

So I'm working on a little card-flipping game to get the hang of both Unity and programming.

Along with some companion books and Unitys script reference, I'm doing ok, but some of this stuff is seriously difficult for me to understand at first.

I just want to make sure I'm understanding this properly. The subject in question; nested arrays.

Here's a bit of the code I'm working on:
function Start() {
             playerCanClick = true;
             aCards = new Array();
             aGrid = new Array();
             aCardsFlipped = new ArrayList();

             for (i=0, i<rows, i++) {
                   aGrid [i] = new Array();

             for (j=0, j<cols, j++) {
                   aGrid [i] [j] = new Card();
                 }
             }
}

Now, I just want to make sure I'm understanding this properly before I move on.

Is this code saying something like this:

'Create a new array in the first 4 slots of 'aGrid', then for each slot in aGrid, add a further 4 'cards'...

So if 'i' is 1 then add 4 cards into aGrid[1], then 'i' is 2 and we put 4 cards into aGrid[2] etc etc.

Is that pretty much it or am I wrong?

If someone has a nice way of understanding arrays/nested arrays then I'd be glad to hear it!

Thanks guys

Replies

  • commander_keen
    Options
    Offline / Send Message
    commander_keen polycounter lvl 18
    Whats with the "aCardsFlipped = new ArrayList();"? afaik ArrayList is only used in c# in place of unityscripts Array.

    The logic of your code seems correct, but it wont work. You must first Add() an element to the array before trying to access it, like this:
    function Start() {
      playerCanClick = true;
      aCards = new Array();
      aGrid = new Array();
    
      for (i=0, i<rows, i++) {
        aGrid.Add(new Array());
    
        for (j=0, j<cols, j++) {
          aGrid[i].Add(new Card());
          aGrid[i][j].foo = "ohai";
        }
      }
    }
    
  • Ben Apuna
    Options
    Offline / Send Message
    What you have here is a two dimensional array. Arrays can also be called "tables".

    With two dimensions you can visualize it just like a spreadsheet which has rows and columns where each row/column combination will point to a specific cell.
    j1 j2 j3 j4 j5 and so on...
    i1__|__|__|__|__
    i2__|__|__|__|__
    i3__|__|__|__|__
    i4__|__|__|__|__
    i5__|__|__|__|__
    and so on...
    
  • Tom Ellis
    Options
    Offline / Send Message
    Thanks guys;

    Ben - ah that helps a lot, looking at it in visual form makes immediate sense.

    Keen - Thanks. The code builds fine, but if I understood your comment correctly, I think I know why you assumed it wouldn't work. There's a fair bit of code I didn't show, I just showed what I was having problems with.

    I've created a custom class called Card as an extension of System.Object, so calling Card() in this case refers to that and adds a new card in the array slots.

    With regards to ArrayList I'm not sure why it's there yet, I'm following some example code to create my project and it hasn't been addressed. From what I understand though, ArrayList is a C# code as you say but I think it can be run in JS anyway.
Sign In or Register to comment.