Chaos Project

RPG Maker => RPG Maker Scripts => Script Requests => Topic started by: PrinceEndymion88 on October 19, 2014, 04:48:28 pm

Title: Error without explanation.
Post by: PrinceEndymion88 on October 19, 2014, 04:48:28 pm
Hi guys, I have a problem with a part of the script of my battle system.
I use CCOA 4.3 EX, until yesterday worked fine, today I take a test and when I start a battle I always get the following error:
Script 'Window_Base' line 136: NoMethodError occured.
undefined method 'name' for nil:NilClass


This is the part of the script where the error occurs ...
#==============================================================================
# ADD-ON - LAYOUT
#==============================================================================
# By Moghunter
#==============================================================================
class React < Window_Base
  attr_reader   :index                   
  attr_reader   :help_window             
  def initialize(x, y, width, height)
    super(x, y, width, height)
    @item_max = 1
    @row_max = 1
    @index = -1
  end
  def index=(index)
    @index = index
     refresh
    if self.active and @help_window != nil
      update_help
    end
    update_cursor_rect
    end
  def row_max
    return (@item_max + @column_max - 1) / @column_max
  end
  def top_row
    return self.oy / 32
  end
  def top_row=(row)
    if row < 0
      row = 0
    end
    if row > row_max - 1
      row = row_max - 1
    end
    self.oy = row * 32
  end
  def page_row_max
    return (self.height - 32) / 32
  end
  def page_item_max
    return page_row_max * @column_max
  end
  def help_window=(help_window)
    @help_window = help_window
    if self.active and @help_window != nil
      update_help
    end
  end
  def update_cursor_rect   
    if @index < 0
      self.cursor_rect.empty
      return
    end
    row = @index / @column_max
    if row < self.top_row
      self.top_row = row
    end
    if row > self.top_row + (self.page_row_max - 1)
      self.top_row = row - (self.page_row_max - 1)
    end
    cursor_width = 0
    x = @index / @column_max * 48 - self.oy
    y = 110 + @index % @column_max * (cursor_width + 100)
    self.cursor_rect.set(x, y, cursor_width, 32)
    t = @index / @column_max * 28 - self.oy
  end
  def update
    super
    if self.active and @item_max > 0 and @index >= 0
      if Input.repeat?(Input::C) or Input.repeat?(Input::B)
          refresh
      end
      if Input.repeat?(Input::RIGHT)
        if (@column_max == 1 and Input.trigger?(Input::RIGHT)) or
           @index < @item_max - @row_max
          $game_system.se_play($data_system.cursor_se)
          @index = (@index + @row_max) % @item_max
          refresh
          end
      end
      if Input.repeat?(Input::LEFT)
        if (@column_max == 1 and Input.trigger?(Input::LEFT)) or
           @index >= @row_max
          $game_system.se_play($data_system.cursor_se)
          @index = (@index - @row_max + @item_max) % @item_max
          refresh
          end
      end
      if Input.repeat?(Input::RIGHT)
        if @column_max >= 2 and @index < @item_max - 1
          $game_system.se_play($data_system.cursor_se)
          @index += 1
          refresh
          end
      end
      if Input.repeat?(Input::LEFT)
        if @column_max >= 2 and @index > 0
          $game_system.se_play($data_system.cursor_se)
          @index -= 1
          refresh
          end
      end
      if Input.repeat?(Input::R)
        if self.top_row + (self.page_row_max - 1) < (self.row_max - 1)
          $game_system.se_play($data_system.cursor_se)
          @index = [@index + self.page_item_max, @item_max - 1].min
          refresh
          self.top_row += self.page_row_max
        end
      end
      if Input.repeat?(Input::L)
        if self.top_row > 0
          $game_system.se_play($data_system.cursor_se)
          @index = [@index - self.page_item_max, 0].max
          refresh
          self.top_row -= self.page_row_max
        end
      end
    end
    if self.active and @help_window != nil
      update_help
    end
    update_cursor_rect
  end
