Calling a common event in damage formula

   I need to be able to check the target before running the event, so doing it in the formula box would be

the only way I can think to do it. Any help is greatly appreciated.
I'm very unsure about the possibility of calling a common event from the damage formula.  But it may be that there is another way round this.  What exactly is it that you need to check about the target?  The target's ID? Does the target have a state?  Something else?  Many of those sorts of things can be done from the formula itself.
I need to run a common event with a conditional branch based on a random variable to remove a state if b.id = x. If b.id ≠ x then I would want

the event to simply not run. 

This is a way around the state removal from the other thread that never got resolved, I am attempting to put a % chance to remove 

state on enemy normal attacks. By running it in a common event I am able to apply the % chance regardless of whether the attack 

connects or not.
This can be done, I have seen something similar, but my Ruby syntax is not good enough to suggest a possibility to you with confidence. It will be something vaguely along the lines of this, with variable #14 being used in the example, and just using adding a state depending on what the random value is to show how it might work.  Obviously you would have to change this to suit your particular needs.

[SIZE=10.5pt]v[14] = math.rand(0,3); v[n]==0 ? b.add_state(32) : v[n]==1 ? b.add_state(64):v[n]>1;[/SIZE] [SIZE=10.5pt]250 + a.mat * 2 - b.mdf * 2[/SIZE]

 Depending on how complex it is, your formula may well be too long for the damage formula box, so you would need to get round that by using a scriptlet.

[SIZE=10pt]class[/SIZE] [SIZE=10pt]Game_Battler[/SIZE] [SIZE=10pt]<[/SIZE] [SIZE=10pt]Game_BattlerBase[/SIZE]

  [SIZE=10pt]def[/SIZE][SIZE=10pt] custom_formula_name[/SIZE][SIZE=10pt]([/SIZE][SIZE=10pt]a[/SIZE][SIZE=10pt],[/SIZE][SIZE=10pt] b[/SIZE][SIZE=10pt],[/SIZE][SIZE=10pt] v[/SIZE][SIZE=10pt])[/SIZE]

    [SIZE=10pt]#here you can add as much as you want[/SIZE]

  [SIZE=10pt]end[/SIZE]

[SIZE=10pt]end[/SIZE]

[SIZE=10.5pt]then you only need to write this in your skill formula box:[/SIZE]

[SIZE=10.5pt]custom_formula_name(a, b, v)[/SIZE]

Is this the sort of thing you are looking for?
To be honest, my ruby syntax is not at all good. But looking at the formula it seems to be along the right lines.

I believe it may be that I need to define a custom formula. Could you get as close to this as you can:

1.) set a variable to 0-3

2.) if variable is > 2 remove state #x

3.) if variable is < 1 remove state #y

4.) damage is dealt a.atk * 4 - b.def *2 (regardless of variable)

this way I could give a 50% chance to remove either of the states with every enemy physical attack, and could just 

make another custom formula whenever I needed a new damage formula for a particular enemy.

thank you again for the help.
This is very tentative, and if some passerby knows better syntax than me, perhaps they can correct it if it's wrong.  I think if you want this to be a 50% chance then it cannot be variable >2 (i.e. variable greater than 2, so that must equal 3) and <1 (i.e. less than 1, so must equal 0).  Doing it that way leaves the random numbers of 1 and 2 unaccounted for.  I think it has to be 

variable is equal to or greater than 2

variable is equal to or less than 1

Again using variable #14 as the example

v[14] = math.rand(0,3); v[n]>=2 ? b.remove_state(32); v[n]<=1? b.remove_state(64); a.atk * 4 - b.def * 2

I'm not sure if you need to put end.  If you do, it would likely be

v[14] = math.rand(0,3); v[n]>=2 ? b.remove_state(32); v[n]<=1? b.remove_state(64); end; a.atk * 4 - b.def * 2
Yeah, it won't quite fit into the formula box, and when I try to use the formula provided as a custom formula I am getting syntax errors on startup. I still really appreciate the help, you have gotten me much closer and maybe

someone will see this and help clean up the formula some.

Right now what I am seeing is (unexpected ';' expecting ':') just for reference.
I think I see the error.  Try

v[14] = math.rand(0,3); v[n]>=2 ? b.remove_state(32) : v[n]<=1? b.remove_state(64) : a.atk * 4 - b.def * 2

Still don't know about the end, though.
Well that fixed the syntax error, but for some reason the attack doesnt remove either state. I double checked to make sure the state i.d.'s were matching in the

formula and the database, but to no avail.

Also, the formula does no damage regardless the enemys atk value :3

just to clarify, im using "custom_formula_name(a)"

in both the formula box and the script.
I don't trust that (a) as part of your name.  Let's call this formula kakaw_bird.  Your snippet would look like this:

class Game_Battler < Game_BattlerBase def kakaw_bird(a, b, v) v[14] = math.rand(0,3); v[n]>=2 ? b.remove_state(32) : v[n]<=1? b.remove_state(64) : a.atk * 4 - b.def * 2 endendIn the damage formula box you simply type 

[SIZE=10.5pt]kakaw_bird(a, b, v)[/SIZE]

[SIZE=10.5pt][/SIZE]

[SIZE=10.5pt]If that doesn't work, it may be that it should be a semi-colon after remove_state(64);[/SIZE]

