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,704
Reaction score
5,376
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: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Broke: Dont move. -boring. -passive. -uncreative. Woke: Bewegen Sie Nicht! +authoritative. +foreign. +reminds your son that he's a garden gnome now.
My girlfriend left a note on the fridge saying 'This isn't working, goodbye.' I opened the refrigerator and it works just fine. Weird.
BCj
Could someone translate something from Japanese to English for me?
You know, I sometimes ponder if working on the story and details is more necessary than the game itself. Its... technically work, just not direct.

Like for me specifically I never stop evolving the story based on what avenues I can approach in gameplay and then I get anxious thinking "hey good thing I thought about that now instead of later". Repeat ad nauseam.

Forum statistics

Threads
94,386
Messages
920,544
Members
124,164
Latest member
Leebob1997
Top