end
class Window_Base < Window
  def draw_face(actor,x,y)
    face = RPG::Cache.picture(actor.name + "_face")
    cw = face.width
    ch = face.height
    src_rect = Rect.new(0, 0, cw, ch)
    self.contents.blt(x , y - ch, face, src_rect)   
  end
def draw_face2(actor,x,y)
    face = RPG::Cache.picture(actor.name + "_face2")
    cw = face.width
    ch = face.height
    src_rect = Rect.new(0, 0, cw, ch)
    self.contents.blt(x , y - ch, face, src_rect)   
end   
def draw_lay(actor,x,y)
    layout = RPG::Cache.picture(actor.name + "_lay")
    cw = layout.width
    ch = layout.height
    src_rect = Rect.new(0, 0, cw, ch)
    self.contents.blt(x , y - ch, layout, src_rect)   
end
def draw_hp(actor, x, y, width = 144)
    back = RPG::Cache.picture("BAR0")   
    cw = back.width 
    ch = back.height
    src_rect = Rect.new(0, 0, cw, ch)   
    self.contents.blt(x + 65, y - ch + 30, back, src_rect)
    meter = RPG::Cache.picture("HP_BAR")   
    cw = meter.width  * actor.hp / actor.maxhp
    ch = meter.height
    src_rect = Rect.new(0, 0, cw, ch)
    self.contents.blt(x + 65, y - ch + 30, meter, src_rect)
    text = RPG::Cache.picture("HP_T")   
    cw = text.width 
    ch = text.height
    src_rect = Rect.new(0, 0, cw, ch)
    self.contents.blt(x + 60, y - ch + 15, text, src_rect)
        self.contents.font.color = system_color
       if width - 32 >= 108
       hp_x = x + width - 108
       flag = true
       elsif width - 32 >= 48
       hp_x = x + width - 48
       flag = false
       end
       self.contents.font.color = Color.new(250,255,255,255)
       self.contents.draw_text(hp_x + 20, y - 8, 48, 32, actor.hp.to_s, 2)
end
def draw_hp2(actor, x, y, width = 144)
    back = RPG::Cache.picture("BAR0")   
    cw = back.width 
    ch = back.height
    src_rect = Rect.new(0, 0, cw, ch)   
    self.contents.blt(x + 65, y - ch + 25, back, src_rect)
    meter = RPG::Cache.picture("HP_BAR")   
    cw = meter.width  * actor.hp / actor.maxhp
    ch = meter.height
    src_rect = Rect.new(0, 0, cw, ch)
    self.contents.blt(x + 65, y - ch + 25, meter, src_rect)
    text = RPG::Cache.picture("HP_T")   
    cw = text.width 
    ch = text.height
    src_rect = Rect.new(0, 0, cw, ch)
    self.contents.blt(x + 33, y - ch + 20, text, src_rect)
        self.contents.font.color = system_color
       if width - 32 >= 108
       hp_x = x + width - 108
       flag = true
       elsif width - 32 >= 48
       hp_x = x + width - 48
       flag = false
     end
     self.contents.font.color = Color.new(250,255,255,255)
       self.contents.sfont = SFont.new("Arial_Battle")
       self.contents.draw_text(hp_x - 21, y - 4, 12, 32, "HP", 1)
       self.contents.draw_text(hp_x - 33, y - 4, 48, 32, actor.hp.to_s, 2)
       self.contents.font.color = normal_color
       self.contents.sfont = SFont.new("Arial_Battle")
       self.contents.draw_text(hp_x + 15, y - 4, 12, 32, "/", 1)
       self.contents.draw_text(hp_x + 27, y - 4, 48, 32, actor.maxhp.to_s)
       
