Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Syntax for multi-dimensional arrays?

I am sure this is simple, but I don't get it.

How do I obtain the data from an array, that is in an array?

I've tried 'array 
 
That is because Ruby does not include multidimensional arrays. Instead, we have the table class. And, as far as I remember, that would be how you get data out of it. Check out "Table" in the RMXP or RMVX help file.

Also, it looks like the array you're trying to pull data from has not been initialized. Just a thought.
 
If I recall correctly, Tables can only store signed integers. If you want to store other objects, you can make your own multi-dimensional array this way:

Code:
 

def init_multi_array(width) #Multi-Dimensional Array Creation

    arr = []

    for i in 0...width

        arr[i] = []

    end

    return arr #Return multi-array

end

 

multi_arr = init_multi_array(10) #create a multi array with a width of 10

 

multi_arr[3][4] = "foo" #this will work to set

multi_arr[3][4]  #=>"foo"

 

multi_arr[12] = "bar" #Note that this will work, because the first dimension of this multi array is just an array

multi_arr[12][3] #This will return an out of bounds exception (Since you just set multi_arr[12] to "bar" and there is no 4th character)

 

multi_arr[17][2] #This will get you an undefined method [] for nil because the 17th slot in your first dimension is nil

 

As you can see, you can get some pretty weird logical errors going on if you use this. It also is not very efficient. I'd say use 100 by 100 tops if you want to use this one in a game.
 
I have this solved, actually. I have a MultiArray class.

But one more question.

I am using this for the same thing I am using the MultiArray for:

Code:
 

def get_dis(x, y)

    dx = (@x - x) * -1

    dy = (@y - y) * -1

    return dx, dy

end

 

def update

  dis = get_dis(0, 0)

  p dis[0]

end

 

and I get a NoMethodError (same one from the multiarray ("[]" is not a method of NilClass)).
 

Atoa

Member

You can have an array of arrays.

n = [[["A"],[["B","C"]],[]]

then to get the objetct in it
n[0] = [["A"],["B"]]
n[0][1] = [["B","C"]]
n[0][1][0] = "B"

or maybe using an Hash.
 
Hash could work very well as a work-around for this:

Code:
 

object = {}

 

key = [x, y, z]

 

object[key] = 'something'

 

#vs.

 

object = []

 

object[x] = []

object[x][y] = []

object[x][y][z] = 'something'

But like Atoa said, since arrays can hold any object, including arrays, you can do it like that. I however I have learned that hashes with a key is probably an easier way. Just use an array key, with each dimension within the array.
 

Zeriab

Sponsor

To sum it all up:
There is no build-in multi-dimensional arrays.
You can create your own multi-dimensional array in several ways and the syntax depends on how you create it.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top