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

qwertyu63

Villager
Member
Joined
Nov 30, 2013
Messages
6
Reaction score
1
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
54
Reaction score
12
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
4,712
Reaction score
5,394
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
54
Reaction score
12
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: 2)

Latest Threads

Latest Posts

Latest Profile Posts

What can I sing for my sisters birthday...?

>.<'
Well, I'll have you know that I'm still around, looking for opportunity to resurface with some stuffs.
I like to think of Profile Posts as tiny homes, full of all kinds of unsuspecting suckers "future" friends.... Many of which are of course only going to be excited when a strange man in black suddenly shows up to say hi. :LZSexcite:
Stream will be live shortly with some more Haunting Ground! Today, we encounter a new stalker! Feel free to drop by!
Tried the new Samsung Galaxy S20 Plus today. Very nice device overall, but flagship smartphone prices are getting completely out of control.

Forum statistics

Threads
94,502
Messages
921,475
Members
124,361
Latest member
Kealoa
Top