Item calls common event & reduces TP/MP

What I want to happen:
  • An item calls a common event.
  • That item checks if the actor using the item has enough TP or MP to perform a certain action, in this case to inflict a state on the user
  • If yes, the action is performed and the actor's TP/MP is reduced by the appropriate amount.

To identify the actor and have the state inflicted on the user is quite straightforward. I simply put
Code:
$game_variables[id] = b.id; 0
in the damage formula, then in the CE have
1625836471561.png

However, the only way I can think of to determine if the actor has enough TP/MP to carry out the action is to have a conditional which asks if the actor identified by variable 172 has either 20 TP or 30 MP (no actor has both) and only if the answer is yes would the state be applied.

Could anyone tell me what the script call would be for that conditional? Or maybe there is a better way to do that which uses something other than a conditional?

The icing on the cake would be to be able to inflict the state on any member of the battle party, not just the user, but I suspect that's not possible.

Thank you.
Could you not add like a "$game_variables[id] = a.id" to your formula, then in the common event have your script conditional be $game_actors[$game_variables[id]].tp >= 20 || $game_actors[$game_variables[id]].mp >= 30
@Trihan That seems to be working, but I discovered that I've lost my script call to reduce TP (needs a script call as there's nothing in the Event Command Menu).

Would you be able to give me that as well? Without it I'm unable to check the item for a TP user.

Thanks

EDIT
Amend that. I was checking to make sure it wouldn't run if they didn't have enough. Now I've run it with an actor that does have enough and what happens is that a pop up appears saying 'Blocked' and the state icon does not appear on the actor's graphic. 'Blcoked' is what I have instead of it saying 'Null'.
I think it would be:
Ruby:
actor_to_check = $game_actors[$game_variables[172]]
if (actor_to_check.tp >= 20)
    actor_to_check.tp -= 20
    actor_to_check.add_state(19)
elsif (actor_to_check.mp >= 30)
    actor_to_check.mp -= 30
    actor_to_check.add_state(19)
end
My Ruby is kind of rusty these days so if it blows up entirely, let me know.
Be sure to replace both instances of 19 with the correct State ID of the state you are trying to apply.
This will be the only item necessary in the common event to execute the logic you described; you should be able to remove the Change State event command.
Edit: nvm, they have a tp= method so what Wavelength said should work.
@Wavelength It semi-worked a couple of times, and other times it didn't.
What happens is that no matter whether the state is applied or not, I get the 'blocked' pop up plus the pop up saying 'Warrior Boost' which would lead one to think that the state had been applied, but the state icon does not appear. Even when the state is applied, I am still getting 'Blocked' appearing.

Just to double check that I am doing this correctly:
My damage formula is
Code:
$game_variables[172] = a.id; $game_variables[id] = b.id;0

My common event only has your code as a script call, nothing else, because your code removes the need to use a Conditional branch event menu command.
If you've pasted your damage formula exactly as-is, that'll be why it doesn't work. You need to replace "id" in the second assignment with an actual number.
@Trihan Sorry, careless typing on my part, I should have done copy/paste. When I do, it is:
Code:
$game_variables[172] = a.id; $game_variables[172] = b.id;0

I have tried changing the final zero (i.e. no damage) to 1 in case it's just a question of the engine not liking a zero there, but that made no difference.
You need to use different variables, otherwise you're setting variable 172 to A's ID and then overwriting it with B's, and never actually retaining the ID of A.
@Trihan How should I, therefore, amend Wavelength's code, if at all?
Let's say a.id = switch 172 and b.id = switch 173
Take out the $game_variables[172] = b.id and I think it'll work. You don't need to store the ID of the target; you only want to check the user, right?

Though keep in mind that by default the "user" of an item isn't the actor who selected the item command, but the party member with the highest pharmacology.
@Trihan Partial progress. If I remove the b.id and restrict the scope to User only, then the state is applied, although there is still 'Blocked' coming up.
If that 'Blocked' bit could be prevented, then I'd have to just accept that this cannot be an item for one ally.