end 
def draw_sp(actor, x, y, width = 144)
    back = RPG::Cache.picture("BAR0")   
    cw = back.width 
    ch = back.height
    src_rect = Rect.new(0, 0, cw, ch)   
    self.contents.blt(x + 65, y - ch + 30, back, src_rect)
    meter = RPG::Cache.picture("SP_BAR")   
    cw = meter.width  * actor.sp / actor.maxsp
    ch = meter.height
    src_rect = Rect.new(0, 0, cw, ch)
    self.contents.blt(x + 65, y - ch + 30, meter, src_rect)
    text = RPG::Cache.picture("SP_T")   
    cw = text.width 
    ch = text.height
    src_rect = Rect.new(0, 0, cw, ch)
    self.contents.blt(x + 60, y - ch + 15, text, src_rect)
        self.contents.font.color = system_color
       if width - 32 >= 108
       sp_x = x + width - 108
       flag = true
       elsif width - 32 >= 48
       sp_x = x + width - 48
       flag = false
       end
       self.contents.font.color = Color.new(250,255,255,255)
       self.contents.draw_text(sp_x + 20, y - 8, 48, 32, actor.sp.to_s, 2)
     end
def draw_sp2(actor, x, y, width = 144)
    back = RPG::Cache.picture("BAR0")   
    cw = back.width 
    ch = back.height
    src_rect = Rect.new(0, 0, cw, ch)   
    self.contents.blt(x + 65, y - ch + 25, back, src_rect)
    meter = RPG::Cache.picture("SP_BAR")   
    cw = meter.width  * actor.sp / actor.maxsp
    ch = meter.height
    src_rect = Rect.new(0, 0, cw, ch)
    self.contents.blt(x + 65, y - ch + 25, meter, src_rect)
    text = RPG::Cache.picture("SP_T")   
    cw = text.width 
    ch = text.height
    src_rect = Rect.new(0, 0, cw, ch)
    self.contents.blt(x + 33, y - ch + 20, text, src_rect)
        self.contents.font.color = system_color
       if width - 32 >= 108
       sp_x = x + width - 108
       flag = true
       elsif width - 32 >= 48
       sp_x = x + width - 48
       flag = false
       end
       self.contents.font.color = Color.new(250,255,255,255)
       self.contents.sfont = SFont.new("Arial_Battle")
       self.contents.draw_text(sp_x - 48, y - 4, 12, 32, "EP", 1)
       self.contents.draw_text(sp_x - 60, y - 4, 48, 32, actor.sp.to_s, 2)
       self.contents.font.color = normal_color
       self.contents.sfont = SFont.new("Arial_Battle")
       self.contents.draw_text(sp_x - 12, y - 4, 12, 32, "/", 1)
       self.contents.draw_text(sp_x, y - 4, 48, 32, actor.maxsp.to_s)
end     
  def draw_name(actor, x, y)
    self.contents.font.color = Color.new(238,46,218,255)
    self.contents.sfont = SFont.new("Arial_Battle")
    self.contents.draw_text(x , y, 100, 40, actor.name,15)    #self.contents.draw_text(x , y, 70, 40, actor.name,1)
  end       
 
def draw_commando(x,y)
    com = RPG::Cache.picture("commando")
    cw = com.width
    ch = com.height
    src_rect = Rect.new(0, 0, cw, ch)
    self.contents.blt(x , y - ch, com, src_rect)   
end
def draw_level(actor, x, y)
    self.contents.font.color = Color.new(238,46,218,255)
    self.contents.draw_text(x + 32, y, 20, 20, actor.level.to_s, 1)
  end 
 
  def draw_com(actor, x, y)
    action = RPG::Cache.picture(actor.name + "_com")
    cw = action.width
    ch = action.height
    src_rect = Rect.new(0, 0, cw, ch)
    self.contents.blt(x , y - ch, action, src_rect)   
  end   
end
def draw_select(x,y)
    com = RPG::Cache.picture("select")
    cw = com.width
    ch = com.height
    src_rect = Rect.new(0, 0, cw, ch)
    self.contents.blt(x , y - ch, com, src_rect)   
  end

there seems to be an error with actor.name, but I have not changed anything about it ... what can it be?
I've also tried to delete new scripts, but nothing =(
Title: Re: Error without explanation.
Post by: ForeverZer0 on October 19, 2014, 07:31:47 pm
The problem isn't with the "name" method, it's the fact that the actor it is trying to reference is nil, which does not have a "name" method, hence the NoMethodError.
Title: Re: Error without explanation.
Post by: KK20 on October 19, 2014, 11:08:15 pm
There's really no way to figure out what is causing the error without seeing the other scripts that are calling your Window_Base methods. For starters, you can try putting
return if actor.nil?

