The RPG Maker Resource Kit

RMRK RPG Maker Creation => RPG Maker General => General Scripting => Topic started by: Aegof on December 28, 2009, 12:48:08 AM

Title: Adding an object to an actor.
Post by: Aegof on December 28, 2009, 12:48:08 AM
Being new to Ruby and RGSS2, I figured that I'd start by doing something simple but useful and make some more parameters for my actors.  I want to make an object (ActorPlus) with the parameter data (and eventually other stuff,) and attach it to the actor in it's setup.

Here's the code, divided into four parts because that's how I've got it set up and it's easy to sort through like this.


Here's ActorPlus and some Vocab stuff:
[spoiler]module RPG
  class Actor
    class ActorPlus   
      attr_accessor :paramaplus
 
      def initialize
        @paramaplus = Table.new(2, 100)   #The table for holding (parameters, levels)
        for i in 1..99
            @paramaplus[0,i] = 10+i         #Charisma
            @paramaplus[1,i] = 10+i          #Luck
            #these formulas might be changable with if (actor_id) statements?
        end
 
      end #end initialize

   
    end #end ActorPlus
  end #end Actor


     
   
  #class StatePlus
   #I'll deal with this later, when I've a better idea how.
   

 
  class System
    class TermsPlus
      attr_accessor :cha
      attr_accessor :luk
      def initialize
        @cha = "CHA"
        @luk = "LUK"
      end
    end
  end #end System

end #end RPG








module Vocab
  #Charisma
  def self.cha
    return $data_system.termplus.cha    #looking at this again, I may have misunderstood
  end                                   #what $data_system was when I wrote this
 
  #Luck
  def self.luk
    return $data_system.termsplus.luk
  end
end
[/spoiler]

A small addition and change in Game_Battler:
[spoiler]#--------------------------------------------------------------------------
# * Clear Values Added to Parameter
#   insert "clear_extra_values_plus" at line 45 in Game_Battler
#                                           
#--------------------------------------------------------------------------
 
class Game_Battler
  def clear_extra_values_plus
    @cha_plus = 0
    @luk_plus = 0
  end
end
[/spoiler]

The actual error message points here: setup() in Game_actor, on line 34, marked with a comment for your convienence.
[spoiler]#setup() is overwritten; nothing else is


class Game_Actor < Game_Battler


  #--------------------------------------------------------------------------
  # * Setup
  #     actor_id : actor ID
  #------------------------------------------------------------------------- 
  def setup(actor_id)
   
    actor = $data_actors[actor_id]
    @actor_id = actor_id
    @name = actor.name
    @character_name = actor.character_name
    @character_index = actor.character_index
    @face_name = actor.face_name
    @face_index = actor.face_index
    @class_id = actor.class_id
    @weapon_id = actor.weapon_id
    @armor1_id = actor.armor1_id
    @armor2_id = actor.armor2_id
    @armor3_id = actor.armor3_id
    @armor4_id = actor.armor4_id
    @level = actor.initial_level
    @exp_list = Array.new(101)
    make_exp_list
    @exp = @exp_list[@level]
    @skills = []
    for i in self.class.learnings
      learn_skill(i.skill_id) if i.level <= @level
    end
    aplus = actorplus                                                       #The Problem Line.
    clear_extra_values
    clear_extra_values_plus
    recover_all
  end



  #I haven't looked into equipment yet, and states are beyond me, so they're
  #commented out for the time being.
 
 
 
  #--------------------------------------------------------------------------
  # * Get Basic Charisma
  #--------------------------------------------------------------------------
  def base_cha
    n = actor.aplus.paramaplus[0, @level] 
#    for item in equips.compact do n += item.agi end
    return n
  end
 
    #--------------------------------------------------------------------------
  # * Get Basic Luck
  #--------------------------------------------------------------------------
  def base_luk
    n = actor.aplus.paramaplus[1, @level]
#    for item in equips.compact do n += item.agi end
    return n
  end
 
 
  #--------------------------------------------------------------------------
  # * Get Charisma
  #--------------------------------------------------------------------------
  def cha
    n = [[base_cha + @cha_plus, 1].max, 999].min
#    for state in states do n *= state.cha_rate / 100.0 end
    n = [[Integer(n), 1].max, 999].min
    return n
  end 
 
  #--------------------------------------------------------------------------
  # * Get Luck
  #--------------------------------------------------------------------------
  def luk
    n = [[base_luk + @luk_plus, 1].max, 999].min
#    for state in states do n *= state.luk_rate / 100.0 end
    n = [[Integer(n), 1].max, 999].min
    return n
  end
 
  #--------------------------------------------------------------------------
  # * Set Charisma
  #     new_cha : new charisma
  #--------------------------------------------------------------------------
  def cha=(new_cha)
    @luk_plus += new_cha - self.cha
    @cha_plus = [[@cha_plus, -999].max, 999].min
  end 

  #--------------------------------------------------------------------------
  # * Set Luck
  #     new_luk : new luck
  #--------------------------------------------------------------------------
  def luk=(new_luk)
    @luk_plus += new_luk - self.luk
    @luk_plus = [[@luk_plus, -999].max, 999].min
  end
end 
[/spoiler]


And here are some changes to windows so I can get the numbers to show up on a status screen:
[spoiler]#these are all straight-up overwritten


class Window_Base < Window
  #--------------------------------------------------------------------------
  # * Draw Parameters
  #     actor : actor
  #     x     : draw spot x-coordinate
  #     y     : draw spot y-coordinate
  #     type  : Type of parameters (0-3)
  #--------------------------------------------------------------------------
  def draw_actor_parameter(actor, x, y, type)
    case type
    when 0
      parameter_name = Vocab::atk
      parameter_value = actor.atk
    when 1
      parameter_name = Vocab::def
      parameter_value = actor.def
    when 2
      parameter_name = Vocab::spi
      parameter_value = actor.spi
    when 3
      parameter_name = Vocab::agi
      parameter_value = actor.agi
    when 4
      parameter_name = Vocab::cha
      parameter_value = actor.cha
    when 5
      parameter_name = Vocab::luk
      parameter_value = actor.luk
    end
    self.contents.font.color = system_color
    self.contents.draw_text(x, y, 120, WLH, parameter_name)
    self.contents.font.color = normal_color
    self.contents.draw_text(x + 120, y, 36, WLH, parameter_value, 2)
  end
end


class Window_Status < Window_Base
 
 
   #--------------------------------------------------------------------------
  # * Draw Parameters
  #     x : Draw spot X coordinate
  #     y : Draw spot Y coordinate
  #     slot, stat
  #--------------------------------------------------------------------------
  def draw_parameters(x, y)
    draw_actor_parameter(@actor, x, y + WLH * 0, 0)
    draw_actor_parameter(@actor, x, y + WLH * 1, 1)
    draw_actor_parameter(@actor, x, y + WLH * 2, 2)
    draw_actor_parameter(@actor, x, y + WLH * 3, 3)
    draw_actor_parameter(@actor, x, y + WLH * 4, 4)#  CHA
    draw_actor_parameter(@actor, x, y + WLH * 5, 5)#  LUK
  end
end
[/spoiler]

(I overrwrite a lot of stuff, mostly because I haven't found an explanation of this whole "alias" buisness that I felt like I understood.)