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.
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)
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)