Checking in a script if a given state is in effect.

qwertyu63

Villager
Member
Joined
Nov 30, 2013
Messages
8
Reaction score
2
First Language
English
Primarily Uses
I'm working on a mini script and I've hit something I don't know how to do. I'm looking for a conditional that checks if the target of an action has a specific state and effects the hit rate. I'll write what I have so far. The underlined part is just pseudocode, since that's what I don't know. I'm pretty sure the rest should work fine.

class Game_Battler < Game_BattlerBase
def item_hit(user, item)
rate = 1.0
rate = 0.5 if target.has_state[5]? # Lower hit rate to 50% if target has specific state.
return rate
end
end

So, yeah. I just need to know what to replace the underlined part with.
 

kyonides

Veteran
Veteran
Joined
Nov 17, 2019
Messages
155
Reaction score
32
First Language
English
Primarily Uses
RMXP
You can also write it like this:

class Game_Battler < Game_BattlerBase
def item_hit(user, item)
rate = target.state?(5)? 0.5 : 1.0 # Lower hit rate to 50% if target has specific state.
end
end

return is unnecessary since rate is the last value read or calculated in that method of yours. It's only convenient if you just need to check 2 different possibilities. You can also include a third by inserting a secondary ternary condition (it's what I've posted above) but usually you wouldn't need to do that.

Just in case I'd like to remind you that you can also rely on alias to keep the method's original code working while you can still add new functionality like that rate.
 

TheoAllen

Self-proclaimed jack of all trades
Veteran
Joined
Mar 16, 2012
Messages
5,437
Reaction score
6,254
First Language
Indonesian
Primarily Uses
RMVXA
class Game_Battler < Game_BattlerBase
def item_hit(user, item)
rate = target.state?(5)? 0.5 : 1.0 # Lower hit rate to 50% if target has specific state.
end
end
"target" does not exist in this context, and assigning to "rate" is not even necessary either. It can be simplified as state?(5) ? 0.5 : 1.0
 

kyonides

Veteran
Veteran
Joined
Nov 17, 2019
Messages
155
Reaction score
32
First Language
English
Primarily Uses
RMXP
Let's say rate could have been left untouched just to let the user recall what it is all about. Concerning target... yeah, that would throw a NoMethodError message at once. Only if you need to make it afflict an enemy or any other actor, you would need to specify target, probably by adding a third argument to the method definition and making it option to keep it compatible with any previous definition, if there was any...
 

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

Latest Threads

Latest Posts

Latest Profile Posts

What is your favourite rpg maker plugin?
Hopefully this gets fixed before release..
Where's this sudden heatwave come from? It's not even sunny, it's just hot. Hate it!
I'm slightly disappointed that the MZ codebase still uses core.js, managers.js, objects.js, scenes.js, sprites.js and windows.js instead of core/Bitmap.js, core/Graphics.js, managers/AudioManager.js, managers/DataManager.js, etc...
just a few more hours until the toughest, most pressure 72 hours of my life, wish me luck guys

Forum statistics

Threads
100,565
Messages
977,269
Members
132,148
Latest member
jcamp10565
Top