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.

DeM0nFiRe's Lite Multi-Dimensional Array

Hello everyone,
It's been quite some time since I released a public script, and I fgured it was about time. This is a Multi-Dimensional Array class that can create arrays of 2 or 3 Dimensions. This script should theoretically work for both RMXP and RMVX, however I have not used VX so I can't guarantee it. if someone can confirm that for me, that would be great. All the information you need should be in the header of the script, and i'm a bit low on time at the moment so I won't go through each section for instructions, terms of use etc.

If I have to, I'll add some more later.

Feel free to ask questions, offer feedback etc.

now to the good stuff:

DeM0nFiRe's Lite Multi-Dimensional Array


This is a lite version of a multi dimensional array. All it will let you do is store and retrieve objects. once you set the size of the array, you cannot change it. This class is a plus over Table in that it can take all kinds of objects, and it can be 3 dimensional as well as 2 dimensional. The full version of this script is being included in an engine i plan to sell so I can't give you guys everything of the class XD. This should still help you nonetheless.

Code:
 

#-------------------------------------------------------------------------------

# Title: DeM0nFiRe's Lite Multi Dimensional Array

# Scripter: DeM0nFiRe

# Version: 1.2.0 (02/15/09)

# Version History:

#   1.0.0 - First Release

#   1.0.1 - minor fix

#   1.1.1 - Updated inititialize method to be faster

#   1.2.0 - Updated to be cleaner

# Function: This class provides a method for 2- and 3- dimensional arrays.

#   This is a lite version of the class, and so the size you declare when 

#   initializing the array is it's final size. If you try to go outside the

#   bounds, it will not automatically resize the array like the standard array

#   will. This class has two distinctive features over the RGSS Table class.

#   one is the fact that Table can only hold 2 Dimensions, this one can do 2 or 

#   3. Also, Table can hold only numbers, this can hold any type of object.

#   PLEASE NOTE: While it has not been tested, this should work in RMVX as well

#   as RMXP. If someone can confirm this for me, that would be great.

# How To Use:

#   first, you must initialize the array like this:

#   "array = Multi_Array.new(dimensions, [w,h,d])"

#   where dimensions is the number of dimensions (2 or 3) and w is width, h is

#   hieght and d is depth. You only include d if dimensions is 3. The array will

#   be filled with nil when first created.

#   

#   once you have the array created, to change an item in the array you use:

#   "array[x,y,z] = object" You only use z if it is a 3 dimensional array. In a 

#   similar fashion, you can access the information with "array[x,y,z]" When you

#   add an object, you will get a return of the object if it worked, if it fails

#   it will print out why and return nil.

#   EXAMPLE:

#           array = Multi_Array.new(3, [3,3,3])

#           array[1,1,1] = 7 #->7

#           array[1,1,1] #-> 7

#           array[1,2,1] #-> nil

#           array[1,2,3] #-> prints "Selection Out of Bounds" and returns nil

#   you can also do array.size to retrun an array of [width, height, depth]

#   depth will be there only if it is a 3D array.

# Terms of Use:

#   You can use this script in any of your non-commercial projects, but I ask

#   that you give me a small mention of credit. if you want to use this for a

#   commercial project, talk to me first. I will probably ok it, but i'd like to

#   know how you will use it in a commercial setting.

#------------------------------------------------------------------------------- 

 

