multi-dimension array problem in RGSS (RPG Maker XP)

Posted by AzDesign on Game Development See other posts from Game Development or by AzDesign
Published on 2012-06-15T21:06:25Z Indexed on 2012/06/15 21:25 UTC
Read the original article Hit count: 479

Filed under:
|

This is my first day code script in RMXP. I read tutorials, ruby references, etc and I found myself stuck on a weird problem, here is the scenario:

  1. I made a custom script to display layered images
  2. Create the class, create an instance variable to hold the array, create a simple method to add an element into it, done
  3. The draw method (skipped the rest of the code to this part):
  def draw
    image = []
    index = 0
    for i in [email protected]
      if image.size > 0
        index = image.size
      end
      image[index] = Sprite.new
      image[index].bitmap = RPG::Cache.picture(@components[i][0] + '.png')
      image[index].x = @x + @components[i][1]
      image[index].y = @y + @components[i][2]
      image[index].z = @z + @components[i][3]
      @test =+ 1
    end
  end
  1. Create an event that does these script
> $layerz = Layerz.new $layerz.configuration[0] = ['root',0,0,1]
> $layerz.configuration[1] = ['bark',0,10,2] 
> $layerz.configuration[2] = ['branch',0,30,3]
> $layerz.configuration[3] = ['leaves',0,60,4] $layerz.draw
  1. Run, trigger the event and the result :

ERROR! Undefined method`[]' for nil:NilClass pointing at this line on draw method :

image[index].bitmap = RPG::Cache.picture(@components[i][0] + '.png')

THEN, I changed the method like these just for testing:

def draw
    image = []
    index = 0
    for i in [email protected]
      if image.size > 0
        index = image.size
      end
      image[index] = Sprite.new
      image[index].bitmap = RPG::Cache.picture(@components[0][0] + '.png')
      image[index].x = @x + @components[0][1]
      image[index].y = @y + @components[0][2]
      image[index].z = @z + @components[0][3]
      @test =+ 1
    end

I changed the @components[i][0] to @components[0][0] and IT WORKS, but only the root as it not iterates to the next array index

Im stuck here, see :

> in single level array, @components[0] and @components[i] has no problem
> in multi-dimension array, @components[0][0] has no problem BUT
> in multi-dimension array, @components[i][0] produce the error as above
> mentioned.

any suggestion to fix the error ? Or did I wrote something wrong ?

© Game Development or respective owner

Related posts about ruby

Related posts about rpg-maker-xp