Flexible way to provide formula variables

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,846
First Language
English
Formula variables is the term I use for any "special variables" that can be used in a formula.

I wrote a brief summary about it here

http://www.himeworks.com/2013/06/04/rgss-formula-variables/

Basically, it's things like in a damage formula, a is the user, b is the target, and v are the game variables.

Currently, all of my scripts hardcode the formula variables into method signatures, and it is impossible to add new variables without basically overwriting the method.

For example, consider the damage formula eval method

def eval(a, b, v=$game_variables) # logicendHow can it be written so that I can easily inject new parameters with default values?For example, I can overwrite the method as this

def eval(a, b, v=$game_variables, s=$game_switches, p=$game_party, t=$game_troop) # logicendBut if you wanted to add one yourself, you wouldn't be able to do it without overwriting my change.At this point, I'm thinking it is not possible given how methods and parameters work, but maybe there's some trick that ruby has that will allow us to accomplish this.
 
Last edited by a moderator:

Yato

(aka Racheal)
Veteran
Joined
Mar 17, 2012
Messages
825
Reaction score
346
Primarily Uses
Why not just make methods in the same class as eval to reference this new data? Something like

def s; $game_switches; end should work the same as passing it as an instance variable.Would there be any reason to set it to a non-default value? That would be an issue with this method.
 
Last edited by a moderator:

MagicMagor

Veteran
Veteran
Joined
Apr 7, 2012
Messages
201
Reaction score
38
First Language
German
Hm.. maybe something like this:

module FormulaV  @data = { :var => $game_variables,  :switch => $game_switches}end # Somewhere elsedef eval(a, b, d=FormulaV::data)v=d[:var]# LogicendAdding new types of these variables would just mean adding to the hash. And possibly adding a line to read the variable out of the hash into a local variable.

I'm pretty sure there exist a meta-programming way to create local variables with a default-value, so that adding a line to eval could be avoided, but it has been a long time since i dabbled in the meta-programming of ruby.
 

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,846
First Language
English
Why not just make methods in the same class as eval to reference this new data? Something like

def s; $game_switches; endshould work the same as passing it as an instance variable.
Would there be any reason to set it to a non-default value? That would be an issue with this method.
I didn't think of approaching it as methods defined in the class, but it would work well.


When you put it that way, there are two types of formula variables


1. Ones that have constant references. eg: s always refers to $game_switches within the context of the particular eval method.


2. Ones that have variable references. eg: the calling method passes in an object such as the current battler


Defining methods would take care of #1 easily, and anyone can introduce their own methods without having to do much work: just throw in a definition and it's ready to go.


FOr #2, if someone actually wanted to pass in more arguments, something along the lines of what MagicMagor is going with may be required.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Profile Posts

Do you Find Tilesetting or Looking for Tilesets/Plugins more fun? Personally I like making my tileset for my Game (Cretaceous Park TM) xD
How many parameters is 'too many'??
Yay, now back in action Happy Christmas time, coming back!






Back in action to develop the indie game that has been long overdue... Final Fallacy. A game that keeps on giving! The development never ends as the developer thinks to be the smart cookie by coming back and beginning by saying... "Oh bother, this indie game has been long overdue..." How could one resist such? No-one c
So I was playing with filters and this looked interesting...

Versus the normal look...

Kind of gives a very different feel. :LZSexcite:

Forum statistics

Threads
105,854
Messages
1,016,998
Members
137,562
Latest member
tamedeathman
Top