class Multi_Array

  

  MULTIARR_BOUNDERR_INIT = Exception.new("A Multi_Array must have only 2 or 3 dimensions")

  MULTIARR_BOUNDERR_ACC = Exception.new("Selection out of bounds on Multi_Array")

  MULTIARR_BOUNDERR_SET = Exception.new("Selection out of bounds on Multi_Array")

  MULTIARR_DIMERR_ACC = Exception.new("Wrong number of dimensions on Multi_Array")

  MULTIARR_DIMERR_SET = Exception.new("Wrong number of dimensions on Multi_Array")

  #---------------------------------------------------------------------

  # - Initialize

  #---------------------------------------------------------------------

 

 

  def initialize(*args, &block)

    @dim = args.size #get dimensions

    @size = args #get size

 

    if (@dim == 2) or (@dim == 3) #Make sure Dimensions is either one or two

      if block_given?

        if @dim == 2 #if it is 2 dimensions

          @data = Array.new(@size[0]){Array.new(@size[1]){block.call}} 

        else

          @data = Array.new(@size[0]){Array.new(@size[1]) {Array.new(@size[2]){block.call}}}

        end

      else

        if @dim == 2 #if it is 2 dimensions

          @data = Array.new(@size[0]){Array.new(@size[1])} 

        else

          @data = Array.new(@size[0]){Array.new(@size[1]) {Array.new(@size[2])}}

        end

      end

      

    else

      raise MULTIARR_BOUNDERR_INIT

    end

    

  end

  

  #---------------------------------------------------------------------

  # - []

  #---------------------------------------------------------------------

  

  def [] (*args)

    if args.size != @dim

      raise MULTIARR_DIMERR_ACC

    end

    

    if (args[0] > @size[0]) or (args[1] > @size[1])

      raise MULTIARR_BOUNDERR_ACC

    else

      if @dim == 3 and args[2] > @size[2]

        raise MULTIARR_BOUNDERR_ACC

      end

    end

    

    if @dim == 2

      return @data[args[0]][args[1]]

    else

      return @data[args[0]][args[1]][args[2]]

    end

    

  end

  

  #--------------------------------------------------------------------

  # - []=

  #--------------------------------------------------------------------

  

  def []=(*args)

    if args.size != (@dim + 1)

      raise MULTIARR_DIMERR_SET

    end

    

    if (args[0] > @size[0]) or (args[1] > @size[1])

      raise MULTIARR_BOUNDERR_SET

    else

      if @dim == 3 and args[2] > @size[2]

        raise MULTIARR_BOUNDERR_SET

      end

    end

    

    if @dim == 2

      @data[args[0]][args[1]] = args[2]

    else

      @data[args[0]][args[1]][args[2]] = args[3]

    end

    

  end

end

 

LATEST UPDATE: I've made the code a lot cleaner this time around. Usage instructions, as well as terms of use, are in the script header.

Previous Update: I've made the initialize method to be alot quicker. Right now I'm working on puting together a report of speeds so you can know how much data you should handle with this.

3-Dimensional Array Creation:
-200x200x200 (8 million cells) takes negligible time
-230x230x230 (12.167 million cells) takes negligible time
-240x240x240 (13.824 million cells) takes about a third of a second
Suggestion: I'd recommend going with a maximum of 10 million cells per array, just to be safe.

2-Dimensional Array Creation:
-1000x1000 (1 million cells) takes negligible time
-2500x2500 (6.25 million cells) takes negligible time
-4500x4500 (20.25 million cells) takes negligible time
-4600x4600 (21.16 million cells) takes negligible time
-4700x4700 (22.09 million cells) takes about a fifth of a second
Suggestion: I'd reccommend staying to a max of around 20 million cells per array, just to be safe.

3-Dimensional Array Accessing (getting data and storing in a variable, done with the max reccomended size of 215x215x215)
-50x50x50 (125 thousand cells) takes negligible time
-60x60x60 (216 thousand cells) takes negligble time
-65x65x65 (274.625 thousand cells) takes about a fifth of a second


