[ACE] SO YOU PROS, HOW WOULD ONE GO ABOUT ADDING NEW VARIABLES/STATS TO A CHARACTER CLASS IN ACE?

Posts

Pages: 1
I was toying with idea of adding some variables to a character / class in VX Ace that would make it convenient for me in-game to manage characters. For example, I wanted to add some stats like Awareness, Demolitions, Stealth, Security etc.. that I could reference in events or battle formulas. Like say there is a lock in the game and Character X is the leader of the party, check to see if CharX.security > 5 then unlock door! That kind of thing.

I don't even know where to begin looking to accomplish this though. (And not in the 'durr read the helpfile' way, I mean, what classes do I inherit from/overload/how-do-I-do-this in Ruby kind of way)

Any guidance from the gurus here?
Assuming you only want it for player characters, Game_Actor is the place you're looking to start adding stuff to.

I don't know 100% if this is the proper way, but I'd go about it by adding a new attribute, probably one for each, most likely an attr_reader, and then create methods within the Game_Actor class to both read and set the value for each attribute. Then you could do something like...

Conditional (Script) $game_party.members.getSecurity() >= 10

Now, would they be learning these skills through in-game methods (ie. you train them as a player) or through leveling up/part of their normal class kit?
Yellow Magic
Could I BE any more Chandler Bing from Friends (TM)?
3229
author=Travio
I don't know 100% if this is the proper way, but I'd go about it by adding a new attribute, probably one for each, most likely an attr_reader, and then create methods within the Game_Actor class to both read and set the value for each attribute.
Why just attr_reader? Don't you need attr_accessor to be able to write to a variable as well? (Ruby beginner here)

Also, if you use attr_accessor I thought you didn't need to write a get method?
I'm not sure on the standard Ruby protocol, but it's just long standing programming paradigm that I've been taught: only an object/class should ever access its own variables. An attr_reader (from what I can tell) can be modified by the object itself - an attr_accessor can be modified from outside (please, correct me if I'm wrong on Ruby conventions; this particular differentiation confuses me at times). From what I can tell, Game_BattlerBase follows this pattern as well - while other methods outside of the class calculate values (such how much damage to take), they pass those values to methods within the Game_BattlerBase class to handle.

As an example, it's easier to compartmentalize your sanity testing if only a class can access its own variables.
I do mine similar to what Travio said, except I place them in Game_BattlerBase, since it was a stat for both actors and enemies. Made an attr_reader and a few methods.
@Yellow Magic, Yes that's correct, accessor does reader and write, and adds both their methods. So I assume accessor would work as well. But I went with reader just because that's how a similar stat was written :P
Yeah, I was hoping to be able to increment those values as the player levels up, but I haven't thought about how I'd want to do that. Probably by getting the player to distribute points in a menu or somesuch.

So, by using GameActor, does that mean each character would have their own particular value? Or what does $game_party.members.getSecurity() >= 10 mean? (mostly the .members thing). I would have assumed it'd be like $game_party.charactersorwhatever.getSecurity() >= 10 to check the second team member's security score, as an example.

E:
ok, I used squarebrackets and then realized that they don't show in RMN's text because the system assumes it is a BBC tag. welp. So that's why I misunderstood you.

$game_party.charactersorwhatever[1].getSecurity() >= 10
Game_BattlerBase and Game_Battler have the shared code of both actors (player characters) and enemies. I don't know why its split in two classes like that, feels like overzealous polymorphism but I haven't cared to really look into it. For a new stat I'd recommend making a new method in one of those two that generates a base value. I'm going to do.... constitution:

class Game_Battler < Game_BattlerBase
  def base_constitution
    return @level * 2
  end

  def constitution
    return base_constitution
  end
end
Now all actors and enemies have a constitution stat equal to double their level. As it is it can't be affected by anything like states or equipment. I have two separate methods for future purposes: Base_Constitution will include all additive bonuses such as +1-kind of equipment or stat-up items while Constitution will use Base_Constitution and apply multiplier effects like states and *2-kind of equipment. To differentiate actors we'll let them have a constitution bonus per level set by each actor and we need to compensate for equipment too. We also need to store the stat bonus through permanent upgrade items.

class Game_Actor < Game_Battler

  # The bonus is just a simple int, this will make it completely public and easy to modify
  attr_accessor :constitution_bonus

  # We will want to initialize the bonus though so do so during setup.
  # We use alias so we can call the original setup method easily while still replacing the original setup with our custom method here
  alias :setup_constitution_ori :setup unless $@
  def setup(*args)
    # Setup everything else first
    setup_constitution_ori
    # Now initialize our constitution bonus to zero
    @constitution_bonus = 0
  end

  # We don't have to initialize it, we could just check if it is nil before we use it
  # but the alias here is quick and simple and good to know how it works for the future

  def base_constitution
    c = super # Get the base constitution
    # Get the constitution bonus per level from the actor object
    level_bonus = @level * $data_actors[@actorid].constitution
    # Get the equipment bonus too. This will sum up the value of all equipped item's constitution score
    equip_bonus = @equips.inject(0) { |total, item| total += item.constitution }
    # Add the level + equipment bonus and the constitution bonus to constitution and return it
    return c + level_bonus + equip_bonus + @constitution_bonus
  end
end

Of course now there's more work to do! The actor class we refer to is the RPG::Actor which is the database model of an actor and what you set in the editor. Same with equipment (RPG::Weapon and RPG::Armor which both derive from RPG::EquippableItem iirc). We need to expand these so they can retrieve a constitution score somewhere. Generally the notebox (fuck the notebox but we'll use it here anyways).

Actually I think RPG::Actor derives from RPG::BaseItem too... we'll assume it does. If so we can just make the modification in it and have the change propagate to everything that extends the class (including weapons and armor... but not states iirc).
class RPG::BaseItem

  # We'll use a regex to find the constitution from the notebox. The slashes here denote that it is a regex object
  # ps fuck regexs too, I am awful at them. This one is simple at least (but inflexible)
  Constitution_Regex = /<constitution: (\d+)>/i
  # ps the starting capital makes this a constant

  def constitution
    # Constitution isn't set so it's nil the first time and we need to pull it from the notebox
    if @constitution.nil?
      # Try to match the notebox to our regex. The =~ operator will do that and assign matches to $1, $2, $3, etc. which is handy
      if @notebox =~ Constitution_Regex
        @constitution = $1.to_i # ($1 is a string, make it a number so we can math it)
      else
        # No regex was found so assign @constitution to 0 so we don't do this again
        @constitution = 0
      end
    end

    return @constitution
  
  end

end

e: Fixed a typo
Hopefully this is a handy start but lunch break ended a while ago so back to work~
Oh thanks this is a sweet framework to work with. I will digest and play around with this soon! I am sure to have questions...

E:
question: You have Constitution_Regex, but then later on you have =~ Regex
Is this some sort of Ruby shortcut or is that just a typo?
I'm just dumb, I'll fix that. It should be Constitution_Regex.

e: There's probably a lot of little typos in there since it's all from memory.
Pages: 1