Chaos Project

RPG Maker => RPG Maker Scripts => Script Troubleshooting => Topic started by: Memor-X on July 22, 2012, 06:22:47 pm

Title: RMXP - Passing Arrays into a function
Post by: Memor-X on July 22, 2012, 06:22:47 pm
how exactly do i do it, all i can remember from c++ programming is that you pass the memory address to the function to allow it to explicitly access the array outside it's scope.

i particularly want to know how i pass an array created using Table.new since i've now started to get in the habit of using the Table CXlass rather than creating a normal array
Title: Re: RMXP - Passing Arrays into a function
Post by: ForeverZer0 on July 22, 2012, 09:33:22 pm
Ruby does not support pointers if that is what you are talking about. There is no such thing, nor any equivalent.

Ruby uses a strange blend of "pass by reference" and "pass by value" unlike other languages. When you pass an object as an argument, you are passing an object, but the object is a reference to the object. It is basically a copy of the object that represents it.

def my_method(string)
  string.gsub!(/foo/, 'bar')
end

str = "the word: foo"
my_method(str)
p str # "the word: bar"


Title: Re: RMXP - Passing Arrays into a function
Post by: Memor-X on July 23, 2012, 06:18:08 pm
so i take it the following code



myArray = Table.new(2,2)
myArray[0,0] = "cows "
myArray[0,1] = "are "
myArray[1,0] = "my "
myArray[1,1] = "friends ,' )"

printArray(myArray)

def printArray(passedArray)
   
   for i in 0...1
       for j in 0...1
           p passedArray[i,j]
       end
   end
   
end



should work find right?

EDIT: oh what the hell, just hooked up my laptop cause i got some time before work just to test the code and keep getting an error saying that it can't convert a string to an integer........any idea why?
Title: Re: RMXP - Passing Arrays into a function
Post by: ForeverZer0 on July 23, 2012, 06:20:34 pm
Yes, that will work fine.
Title: Re: RMXP - Passing Arrays into a function
Post by: KK20 on July 23, 2012, 07:07:52 pm
Quote from: RMXP Help File -- TableThe multidimensional array class. Each element takes up 2 signed bytes, ranging from -32,768 to 32,767.


You can only store integers (or a 'short' in this case) in a Table instance. So...sorry, if you want an array of strings, gotta use an Array.
Title: Re: RMXP - Passing Arrays into a function
Post by: ForeverZer0 on July 23, 2012, 08:13:50 pm
KK20 is correct. I didn't notice you were attempting to place strings into a Table.

For normal function, there is no real advantage to using a Table anyway. A Table is simply a 1-dimensional C array of shorts. RMXP uses this only because Ruby's Array class is not fit to handle very large amounts of data, and you can get a slight performance increase when the amount of data is very large and accessed often.

In your case, if you need a container that is not so penalized for containing large amounts of data, just use a Hash. If performance is that critical where a Hash is not even acceptable in terms of perforamce, then you are using the wrong language.
Title: Re: RMXP - Passing Arrays into a function
Post by: nathmatt on July 23, 2012, 09:09:07 pm
@ForeverZer0 true but was bored so i made a class that would allow it

Spoiler: ShowHide
class Enhanced_table
 
  def initialize
    @data = {}
  end
 
  def [](*args)
    return @data[args]
  end
 
  def []=(*args)
    value = args.pop
    @data[args] = value
  end
 
end


edit fixed to allow any size

myArray = Enhanced_table.new
myArray[0,1,2,3,4,5,6,7,8,9] = "cows "
Title: Re: RMXP - Passing Arrays into a function
Post by: ForeverZer0 on July 23, 2012, 09:55:15 pm
Ruby natively supports multidimensional arrays. Simply place an array into an array, and its the same thing. A Ruby class that is simply a copy of the Table class that allows Ruby objects has even less performance than a standard array. RMXP's Table class is only better on performance because it is simply a wrapper for a class of a much lower-level language.
Title: Re: RMXP - Passing Arrays into a function
Post by: Memor-X on July 23, 2012, 10:26:29 pm
Quote from: nathmatt on July 23, 2012, 09:09:07 pm
@ForeverZer0 true but was bored so i made a class that would allow it

