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.

??? arrays inside arrays??

I needed some help here, i'm trying to order some arrarys... is it possible to use arrays inside arrays like in C?

array1 = [att1, def1, agl1, mag1, ID]
array2 = [att2, def2, agl2, mag2, ID]
array3 = [att3, def3, agl3, mag3, ID]

i want ot order them by the value in the agility (agl) slot.

in C i would have used arrays inside arrays, I don't know how to do in RGSS! ???

Thank you in advance,

Francisco Dias
 

poccil

Sponsor

It certainly is possible.  Here is an example:

Code:
array=[
   [att1, def1, agl1, mag1, ID],
   [att2, def2, agl2, mag2, ID],
   [att3, def3, agl3, mag3, ID]
]
array.sort!{|a,b|  a[2]<=>b[2]}
The sort! method sorts the array in place.  The two parameters, a and b, are two items
from the array to be compared.
 
thank you very much theres also something i wanted to ask then,

in the array:

Code:
array = [
   [12, 32, 11, 0, 77]
   [2, 26, 1, 12, 3]
   [1,98,54,22,89]
]

how do I refer to number 12?

is it array[1][3] ??
do you know were I can learn more about those things?
I know where to find ruby info but i heard some functions do not work in RGSS

Francisco Dias
 

khmp

Sponsor

array[0][0] Indexing starts with 0 and increases sequentially. And to learn more about Ruby/RGSS visit http://www.rmxp.org/forums/index.php?topic=1868.0
I'm not aware of any functionality that doesn't work on RGSS that works on Ruby. RGSS is built on top of Ruby after all.

To access it sequentially:
Code:
for i in 0...array.size
  array[i].each {|item| p item }
end

for i in 0...array.size
  for j in 0...array[i].size
    p array[i][j]
  end
end

array.each do |subarray|
  subarray.each do |item|
    p item
  end
end

Oh one last thing don't forget the comma's in between array elements.
array =
[
  [1, 2, 3, 4],
  [5, 6, 7, 8],
  [9, 10, 11, 12]
]

Good luck with it xykudyax! :afro:
 

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