What I have here is a really simple Multi-Dimensional Array class. This is a Lite version with only very basic features. It supports 2- and 3-Dimenional Arrays, and it only really has capabilities for storing and accessing data. I am working on a fully featured version of the class, but it's going in a game engine I am working on that I'll hopefully be selling licenses for, so it would do no good to release all the classes in it.
This class has relatively good performance on my machine, but I've had reports of less performance on lesser machines, so you may want to watch out for that when using this. This class beats the Matrix class because the Matrix can hold only numbers. Multi_Array can hold any type of object. There are some issues with this code that I am aware of and that I am fixing, but the code is still functional.
The comments at the top of the code refer to RMXP and RMVX because I originally released it as a script for those two programs. However, it is straight ruby so it will work without those programs just fine. Terms of use and instructions for use are found above the code in those same comments.
CODE
#-------------------------------------------------------------------------------
# Title: DeM0nFiRe's Lite Multi Dimensional Array
# Scripter: DeM0nFiRe
# Version: 1.1.1 (03/28/08)
# Version History:
# 1.0.0 - First Release
# 1.0.1 - minor fix
# 1.1.1 - Updated inititialize method to be faster
# 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
################################################################################
class Multi_Array
attr_reader :size
#=============================================================================
# -initialize(dimensions, [w,h,d])
#---------------------------------
# dimensions: number of dimensions for the array (2 or 3 only) [REQUIRED]
# w: number of objects wide the array is [REQUIRED]
# h: number of objects tall the array is [REQUIRED]
# d: number of objects deep the array is [ONLY IF dimensions IS 3]
#=============================================================================
def initialize(dimensions, size)
@data = [] #create standard array
@size = size #size of array
@dimensions = dimensions #dimensions of array
if (@dimensions >= 2) and (@dimensions <= 4)
if size.length < 2 #make sure there are enough dimensions given in size
print("Not enough dimensions in size")
return nil
end
#Make array 2D
for i in 0...size[0]
@data[i] = Array.new(@size[1])
end
if @dimensions == 2 #if the array will be 2D
return self #return the 2D array
else #if array will not be 2D
if @dimensions == 3 #if array will be 3D
if @size.length != 3
print("Wrong number of dimensions in size")
return nil
end
#add 3rd dimension to array
for i in 0...size[0]
for j in 0...size[1]
@data[i][j] = Array.new(size[2])
end
end
return self #return the 3D array
else #if array is not 2D or 3D
print("Multi_Array can take only 2 or 3 dimensions.")
return nil
end
end
else
print("Multi_Array can take only 2 or 3 dimensions.")
return nil
end
end
#=============================================================================
# -[x,y,z]
#--------
# x: the x of your selection
# y: the y of your selection
# z: teh z of your selection (only for 3D array
#=============================================================================
def [](x,y, z=nil)
if (z !=nil and @dimensions !=3) or (z == nil and @dimensions == 3) #if wrong number of dimensions accessed
print("Wrong Number of Dimensions Accessed on " + @dimensions.to_s + "D array")
return nil
end
if (x > @size[0]+1) or (y > @size[1]+1) #if x or y out of bounds
print("Selection Out of Bounds")
return nil
end
if (@dimensions == 2) and (z > @size[2]+1) #if z out of bounds on 3D array
print("Selection Out of Bounds")
return nil
end
if @dimensions == 2 #if array is 2D
return @data[x][y] #return selection
end
if @dimensions == 3 #if array is 3D
return @data[x][y][z] #return selection
end
#now, if the array makes it this far, that means I messed up the code
#or it was used in a situation I did not anticipate (Which still means
#I messed up
)
print("Array has been corrupted")
return nil
end
#=============================================================================
# - [x,y,z] = object
#-------------------
# x: the x of your selection
# y: the y of your selection
# z: the z of your selection (only for 3D array)
# object: what you want to set the selcection to
#=============================================================================
def []=(x, y, z_or_obj, object=nil)
if z_or_obj == nil
print("The object was not set correctly. Please refer to script header for instructions")
return nil
end
if object != nil and @dimensions == 2
print("The object was not set correctly. Please refer to script header for instructions")
return nil
end
if @dimensions == 2
@data[x][y] = z_or_obj
return @data[x][y]
end
if @dimensions == 3
@data[x][y][z_or_obj] = object
return @data[x][y][z_or_obj]
end
#now, if the array makes it this far, that means I messed up the code
#or it was used in a situation I did not anticipate (Which still means
#I messed up
)
print("Array has been corrupted")
return nil
end
end
# Title: DeM0nFiRe's Lite Multi Dimensional Array
# Scripter: DeM0nFiRe
# Version: 1.1.1 (03/28/08)
# Version History:
# 1.0.0 - First Release
# 1.0.1 - minor fix
# 1.1.1 - Updated inititialize method to be faster
# 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
################################################################################
class Multi_Array
attr_reader :size
#=============================================================================
# -initialize(dimensions, [w,h,d])
#---------------------------------
# dimensions: number of dimensions for the array (2 or 3 only) [REQUIRED]
# w: number of objects wide the array is [REQUIRED]
# h: number of objects tall the array is [REQUIRED]
# d: number of objects deep the array is [ONLY IF dimensions IS 3]
#=============================================================================
def initialize(dimensions, size)
@data = [] #create standard array
@size = size #size of array
@dimensions = dimensions #dimensions of array
if (@dimensions >= 2) and (@dimensions <= 4)
if size.length < 2 #make sure there are enough dimensions given in size
print("Not enough dimensions in size")
return nil
end
#Make array 2D
for i in 0...size[0]
@data[i] = Array.new(@size[1])
end
if @dimensions == 2 #if the array will be 2D
return self #return the 2D array
else #if array will not be 2D
if @dimensions == 3 #if array will be 3D
if @size.length != 3
print("Wrong number of dimensions in size")
return nil
end
#add 3rd dimension to array
for i in 0...size[0]
for j in 0...size[1]
@data[i][j] = Array.new(size[2])
end
end
return self #return the 3D array
else #if array is not 2D or 3D
print("Multi_Array can take only 2 or 3 dimensions.")
return nil
end
end
else
print("Multi_Array can take only 2 or 3 dimensions.")
return nil
end
end
#=============================================================================
# -[x,y,z]
#--------
# x: the x of your selection
# y: the y of your selection
# z: teh z of your selection (only for 3D array
#=============================================================================
def [](x,y, z=nil)
if (z !=nil and @dimensions !=3) or (z == nil and @dimensions == 3) #if wrong number of dimensions accessed
print("Wrong Number of Dimensions Accessed on " + @dimensions.to_s + "D array")
return nil
end
if (x > @size[0]+1) or (y > @size[1]+1) #if x or y out of bounds
print("Selection Out of Bounds")
return nil
end
if (@dimensions == 2) and (z > @size[2]+1) #if z out of bounds on 3D array
print("Selection Out of Bounds")
return nil
end
if @dimensions == 2 #if array is 2D
return @data[x][y] #return selection
end
if @dimensions == 3 #if array is 3D
return @data[x][y][z] #return selection
end
#now, if the array makes it this far, that means I messed up the code
#or it was used in a situation I did not anticipate (Which still means
#I messed up
print("Array has been corrupted")
return nil
end
#=============================================================================
# - [x,y,z] = object
#-------------------
# x: the x of your selection
# y: the y of your selection
# z: the z of your selection (only for 3D array)
# object: what you want to set the selcection to
#=============================================================================
def []=(x, y, z_or_obj, object=nil)
if z_or_obj == nil
print("The object was not set correctly. Please refer to script header for instructions")
return nil
end
if object != nil and @dimensions == 2
print("The object was not set correctly. Please refer to script header for instructions")
return nil
end
if @dimensions == 2
@data[x][y] = z_or_obj
return @data[x][y]
end
if @dimensions == 3
@data[x][y][z_or_obj] = object
return @data[x][y][z_or_obj]
end
#now, if the array makes it this far, that means I messed up the code
#or it was used in a situation I did not anticipate (Which still means
#I messed up
print("Array has been corrupted")
return nil
end
end