Spoiler: ShowHide
class Enhanced_Table
 
 def initialize
   @data = {}
 end
 
 def [](key1,key2=nil,key3=nil)
   key = [key1,key2,key3].compact
   return @data[key]
 end
 
 def []=(key1, key2, key3=nil, value=nil)
   if value != nil
     return @data[[key1,key2,key3]] = value
   elsif key3 != nil
     return @data[[key1,key2]] = key3
   else
     return @data[[key1]] = key2
   end
 end
 
end



so if i want to use this class i use Enhanced_Table.new instead of Table.new (and also making sure that the script is in my script list), nice, i honestly can not remember now why i was using strings for testing but i prefer flexibility, in case if i need to store an array of strings and strings can easily be converted to integers with .to_i (or was it .to_int)

Quote from: ForeverZer0 on July 23, 2012, 08:13:50 pm
KK20 is correct. I didn't notice you were attempting to place strings into a Table.

For normal function, there is no real advantage to using a Table anyway. A Table is simply a 1-dimensional C array of shorts. RMXP uses this only because Ruby's Array class is not fit to handle very large amounts of data, and you can get a slight performance increase when the amount of data is very large and accessed often.

In your case, if you need a container that is not so penalized for containing large amounts of data, just use a Hash. If performance is that critical where a Hash is not even acceptable in terms of perforamce, then you are using the wrong language.


using Table just seems easier to be easier to create blank multi-dimensional arrays which i populate with for loops or having a variable indicating the last index (if the variable is set to 5, then index 0 to 4 have data while all the rest are blank), with a normal array i think it's like


myArray1 = []
myArray2 = [[],[]]
myArray3 = [[[],[],[]],[[],[],[]]]


or something like that, i know the first differently works cause i use it in my Pandora Plugin Script before i go to import data from a file (i only ever used 1d arrays in that)

anyway, i'm not passing in huge arrays, all what needed to do is pass an array of indexs for an array created in a class to change the values of, insted of using


class.array[0] = 2
class.array[1] = 2
class.array[5] = 2
class.array[4] = 2
class.array[7] = 2
class.array[15] = 2
class.array[14] = 2


i pan to do this


indexArray = Table.new(7)
indexArray = [0,1,5,4,7,15,14]
class.changeArray(indexArray)

class class

   classArray = Table.new(100)
   
   def changeArray(array)
   
   for i in 0...array.length
       classArray[array[i]] = 2
   end
   
   end

end


the second is a hell lot neater and i'm less likely to forget what the hell i'm doing if i forget to document
Title: Re: RMXP - Passing Arrays into a function
Post by: ForeverZer0 on July 23, 2012, 10:51:07 pm
Wow, you are really over-complicating basic things.

[0, 1, 5, 4, 7, 15, 14].each {|i| class.array[i] = 2 }


or if you prefer:

indices = [0, 1, 5, 4, 7, 15, 14]
indices.each {|i| class.array[i] = 2 }
Title: Re: RMXP - Passing Arrays into a function
Post by: Memor-X on July 24, 2012, 03:01:28 am
Quote from: ForeverZer0 on July 23, 2012, 10:51:07 pm
Wow, you are really over-complicating basic things.

[0, 1, 5, 4, 7, 15, 14].each {|i| class.array[i] = 2 }


or if you prefer:

indices = [0, 1, 5, 4, 7, 15, 14]
indices.each {|i| class.array[i] = 2 }



ahhhhhhh, i've seen that code in Blizzard's and your scripts when i was snooping around, never understood it though cause it was mixed in code that i was unfamiliar with but now that it's using code i am familiar with then it makes sense to me now

and for the record, what i was going to do was basic for me so i didn't think it was overly complex, it was the first system i developed during my Game Design Course back in Uni, a function that takes an array of indexs and alters the data in an array corresponding to those indexes and thus bypassing you having to overload the function or call the function multiple times, i had a headache, was falling asleep almost every 3 minutes and was out of Cola so given all of that it was very simple to me, very little thought went into it,

ofcause i'm still learning Ruby and RGSS so i'm bound to find ways to improve my already existing code, kinda hoping it carries into other languages, lucky i shared that, thanks ForeverZer0