on the line immediately after the method declaration(s) where you are getting errors.

It would also be nice if you had a backtrace implemented.
Title: Re: Error without explanation.
Post by: PrinceEndymion88 on October 20, 2014, 04:36:28 pm
I've found the error in another piece of CCOA script.
This is the original script:
#==============================================================================
# ADD-ON - LAYOUT
#==============================================================================
# By Moghunter
#==============================================================================
class Window_BattleStatus < Window_Base
 def initialize
   super(-20, 330, 680, 180)
   self.contents = Bitmap.new(width - 32, height - 32)
   self.contents.font.name = "Georgia"
   self.contents.font.size = 18
   self.contents.font.bold = true
   self.opacity = 0
   @level_up_flags = [false, false, false, false]
   refresh
 end
 def dispose
   super
 end
 def level_up(actor_index)
   @level_up_flags[actor_index] = true
 end
 def refresh
   self.contents.clear
   self.contents.font.size = 18
   @item_max = $game_party.actors.size
   for i in 0...$game_party.actors.size    
   if $game_switches[5] == true
     actor = $game_party.actors[i]
     actor_x = i * 160 + 12
     actor_y = i * 32
     draw_face2(actor,355 ,actor_y + 38)
     draw_hp2(actor, 360  , actor_y + 5, 120)
     draw_sp2(actor, 480, actor_y + 5, 120)
     if @level_up_flags[i]
       self.contents.font.color = normal_color
       self.contents.draw_text(actor, actor_x + 65, 80, 32, "LEVEL UP!")
     else
       self.contents.font.size = 12
       draw_actor_state(actor, 410, actor_y + 15)
       self.contents.font.size = 18
     end
  else
     actor = $game_party.actors[i]
     actor_x = i * 160 + 12
     draw_lay(actor,actor_x - 5,110)
     draw_face(actor,actor_x,100)
     draw_name(actor, actor_x, 0)
     draw_hp(actor, actor_x, 32, 120)
     draw_sp(actor, actor_x, 64, 120)
     if @level_up_flags[i]
       self.contents.font.color = normal_color
       self.contents.draw_text(actor, actor_x + 65, 80, 32, "LEVEL UP!")
     else
       self.contents.font.size = 12
       draw_actor_state(actor, actor_x + 70, 80)
       self.contents.font.size = 18
     end    
  end
end
end
 def update
   super
 end
end


And this is my edited script:
#==============================================================================
# ADD-ON - LAYOUT
#==============================================================================
# By Moghunter
#==============================================================================
class Window_BattleStatus < Window_Base
 def initialize
   super(-40, 310, 700, 200)
   self.contents = Bitmap.new(width - 32, height - 32)
   self.contents.font.name = "Georgia"
   self.contents.font.size = 18
   self.contents.font.bold = true
   self.opacity = 0
   @level_up_flags = [false, false, false, false]
   refresh
 end
 def dispose
   super
 end
 def level_up(actor_index)
   @level_up_flags[actor_index] = true
 end
 def refresh
   self.contents.clear
   self.contents.font.size = 18
   @item_max = $game_party.actors.size
   for i in 0...$game_party.actors.size    
   if $game_switches[5] == true
     actor = $game_party.actors[i]
     actor_x = i * 180 + 52
     actor_y = i * 28
     draw_face2($game_party.actors[0],59 ,48) #imposta la grafica dei dettagli personaggi in battaglia
     draw_face2($game_party.actors[1],281 ,48) #281
     draw_face2($game_party.actors[2],502 ,48) #502
     draw_face2($game_party.actors[3],177 ,104)
     draw_face2($game_party.actors[4],405 ,104)
     draw_hp2($game_party.actors[0], 64  , 20, 120)
     draw_sp2($game_party.actors[0], 91, 32, 120)
     draw_hp2($game_party.actors[1], 286  , 20, 120)
     draw_sp2($game_party.actors[1], 313, 32, 120)
     draw_hp2($game_party.actors[2], 507  , 20, 120)
     draw_sp2($game_party.actors[2], 534, 32, 120)
     
     draw_hp2($game_party.actors[3], 182  , 76, 120)
     draw_sp2($game_party.actors[3], 209, 88, 120)
     draw_hp2($game_party.actors[4], 410  , 76, 120)
     draw_sp2($game_party.actors[4], 437, 88, 120)
     if @level_up_flags[i]
       self.contents.font.color = normal_color
       self.contents.draw_text(actor, actor_x + 65, 80, 32, "LEVEL UP!")
     else
       self.contents.font.size = 18
       #draw_actor_state(actor, 410, actor_y + 8)
       draw_actor_state($game_party.actors[0], 114, 40)
       draw_actor_state($game_party.actors[1], 336, 40)
       draw_actor_state($game_party.actors[2], 557, 40)
       draw_actor_state($game_party.actors[3], 232, 96)
       draw_actor_state($game_party.actors[4], 460, 96)
       self.contents.font.size = 18
     end
  else
     actor = $game_party.actors[i]
     actor_x = i * 160 + 12
     draw_lay(actor,actor_x - 5,110)
     draw_face(actor,actor_x,100)
     draw_name(actor, actor_x, 0)
     draw_hp(actor, actor_x, 32, 120)
     draw_sp(actor, actor_x, 64, 120)
     if @level_up_flags[i]
       self.contents.font.color = normal_color
       self.contents.draw_text(actor, actor_x + 65, 80, 32, "LEVEL UP!")
     else
       self.contents.font.size = 12
       draw_actor_state(actor, actor_x + 80, 80)
       self.contents.font.size = 18
     end    
  end
