Adding a Custom Notetag
Sometimes you may want to add extra data to actors or items or whatever for eventing, or... something else. I don't know, but it's useful to know anyways in case there ever comes a situation where it would be helpful. (Like having certain equip items do things like avoid evented trap damage or allow passage through certain areas, or wear brown shoes.) It's quite easy to do!
Let's start with an example:
Explained line by line, line 1 is the class definition for whichever class you wish to add a notetag to (RPG::Base_Item will apply the tag to well... everything, but if you want to be more specific you can use RPG::Actor, RPG::Enemy, RPG::EquipItem, etc...). Line 2 is the method definition, and can be whatever word you want, probably something relevant and easy to remember. Line 4 and 5 are necessary to close the definitions. And line 3...
Line 3 is where the magic happens. For the above example, it's a simple statement with no extra data. self.note =~ tells it to preform a regex expression.. thingy, no need to know what that means, /something/ is what it checks for (<MALE> in the above example), It doesn't have to have the <> and can be whatever you want it to be, unless what you want it to be has forward slashes. It will check for exactly whatever you put between / and /. And the final part ? expr1 : expr2, is where the result is returned. If the exact string was found then it will turn expr1 (true in the above example) and if not expr2 (false in the above example). And that's a simple no data note tag.
But what if you need more data! Maybe you want a note tag that return a different and specific value for different actors, or return a special string. Little tougher, but not that hard. Again, let's start with an example:
Similar to the first example, but with a few different things on line 3, as you can see. The new piece of data we added in the /something/ is (\d+), and without explaining that too much, it merely checks for any number of digits between <ILVL and >. It will only match and return expr1 if it finds that tag with a number(s) in the right spot. Then to return that number we use the global variable $1 (the first match in a regex expression) and use .to_i to convert it into an integer, then viola. You can use a script call to get the item_level of the object! expr2 is 0 so we have a default value to return in case no notetag is found.
If you want a string you would use (.+) instead and you wouldn't convert it into an integer, instead just returning $1. If you wanted to get more then one piece of data you can just add more (\d+) or (.+), separated by something simple like a comma. Instead of returning merely $1 in this case, you would return as expr1 [$1,$2,$3,...] however many you have (and .to_i if it's numbers). This returns an array which you just add [index] to the end of your script call to access certain values from the array ([0] for the first item, [1] for the second, etc.. using the previous example it would be similar to $game_actors[actor_id].actor.gender[0] ).
Additional Info:
Not going to go into detail with this, but here's some examples to help you get started. (Just wrote these in here and assume they work.)
This first one will return the average item level of the actor, based on the item_level notetags.
This second one returns true if any equipped item has the NOTRAPS tag:
Let's start with an example:
- class RPG::Base_Item
- def gender
- self.note =~ /<MALE>/ ? true : false
- end
- end
Explained line by line, line 1 is the class definition for whichever class you wish to add a notetag to (RPG::Base_Item will apply the tag to well... everything, but if you want to be more specific you can use RPG::Actor, RPG::Enemy, RPG::EquipItem, etc...). Line 2 is the method definition, and can be whatever word you want, probably something relevant and easy to remember. Line 4 and 5 are necessary to close the definitions. And line 3...
Line 3 is where the magic happens. For the above example, it's a simple statement with no extra data. self.note =~ tells it to preform a regex expression.. thingy, no need to know what that means, /something/ is what it checks for (<MALE> in the above example), It doesn't have to have the <> and can be whatever you want it to be, unless what you want it to be has forward slashes. It will check for exactly whatever you put between / and /. And the final part ? expr1 : expr2, is where the result is returned. If the exact string was found then it will turn expr1 (true in the above example) and if not expr2 (false in the above example). And that's a simple no data note tag.
But what if you need more data! Maybe you want a note tag that return a different and specific value for different actors, or return a special string. Little tougher, but not that hard. Again, let's start with an example:
- class RPG::Base_Item
- def item_level
- self.note =~ /<ILVL (\d+)>/ ? $1.to_i : 0
- end
- end
Similar to the first example, but with a few different things on line 3, as you can see. The new piece of data we added in the /something/ is (\d+), and without explaining that too much, it merely checks for any number of digits between <ILVL and >. It will only match and return expr1 if it finds that tag with a number(s) in the right spot. Then to return that number we use the global variable $1 (the first match in a regex expression) and use .to_i to convert it into an integer, then viola. You can use a script call to get the item_level of the object! expr2 is 0 so we have a default value to return in case no notetag is found.
If you want a string you would use (.+) instead and you wouldn't convert it into an integer, instead just returning $1. If you wanted to get more then one piece of data you can just add more (\d+) or (.+), separated by something simple like a comma. Instead of returning merely $1 in this case, you would return as expr1 [$1,$2,$3,...] however many you have (and .to_i if it's numbers). This returns an array which you just add [index] to the end of your script call to access certain values from the array ([0] for the first item, [1] for the second, etc.. using the previous example it would be similar to $game_actors[actor_id].actor.gender[0] ).
Additional Info:
Not going to go into detail with this, but here's some examples to help you get started. (Just wrote these in here and assume they work.)
This first one will return the average item level of the actor, based on the item_level notetags.
- class Game_Actor
- def avg_item_level
- ilvl = 0
- equips.each do |item|
- next if item.nil?
- ilvl += item.item_level
- end
- ilvl / equips.size
- end
- end
- class RPG::EquipItem
- def item_level
- self.note =~ /<ILVL (\d+)>/ ? $1.to_i : 0
- end
- end
This second one returns true if any equipped item has the NOTRAPS tag:
- class Game_Actor
- def notraps?
- equips.each do |item|
- next if item.nil?
- return true if item.notraps
- end
- return false
- end
- end
- class RPG::EquipItem
- def notraps
- self.note =~ /<NOTRAP>/ ? true : false
- end
- end