I'm not sure enough of my syntax to be able to say.
I didn't test any of the following yet, Sorry.

About the snippet:

Spoiler
I don't think  math.rand(0,3) will work unless you define  math  first.

You can use  rand(4)  instead which will calculate a random integer between 0 (inclusive) and 4 (exclusive).

The snipped uses  n  twice which doen't seem to be defined and was probably considered a placeholder for the actual variable ID.

At the moment, the actual damage formula  a.atk * 4 - b.def * 2  is never executed (there also is no case where variable 14 is neither greater/equal 2 nor less/equal 1). If you want to make the damage formula part of your method, you should probably exclude it from the fork condition.

The formula is usually evaluated in the context of Kernel. If you define your method for the Game_Battler class, you have to call it for a battler or the method won't be found.

After modifying ksjp17's snippet it could look like this:

module Kernel   def self.kakaw_bird(a, b, v)    v[14] = rand(4)    if v[14] >= 2      b.remove_state(32)    else      b.remove_state(64)    end    a.atk * 4 - b.def * 2  endend
About the common event alternative:

Spoiler
If I got you correctly you want to call a common event and instantly execute it? If so you can try this code:
 

interpreter = Game_Interpreter.new

interpreter.setup($data_common_events[10].list)

interpreter.update

The first line creates an interpreter object that can be used to execute events. The second line loads the command list of common event 10 (change the ID at wish).

The third line processes one frame of the event execution. If you don't use commands that cause the interpreter to pause this should execute the entire event. If you want to make sure the event is executed in its entirety you can replace that third line by

interpreter.update  while interpreter.running?

to skip any "wait" in the event. Don't use messages or other commands that can't be skipped immediately or the game will freeze.

To squeeze it into the damage formula box, replace the line breaks by semicolons. If you don't have enough space, you can move the event execution to a custom method instead:

module Kernel   def self.run_event(id)    interpreter = Game_Interpreter.new    interpreter.setup($data_common_events[id].list)    interpreter.update  while interpreter.running?  endendTo use it in your formula, just type

run_event(10); Your actual Damage Formula
Thank you so much for the help guys, I think I am really close to getting it.

As of right now, with the variable method, it is calling the formula now, but the state wont be removed if the attack is evaded. I need for the state to be removed even if the

target evades or counters, which is why I was going the common event route.

First of all, i need to check b.id before running the event, so hopefully I could just just

damage formula; if b.id = 3,4,5 then run_event(10)

Not sure if that would work, but on top of that:

Using the kernel to call the common event in the formula, is calling the event for sure, but using

run_event(10); a.atk * 4 - b.def * 2

is not running the damage formula, only the common event. Also it is only running the event on a successful hit, I

was hoping to: run damage formula, check id, run event.

I tried putting the damage formula first:

a.atk * 4 - b.def * 2; run_event(10)

and the attack is happening, triggering evade/miss etc., but for some reason when the attack connects,

there is no formula for the damage, it is not triggering a null or anything.
Evasion, miss and counter chances are evaluated before the damage formula is executed. Unless the action is a success the damage process is skipped, regardless how the damage formula looks like. 

The result of the damage formula is usually the result of the last command executed, so the order is still important.

[...] is not running the damage formula, only the common event.
What do you mean by that? Does the skill deal not the damage you'd expect it to do? Or something completely different? The damage formula is only used to calculate the skills base damage, which is the first step of the damage calculation. The target is not supposed to take any damage and modifiers like element affinity are not considered yet.

If you want to do something even when the attack misses or even is completely prevented (counter), using the damage formula might not help you that much.

What do you want to achieve exactly? Especially, what should happen when...

- ...the attack is reflected

- ...the original target is substituted by another one

- ...the action fails because the attacker can't pay its cost / can't move / is silenced

- ...the action gets replaced because the attacker is confused

...if those events can occur in your project in the first place?

For example, you can access which target was selected for each action before the action is executed unless it is determined randomly. However, it's not guaranteed the action will actually hit that target in all cases of course.

Edit: If I understand your posts correctly you want to call a common event when the skill is used on a particular target? And even call it when the actual attack fails for any reason, including being unable to act, miss or get countered?
Sorry, I can see how this has become confusing. 

It totally makes sense to me after thinking about it, that nothing in the damage formula box would be run unless the attack connects.

My original goal was to have 2 states:

1.) One that raises evasion +100%, and removes itself after you evade two physical attacks.

2.) One that adds counter +100%, and removes itself after you counter two physical attacks.

I also wanted to alter the counterattack to where the automatic evasion would not take place when countering. I posted this in script requests a week or so ago.

After that not being resolved, I started trying to think around it, and figured I could run a common event with all enemy attacks that would remove the states on

a % chance. Not exactly ideal, but would still keep the basic idea behind what I was working at. The only problem with running a common event in the effects

box was that I could not check the target of the enemy attack first. I didn't want the state to be erased on a % chance when the person with the state active

wasn't even being targeted. So I figured if I ran a target check and the common event in the damage formula then maybe that could work. But it makes sense

to me now that it won't, because in that case the event won't be run unless the attack actually connects. 

I really appreciate all the help guys. If you have any suggestions for me i'll be checking back after work.

Thanks so much.

Original thread: https://forums.rpgmakerweb.com/threads/calling-a-common-event-in-damage-formula.41497/