end
end
 def update
   super
 end
end


I've used, for example:
     draw_face2($game_party.actors[0],59 ,48)
     draw_face2($game_party.actors[1],281 ,48)
     draw_face2($game_party.actors[2],502 ,48)
     draw_face2($game_party.actors[3],177 ,104)
     draw_face2($game_party.actors[4],405 ,104)

instead of:
draw_face2(actor,355 ,actor_y + 38)

And this cause the error.... Is there a way to fix it? I want to draw my face2, hp2 and s2 element into the specified position. But I don't know why, first time I've used it works and after I've got the error... =(
Title: Re: Error without explanation.
Post by: G_G on October 20, 2014, 04:54:37 pm
Woah there. Hold on a minute. There's a reason why "draw_face2(actor,355 ,actor_y + 38)" was there. The code you have is drawing all actors in your party multiple times. You should have left the line alone. And from what I can see, unless you're using a larger party script, your party shouldn't have more than 4 members and your code is trying to attempt the stats for a 5th actor. So it's your modifications that are messing up.

Second, the code automatically iterates through all actors in your party, thats why it uses "actor" instead of "$game_party.actors". The code your providing makes it so it draws all actors at once, but what if you only have one or two members in your party, your code is attempting to draw an actor that's not even there. And then, you're drawing every actor over and over. So if you have 3 actors, it draws all three actors, three different times because your code is in the "for" loop.

