The RPG Maker Resource Kit

RMRK RPG Maker Creation => RPG Maker General => General Scripting => Topic started by: shintashi on November 19, 2010, 04:26:43 AM

Title: how do I deal with a null?
Post by: shintashi on November 19, 2010, 04:26:43 AM
I have a simple script for determining if you are using weapon A or weapon B, and it displays some info, I know there's going to be a "nil" result for atk  but I don't know how to address that fact and say "draw text nothing"

My script is as follows.

      if wpn_num >= 17
  self.contents.draw_text(484, 256, 96, 32, $data_weapons[wpn_num].atk.to_s)
    self.contents.font.color = system_color
  self.contents.draw_text(524, 224, 96, 32, "Rng")
    self.contents.font.color = normal_color
  self.contents.draw_text(576, 224, 64, 32, $ABS.RANGE_WEAPONS[wpn_num][4].to_s)
     else
if $data_weapons[wpn_num].atk != nil       
       self.contents.draw_text(469, 256, 96, 32, "+ " + $data_weapons[wpn_num].atk.to_s)
     else
       self.contents.draw_text(469, 256, 96, 32, "")
      end
       end


i'm not sure I parsed my "if" statement correctly either, actually.
Title: Re: how do I deal with a null?
Post by: Shinami on November 19, 2010, 07:21:30 AM
You might want to try something like "unless weapon is nil?" to keep the code in the "unless" branch from running unless there's a valid weapon equipped.
Title: [Resolved] how do I deal with a null?
Post by: shintashi on November 19, 2010, 12:07:54 PM
It was my fault not realizing "no weapon" is actually registered as "0" and not nil. the thing that is nil is the base atk of the weapon 0. I think it's probably possible to even modify the stats of your "0" weapon, which could be turned into a series of martial arts - but you didn't hear that from me...

So here's my fixed code counting in "weapon 0".
if wpn_num >= 17
  self.contents.draw_text(484, 256, 96, 32, $data_weapons[wpn_num].atk.to_s)
    self.contents.font.color = system_color
  self.contents.draw_text(524, 224, 96, 32, "Rng")
    self.contents.font.color = normal_color
  self.contents.draw_text(576, 224, 64, 32, $ABS.RANGE_WEAPONS[wpn_num][4].to_s)
elsif wpn_num < 17 && wpn_num > 0 
  self.contents.draw_text(469, 256, 96, 32, "+ " + $data_weapons[wpn_num].atk.to_s)
else
  self.contents.draw_text(328, 256, 112, 32, "Unarmed: ")
  self.contents.draw_text(469, 256, 96, 32, "No Bonus")
end