(I'll finish the rest of this report when I get a chance)
 

e

Sponsor

This is pure Ruby and, as far as I could see, doesn't use any of the exclusive RGSS classes; so yes, it will work in both RMXP and RMVX; actually, it'll work in anything that supports Ruby.

I'd just like to point out that this, considering it's not an RGSS script for games, but more of a class for other scripters, should go (I think) in the Scripter's Corner, which is specifically designated for classes/modules/methods/tips/etc for scripters in general.

Looks good anyhow; you should probably add an "each" method in there, so anyone who wants other methods could simply include the Enumerable module (which adds a buncha useful stuff like "all?", "find?", "grep", etc.)
 
Well, as I said, this is a lite version. To get access to all the goody features you'll have to pay. :D (The engine is not in RMXP, I just saw this as a good script for RMXP users.)

As for the compatibility, I don't know any details on RGSS 2 so I didn't know if they built it off of something other than ruby this time around (no, I don't know what they would make the R stand for if that was the case ;) )

And for the reason of it's placements I have two (only one of the is a good one)

Starting with the bad one, I simply did not know there was a Scripter's corner :)

And the main reason I had was that I figure it could be used in RMXP/VX to do things like write a new tilemap class that can take more than 3 layers or maybe more event layers. (That is how it will be used in my engine)

Thanks for confirming the compatibility for me, etheon.

UPDATE: Compatibility with RMVX is Confirmed. I'll make the new update to the first post and the script header when I get a chance.
 
why have this only 3 dimensions?? xD
and why do you not include dim 1?

i think i can upgrade this to X

error found:

Code:
    if (@dimensions == 2) and (z > @size[2]+1) #if z out of bounds on 3D array
      print("Selection Out of Bounds")
      return nil
    end
@dimensions == 2 must be @dimensions == 3
 
@hanmac: Whoops, thanks for catching that error. I was changing it from @dimensions != 2 to @dimensions == 3 and I forgot to change the number ;)

This does not have only 3 dimesnions, it can have 3 or 2. it does not have more than 3 because, as I said, this was originally for an engine I'm making that only needs 2 or 3. true, it would be a relatively simple deal to make it support more than 3 Dimensions.

As for not including a one dimensional array, my multi-array class uses the array class. If you want a single dimensional array, you can just use the array class.

Maybe when I have more time I'll release a version with more dimensions.

@Tdata: Well, It'd be kinda hard to print a 3D table anyway, and this version of the array is only to store and retrieve data. it doesn't do much more than that.

And to everyone: I've noticed a few errors, and Zeriab has pointed out some things I could do better so I'm working on a new release that should make it a litlle cleaner.
 
xD it's not supposed to change anything ... if you don't use it... it's not a graphical thing or whatever..

Anyway, nothing to complicated here:p, I did a  N*dimensions array, (the trick it's a simple array... of dim1 * dim 2 * ... * dimN, where the "coordinates" are used to get the right index ), But... nothing complicated either. What would be great with multi-dim arrays.... 
Matrix Calculus... like rotations and operations(+,-,*,/), Guys who do some semi-advanced math will see what i'm talking about...  can be really usefull in my opinion for some advanced stuff... (map rotations maybe....(well only 90°each time) )
 
yeah i know:p that ruby already as one, but as you said, the rgss doesn't include, so that's why i would like to see it.. xD
the thing is , those 2 classes only work with numbers. would be cool to do that on normal arrays...
 
i dont get this
if it dont make changes to the object what does it do

can somone make a demo te explane it

it would be verry helpfull
 

e

Sponsor

Like I said in my first post, this should've gone in the Scripter's Corner, since it is to be used by other scripters.
 
As far as the Ruby Matrix goes, as far as I know it is only 2 dimensions and only integers. I've only vaguely looked at vectors, so I can't say much on that.

As far as map rotations and such go, that is interesting. For the final version of it that'll be in an engine I am working on, I should look into that. Unfortunately, all of my teachers so far have skipped any math dealing with matrices, so I'll have to come up with that stuff on my own :P

As far as more than 3 dimensions go, I just don't really see the point. I mean, I plan on making it be more than 3D anyway just for the fun of trying it, but in the context it is meant for, there's not really much point. XD
 
Hey, guess what! I just found a newer version of this script! The newer version is a lot cleaner as far as code goes. I just found this updated version because someone is looking for a MultiArray class.
 

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