Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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 0..@components.size
      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 0..@components.size
      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 ?

share|improve this question
    
Have you tried a nested hash instead? –  Byte56 Jun 16 '12 at 0:05
    
XP is old. It was old in 2003. Consider upgrading to a newer RPG Maker. –  ashes999 Jun 16 '12 at 2:14
    
@Byte56 : For the last resort, I'll try hash or vector, so this is confirmed as array limitation in ruby ? –  AzDesign Jun 16 '12 at 4:41
    
@ashes999, yes it is old, but I need XP for its styles (VX and VXAce has chibi-character style, I dont like it) And most custom script available in forum dedicated to XP –  AzDesign Jun 16 '12 at 4:43
    
I'm not sure about "confirmed" but the nested hash does seem easier. –  Byte56 Jun 16 '12 at 4:44
show 1 more comment

1 Answer 1

Ok, after doing some reading, understanding ruby, I found the solution without changing the data type to hash/vector

  def draw
    image = []
    index = 0
    @components.each { |arr|
      if image.size > 0
        index = image.size
      end
      image[index] = Sprite.new
      image[index].bitmap = RPG::Cache.picture(arr[0] + '.png')
      image[index].x = @x + arr[1]
      image[index].y = @y + arr[2]
      image[index].z = @z + arr[3]
      @test =+ 1
    }
  end

each function is really convenient, I just learned that. Thanks the feedback guys.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.