Add a new "Accessible" battler thingy

So, I know this is monkey code... but this works to show the actor's elemental resistance to Fire (elementID 3):

act = $game_actors[1]$game_variables[5] = act.element_rate(3)... and then I just text out game variable 5 and it shows the correct, combined element rate for that element ID.

what I want is for this to be accessible like hp, agi, luk, etc are:

act.lukI see many of the parameters are in Game_BattlerBase like so:

def luk;  param(7);   end               # LUK  LUcKBut when I mashed the keyboard like an ape trying to get my simple code in there, I get nuttin'.

My end goal is to add this in to Yanfly's Status menu... like so:

PROPERTIES_COLUMN1 =[      [:eva, "Evasion"],      [:cri, "Critical Hit"],      [:cev, "Critical Evade"],     ] Thanks in advance... 
So what are you actually asking for?


$game_actors[id].element_rate(3) should work. What happens when you try it? Do you get an error message? Does the actor HAVE an element rate for fire?
def res_fire; element_rate(3); end


then call it like


act.res_fire
You know... I swear I do spend hours trying this on my own before posting... 

... and after posting, invariably, I figure it out on my own or with the help of you fine folk. 

All set here. please close. sorry!

EDIT: thanks adik... that's another way to get at it!
You can also use this script (place it above Main Process and below Materials in the script editor):

class Game_BattlerBase #-------------------------------------------------------------------------- # * ALIAS Object Initialization #-------------------------------------------------------------------------- alias_method :elemental_attributes_dynamic_initialize, :initialize def initialize elemental_attributes_dynamic_initialize $data_system.elements.each_with_index do |element, index| # First we downcase caps letters and substitute spaces with underscores element = element.downcase.gsub(" ", "_") # We then create a new accessor method for the class: # The element "Physical" can now be called with ele_physical # and "Two Words" would be called with ele_two_words define_singleton_method("ele_#{element}") do element_rate(index) end end endendIt automatically creates accessor methods for all the elements defined in your database. So the element Physical can be accessed by calling ele_physical. If there are spaces in the element name, they're converted to underscores. So "Holy Power" would be ele_holy_power 🙂

The benefit is that you now don't have to manually add these methods yourself when you add new elements or rename them in your database.

If you use them in Yanfly's Status Menu, you can use these as symbols just like the other properties, so you can write :ele_physical and so on.
@Galenmereth -

I would recommend against using that approach to adding the methods, as singletons can't be marashalled so you would run into a problem when you try to save the game (Game_BattlerBase is used in Game_Actor). Plus, you are performing method definitions many times more than necessary, since you are doing it individually for every BattlerBase object when it only needs to be once per element, for the class as a whole.

You can still perform this as metacode, for example, if you were to alias into load_database and send the definitions to Game_BattlerBase right afterward:

Like, say:

Code:
class << DataManager  alias load_database_parse_elements load_database   def load_database    load_database_parse_elements      $data_system.elements.each_with_index { |element, index|        element = element.downcase.gsub(" ", "_")        Game_BattlerBase.send(:define_method, "ele_#{element}") {          element_rate(index) }        }  end      end
Or actually this might be better (I didn't bench either one, but #send has slowed other methods I have benched, not like you should see any difference here):
Code:
class << DataManager  alias load_database_parse_elements load_database   def load_database    load_database_parse_elements       Game_BattlerBase.class_eval {         $data_system.elements.each_with_index { |element, index|        element = element.downcase.gsub(" ", "_")        define_method("ele_#{element}")  {          element_rate(index)           }          }      }  end      end
@Mithran: You're absolutely right. I'd say your second script snippet there is the definitive version. I'm not sure what I was thinking with that define_singleton_method call in the initialize method; guess that's what I get for hammering it out quickly in the middle of the night 😀

Original thread: https://forums.rpgmakerweb.com/threads/add-a-new-accessible-battler-thingy.20841/