I've now got to stop for a couple of hours, so won't be able to do any more testing on this for a bit.

Thanks for your help so far.
I'll have a think about how you could make it work.
Getting the popup but then not having the state icon appear is very strange. I wonder if it is because you were looking in the wrong place, which would partially be my fault since I didn't notice the first time around that it is the user whose TP/MP you wanted to check and reduce; I thought it was the target's.

Try this code:
Ruby:
actor_to_check = $game_actors[$game_variables[172]]
actor_to_apply = $game_actors[$game_variables[173]]
if (actor_to_check.tp >= 20)
    actor_to_check.tp -= 20
    actor_to_apply.add_state(19)
elsif (actor_to_check.mp >= 30)
    actor_to_check.mp -= 30
    actor_to_apply.add_state(19)
end

Do the full version of the damage formula tracking both actors' IDs - that is:
$game_variables[172] = a.id; $game_variables[173] = b.id; 0

1 Ally scope should work.

If this doesn't work, my suspicion is that a plugin (perhaps once that deals with popups?) has overwritten the add_state method and could be responsible for the state not being added to the actor properly. Though it's also possible I made an error that has to do with the add state flow.
@Wavelength I've done a lot of experimenting.
The only script I have which affects pop ups is Yanfly's Ace Battle Engine. Sadly the Github link to it seems to have got messed up and that is probably the only battle script that I can't link you to.

What I did to test this was to have two items applying this state.
The first one was a straightforward item, set to one ally and all it did was apply the state using the Effects box.
That worked absolutely fine, the state was applied instantly (why that's important will be clear in a moment), the icon appeared and there was no 'Blocked' pop up, but the state pop up appeared correctly. To make sure that I could see that the state had been applied, I gave it my state animation for poison, so the whole time the state is in place, the actor's sprite is flashing a sickly green colour.

The second one had the damage formula, set to user and the common event with the revised code you gave. The 'Blocked' pop up appeared, but so did the state animation and the pop up with the state name. No icon and no TP/MP reduction until (as far as I could work out with the PCTB battle system) the end of the turn when the icon appeared, but the TP/MP still did not reduce.

So going by the state animation and state name pop up the state is inflicted immediately, but going by the icon it has to wait until the next turn, and going by the resource reduction and Blocked pop up it never happens.
How strange. I do wonder if the battle system screws something up with the State application when applied by Common Event or Scripting.

The only other thing I could suggest would be to convert my code into a long nested Conditional Branch inside the Common Event and hope that works better than a Script call.

So it would be something like:
Conditional Branch: If Variable 172 (User ID) = 1
..Conditional Branch: If $game_actors[1].mp >= 30 (or, if it's a TP actor, $game_actors[1].tp >= 20)
....Reduce Actor 1's MP by 30 (you may have to do this using the script line from above for TP)
....Conditional Branch: If Variable 173 (Target Ally ID) = 1
......Apply Warrior Boost state to Actor #1
....End
....Conditional Branch: If Variable 173 (Target Ally ID) = 2
......Apply Warrior Boost state to Actor #2
....End
....Conditional Branch: If Variable 173 (Target Ally ID) = 3
......Apply Warrior Boost state to Actor #3
....End
....etc.
..End
End
Conditional Branch: If Variable 172 (User ID) = 2
..Conditional Branch: If $game_actors[2].tp >= 20
....Reduce Actor 2's TP by 20 (you may have to do this using the script line from above)
....Conditional Branch: If Variable 173 (Target Ally ID) = 1
......Apply Warrior Boost state to Actor #1
....End
....Conditional Branch: If Variable 173 (Target Ally ID) = 2
......Apply Warrior Boost state to Actor #2
....End
....Conditional Branch: If Variable 173 (Target Ally ID) = 3
......Apply Warrior Boost state to Actor #3
....End
....etc.
..End
End
etc.

That's probably the best last resort shot I could offer. 🙁

Original thread: https://forums.rpgmakerweb.com/threads/item-calls-common-event-reduces-tp-mp.138310/