Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

A little more advanced scripting question

Dko

Member

Ok I want to make method for a variable of mine. A table to be exact. So that my class knows how to set its value like thus.
Code:
#psuedocode
@variable = Table.new(5,6) #example table

def variable=(var)
  @variable[#, #] = var
end

The problem is that any Table objects are basically multidimensional arrays and I don't know how to adjust the variable method so it knows which[#, #] I have specified. Or how to do this with any kind of array. Some help would be nice sence this doesn't seam coverd in most tutorials.

In the end I want to make sure I can do this: Base_class.variable[a,b] = 5
 

Dko

Member

BUt im getting errors saying the method doesn't exist when I do just that.
Here is what causes the error.
Code:
 $game_actors[@actor_id].parameters[6, @level] = 5

and it gets me the error

undefined method 'parameters' for #Game_Actor:0x488d9d8
 

Dko

Member

Well there is some sort of big problem im having. I try to use $data_actors and it returns nil and I cant use $Game_actors like you said. I can't get it to work for the life of me. This is getting me so frustrated. Adding another character stat shouldn't be so fricken hard. Especialy with Ruby. Im about ready to quit
 
Ok As Seen In My Stats that Level Up Script
Code:
class Scene_Title
  #--------------------------------------------------------------------------
  # * Main Database
  #--------------------------------------------------------------------------
  alias stats_main_database main_database
  def main_database
    stats_main_database
    $data_actors.each {|actor| actor.setup_stats if actor != nil}
  end
end

module RPG
class Actor
  #--------------------------------------------------------------------------
  # * Include Stats
  #--------------------------------------------------------------------------
  include Stats
  #--------------------------------------------------------------------------
  # * Setup Stats
  #--------------------------------------------------------------------------
  def setup_stats
    # Setup New Table
    parameters = Level_Table.new(10, final_level < 100 ? 100 : final_level+1)
    # Run Through and Copy From Parameters
    @parameters.ysize.times do |y|
      @parameters.xsize.times do |x|
        parameters[x, y] = @parameters[x, y]
      end
    end
    # Setup New Parameters
    @parameters = parameters
    # Get Curves
    hp = Hp_Curve_Actor[id].nil? ? Hp_Curve_Class[class_id] : Hp_Curve_Actor[id]
    sp = Sp_Curve_Actor[id].nil? ? Sp_Curve_Class[class_id] : Sp_Curve_Actor[id]
    str = Str_Curve_Actor[id].nil? ? Str_Curve_Class[class_id] : Str_Curve_Actor[id]
    dex = Dex_Curve_Actor[id].nil? ? Dex_Curve_Class[class_id] : Dex_Curve_Actor[id]
    agi = Agi_Curve_Actor[id].nil? ? Agi_Curve_Class[class_id] : Agi_Curve_Actor[id]
    int = Int_Curve_Actor[id].nil? ? Int_Curve_Class[class_id] : Int_Curve_Actor[id]
    atk = Atk_Curve_Actor[id].nil? ? Atk_Curve_Class[class_id] : Atk_Curve_Actor[id]
    pdef = PDef_Curve_Actor[id].nil? ? PDef_Curve_Class[class_id] : PDef_Curve_Actor[id]
    mdef = MDef_Curve_Actor[id].nil? ? MDef_Curve_Class[class_id] : MDef_Curve_Actor[id]
    eva = Eva_Curve_Actor[id].nil? ? Eva_Curve_Class[class_id] : Eva_Curve_Actor[id]
    # Setup Stats Array
    stats = %w( hp sp str dex agi int atk pdef mdef eva )
    # Run Through each Stat
    stats.each do |stat|
      eval("#{stat} = Level_Generation.new(*#{stat})") if eval(stat) != nil
    end
    # Run Through Each Level
    (1..final_level).each do |level|
      # Run Through each stat
      stats.each_with_index do |stat, index|
        if eval(stat) == nil and index <= 5
          @parameters[index, level] = @parameters[index, 99] if level > 99
          next
        elsif eval(stat) == nil and index > 5
          @parameters[index, level] = 0
        else
          eval("@parameters[index, level] = #{stat}.get_stat(level)")
        end
      end
    end
  end

and In Game_Actor
Code:
  #--------------------------------------------------------------------------
  # * Get Basic Attack Power
  #--------------------------------------------------------------------------
  alias generate_base_atk base_atk
  def base_atk
    n = generate_base_atk
    n += $data_actors[@actor_id].parameters[6, @level]
    return n
  end
  #--------------------------------------------------------------------------
  # * Get Basic Physical Defense
  #--------------------------------------------------------------------------
  alias generate_base_pdef base_pdef
  def base_pdef
    n = generate_base_pdef
    n += $data_actors[@actor_id].parameters[7, @level]
    return n
  end
  #--------------------------------------------------------------------------
  # * Get Basic Magic Defense
  #--------------------------------------------------------------------------
  alias generate_base_mdef base_mdef
  def base_mdef
    n = generate_base_mdef
    n += $data_actors[@actor_id].parameters[8, @level]
    return n
  end
  #--------------------------------------------------------------------------
  # * Get Basic Evasion Correction
  #--------------------------------------------------------------------------
  alias generate_base_eva base_eva
  def base_eva
    n = generate_base_eva
    n += $data_actors[@actor_id].parameters[9, @level]
  end

Ok I'll explain The Main Database method is a method that is defined in the SDK and does the database loading functions. After that is done I run through $data_actors using Array#each and calling setup_stats if the actor is not nil.

The Setup_Stats Method I changed the class the variable @parameters was using into a Level_Table (The Table class has value restrictions of -2^15 to 2^15-1 which is -32768 to 32767). The Level_Table class I created acts just like a table.

I first created a local variable, loaded the old stuff into the local variable and then set the instance variable to the local variable. All of the Curves are loaded and the Level_Generation class is created and used to generate the stats, which is then loaded into @parameters in the proper place if it was defined

Now in Game_Actor in the methods shown above the values are loaded from the the actor object's parameters

I hope I explained this well enough for you if there is something you don't get from my explanation don't hesitate to ask
 
No, Main Database is only defined if you have the SDK

Try this (Since Your error might be my fault)

instead of aliasing main alias command_new_game and command_continue performing the changes to $data_actors first then calling the old method the code when written should look similar to this

Code:
alias dko_command_new_game command_new_game
def command_new_game
  $data_actors.each {|actor| actor.setup_new_stats if actor != nil}
  dko_command_new_game
end

Code:
alias dko_command_continue command_continue
def command_continue
  $data_actors.each {|actor| actor.setup_new_stats if actor != nil}
  dko_command_continue
end
 
I didn't read the whole thing but to answer your first question about your method, look at the code :
Code:
class Whatever
  #---
  def initialize
    @variable = Table.new(5, 6) 
  end
  #---
  def []=(x, y, value)
    @variable[x, y] = value
  end
end

blah = Whatever.new
blah[0, 0] = 120
 

Dko

Member

Thanks a lot everyone. I took Tricksters changes and boiled them down to just what I need like this.
Code:
class Scene_Title
  #--------------------------------------------------------------------------
  # * Main Database
  #--------------------------------------------------------------------------
  alias stats_main_database main_database
  def main_database
    stats_main_database
    $data_actors.each {|actor| actor.setup_stats if actor != nil}
  end
end

module RPG
  class Actor
    def setup_stats
      # Setup New Table
      parameters = Table.new(7, 100)
      # Run Through and Copy From Parameters
      @parameters.ysize.times do |y|
        @parameters.xsize.times do |x|
          parameters[x, y] = @parameters[x, y]
        end
      end
      # Setup new parameters
      @parameters = parameters
    end
  end
end
and now I can do exactly what ive been trying to do with my characters stats. Im going to have it so my new stat can be set in the debug menu.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top