If you want to draw each actor individually and specify exact coordinates, you need to move your code out of the for loop and implement some if branches to see if actor 0, 1, 2, 3, or 4 is actually in the party or not. What I'd recommend though, is changing it all back to what it originally was and mess with the variables "actor_x" and "actor_y" to get the position you need. It'll keep your code much cleaner and less likely to encounter any bugs.
Title: Re: Error without explanation.
Post by: PrinceEndymion88 on October 20, 2014, 05:38:13 pm
My Battle System has 5 members :D However, you're right but I've tried with actor_x and actor_y but every time I replicated graphical element 5 times... I don't know why... I've found this escamotage but it doesn't works XD I want to draw my face2, hp2 and sp2 resources into the position indicated into my wrong script but everytime I tried I've 5 time the same resource for every actor =(...
Title: Re: Error without explanation.
Post by: PrinceEndymion88 on October 21, 2014, 06:47:01 pm
Guys I've edited the piece of script and now the battle system works perfectly :D but what do you think about my edit? Your opinion is important for me, cause you are really good scripter :D

#==============================================================================
# ADD-ON - LAYOUT
#==============================================================================
# By Moghunter
#==============================================================================
class Window_BattleStatus < Window_Base
  def initialize
    super(-40, 310, 700, 200)
    self.contents = Bitmap.new(width - 32, height - 32)
    self.contents.font.name = "Georgia"
    self.contents.font.size = 18
    self.contents.font.bold = true
    self.opacity = 0
    @level_up_flags = [false, false, false, false]
    refresh
  end
  def dispose
    super
  end
  def level_up(actor_index)
    @level_up_flags[actor_index] = true
  end
  def refresh
    self.contents.clear
    self.contents.font.size = 18
    @item_max = $game_party.actors.size
    for i in 0...$game_party.actors.size   
    if $game_switches[5] == true
      actor = $game_party.actors[i]
      actor_x = i * 180 + 52
      actor_y = i * 28
    if i == 0
      draw_face2(actor, 59 ,48)
      draw_hp2(actor, 64  , 20, 120)
      draw_sp2(actor, 91, 32, 120)
    end
    if i == 1
      draw_face2(actor, 281 ,48)
      draw_hp2(actor, 286  , 20, 120)
      draw_sp2(actor, 313, 32, 120)
    end
    if i == 2
      draw_face2(actor, 502 ,48)
      draw_hp2(actor, 507  , 20, 120)
      draw_sp2(actor, 534, 32, 120)
    end
    if i == 3
      draw_face2(actor, 177 ,104)
      draw_hp2(actor, 182  , 76, 120)
      draw_sp2(actor, 209, 88, 120)
    end
    if i == 4
      draw_face2(actor, 405 ,104)
      draw_hp2(actor, 410  , 76, 120)
      draw_sp2(actor, 437, 88, 120)
    end
      if @level_up_flags[i]
        self.contents.font.color = normal_color
        self.contents.draw_text(actor, actor_x + 65, 80, 32, "LEVEL UP!")
      else
        self.contents.font.size = 18
       if i == 0
      draw_actor_state(actor, 114, 40)
    end
    if i == 1
      draw_actor_state(actor, 336, 40)
    end
    if i == 2
      draw_actor_state(actor, 557, 40)
    end
    if i == 3
      draw_actor_state(actor, 232, 96)
    end
    if i == 4
      draw_actor_state(actor, 460, 96)
    end
        self.contents.font.size = 18
      end
   else
      actor = $game_party.actors[i]
      actor_x = i * 160 + 12
      draw_lay(actor,actor_x - 5,110)
      draw_face(actor,actor_x,100)
      draw_name(actor, actor_x, 0)
      draw_hp(actor, actor_x, 32, 120)
      draw_sp(actor, actor_x, 64, 120)
      if @level_up_flags[i]
        self.contents.font.color = normal_color
        self.contents.draw_text(actor, actor_x + 65, 80, 32, "LEVEL UP!")
      else
        self.contents.font.size = 12
        draw_actor_state(actor, actor_x + 80, 80)
        self.contents.font.size = 18
      end     
   end
end
end
  def update
    super
  end
end


This is a screenshot of the battle system:
(http://i57.tinypic.com/5ujocp.png)
Title: Re: Error without explanation.
Post by: KK20 on October 21, 2014, 07:11:46 pm
Fix the tabulation of your code first; you have a lot if-statements which makes it hard to navigate at a glance.

Rather than going

if i == 0
...
if i == 1

You can use a case

case i
when 0
...
when 1
...
end

But in this situation, I'd just initialize some arrays to shorten up the code.

face_coords = [[59, 48], [281, 48], ...]
hp_coords = [[64, 20], [286,20], ...]
sp_coords = [[91,32], [313, 32], ...]
for i in 0...$game_party.actors.size
  ...
  draw_face2(actor, face_coords[i][0], face_coords[i][1])
  draw_hp2(actor, hp_coords[i][0], hp_coords[i][1])
  draw_sp2(actor, sp_coords[i][0], sp_coords[i][1])

Rather than having 5 different evaluation blocks, you only have to put the draw_ commands once. Normally I'd use some kind of formula to calculate the drawing coordinates based on the index value, but it looks like you hand-picked your numbers from thin air.