2
\$\begingroup\$

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 ?

\$\endgroup\$
6
  • \$\begingroup\$ Have you tried a nested hash instead? \$\endgroup\$ Commented Jun 16, 2012 at 0:05
  • \$\begingroup\$ XP is old. It was old in 2003. Consider upgrading to a newer RPG Maker. \$\endgroup\$ Commented Jun 16, 2012 at 2:14
  • \$\begingroup\$ @Byte56 : For the last resort, I'll try hash or vector, so this is confirmed as array limitation in ruby ? \$\endgroup\$ Commented Jun 16, 2012 at 4:41
  • 1
    \$\begingroup\$ @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 \$\endgroup\$ Commented Jun 16, 2012 at 4:43
  • \$\begingroup\$ I'm not sure about "confirmed" but the nested hash does seem easier. \$\endgroup\$ Commented Jun 16, 2012 at 4:44

1 Answer 1

1
\$\begingroup\$

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.

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.