MV Trying to understand how .forceAction() works

I made a succubus skill using events, this skill makes all the actors having a charm state to give the succubus a potion, to make this I made a skill called "Give potion" and I am forcing the actors to give her the potion, but... the results are unexpected...

This is the script in the troop event:

JavaScript:
for(f=0;f<4;f++){ // for the 4 actors
  if($gameParty.members()[f].isStateAffected(38)){ // if they are under charm state
    $gameParty.members()[f].startAnimation(231); // it shows a heart animation
    setTimeout(function(f){ // 1 second time out to show the heart animation
      $gameParty.members()[f].removeState(38); // Remove the charm state in order to let the actor to act
      $gameParty.members()[f].forceAction(161,0); // Then I force the give potion skill
      BattleManager.forceAction($gameParty.members()[f]); // Perform force action
      $gameParty.members()[f].addState(38); // Add charm state again
    }, 1000, f);
  }
}

These are my unexpected results:

Having 2 actors having charm and they didn't act on the current turn yet:
  1. the first actor walk ahead,
  2. then the second actor remove the charm state and gives the potion to the succubus as expected.
  3. then the first actor attack the first party member (index 0) twice (WHY?).
Having 2 actors having charm and they already act on the current turn:
  1. the first and second actors remove the charm state.
  2. then they do nothing, no potion, no attack, no anything, it looks like the enemy had mercy and removed the charm for both 😂
I can't understand how it works, both results don't make any sense for me.
I think that force action does not add action, it only forces existing actions to be done..

there are 2 problems here:

- if another force action is triggered it will be used before the first one, that somehow also bugs the first force action

- if actors allready act, they cant use force action
( thats probably related to not having a gameAction, not sure because i dont use normal sv battle in my projects )
-----
=> i think the whole problem is related to using for(f=0;f<4;f++){ ect..
that seems to build a longer code action chain before everything gets executed, so that every new force action will push forward in line to be the first one.

==> try to avoid that by building separated action chains that finish ,before the next one gets triggerd ..
(not sure if what i am saying makes sence, when i manipulated and used force action it was for srpg-projects so i cant give proper coding advice on normal sv battles)

.. also try to figure out if every party member gets an gameAction(if they dont have one left), before force action is used. i think you might need some code like "add.action", so that there is an gameAction that can be forced to trigger next.

edit:
i try to explain it better,
every battler has a gameAction (or several or none left), those are on action state "waiting" till its their "turn", if using "force action" that pushes the gameAction to be executed asap and it gives the required data to the gameAction.
If there is no gameAction left, no action can be forced.
If somehow you added to much gameActions to a battler, than several actions will be executed till there is none left.

edit 2
(pls forget the srpg code part on the img below, its because i only could use one of my srpg projects to get the img)
Screenshot_1.png


you probably need to add stuff like:
- use the "actionInputIndex" to make sure only actors who are on their turn get triggered
(perhaps you could use "f" on "actionInputIndex" aswell idk.. i am not sure if thats how it works perhaps i missunderstood "actionInputIndex" myself)
- check "actions.lenght" of " $gameParty.members()[f]." and if 0 add.action .. i hope that doesnt trigger an endless chain..
(also remember that if there is no action, the action state is "" ,else with an action but if its not their turn its "waiting" )

something like this in console f8 while in battle ,could give some help for better understanding:

$gameParty.members()[0]; //opens data of battler like img above
$gameParty.members()[0].actionInputIndex; // returns the actionInputIndex number
$gameParty.members()[0].actionState; // returns the actionState

$gameParty.members()[0].actions; // opens the data of action like img above
$gameParty.members()[0].actions[0]; // adress existing action with its number,starts with 0 i guess

$gameParty.members()[0].actions.lenght; // returns the number of "how much gameAction the battler has left"

to find out how the code stuff looks like ,you can also look into :
GameActor & GameParty & GameBattler & GameAction
they are all in the rpg objects plugin
Thank you,

This is what I get after forcing the action for $gameParty.members()[f]._actions and $gameParty.members()[f]._actionInputIndex:

1664734537865.png


It seems like the objects are ok, it should use the skill 161, but an attack (skill 1) is done.

I tested checking for the first actor only, without the for(), I got the same result, he still attacking the party like he still under the charm state.

JavaScript:
f=0;
  if($gameParty.members()[f].isStateAffected(38)){
    $gameParty.members()[f].startAnimation(231);
    setTimeout(function(f){
      $gameParty.members()[f].removeState(38);
      $gameParty.members()[f].forceAction(161,0);console.log($gameParty.members()[f]._actions);
      console.log($gameParty.members()[f]._actionInputIndex);
      BattleManager.forceAction($gameParty.members()[f]);
      $gameParty.members()[f].addState(38);
    }, 1000, f);
  }

I will try not adding the state at the end... (commenting this line: $gameParty.members()[f].addState(38))

Ok, that works, but now the actor is not under charming, I need to find a way to reapply it.

--- edit ---

Well... this works but I don't like it:

1664736643999.png


I made 4 if(), for each actor, and added 2 "waits", that way the engine has enough time to make any action.

I wonder if there is any way to make it more efficient.
if you only try it out on 1 actor dont use f .. use 0

if you can make it work for a singel actor you might find a way to make it work for all party members later.

about the charm state.. it could be possible that this state is a problem and manipulates the action to be a normal attack..( i think the default charm state, always triggers the normal attack usage on owm members )

try to use a "fake charm state"
(which looks the same but does nothing except beeing the trigger state for your experiment)
- if that works, you can try out how much the state can be changed from its setup , in order to see if any setup bugs your experiment.

edit
also one question what is the " 1000, f)" ??
(i got no idea why its added)


edit 2

about your edit info..

Congratz that it works so far, if you cant use the default charm state , try an empty state first (as triggerState) and if that works try step by step how much you can change this state setup before it bugs..

for example you could try "no movement possible" and make it look like a charm state by using the charm hearts overlay for the state
Screenshot_1.png

---------

also you can still try to make things work more efficient with different coding, but the most important thing is having a basic code that works, so that you can go on from there and notice every bug that any changed coding might make...


edit 3
perhaps you could use the "for each" method instead of the "for" which u used at first, to make it more efficient
i used that for game map event and triggering each event,.. it looks like this :

$gameMap.events().forEach(function(event) {code stuff doing here})
(this uses code for each event on map)

I am just not sure how to use that for your purposes with $gameParty.members() or $gameActors ect, instead of $gameMap.events() & event, i dont have so much exp with this method
Just to put forth the option...you might find it easier to program this into an action sequence.

Instead of actually making every afflicted party member execute a separate skill that gives a potion, you can use action sequence commands to check each party member, have them do whatever that other skill normally shows, deduct the item, etc., but avoid calling any external actions

I would think that's cleaner than trying to do it in JavaScript and with all those skills being called.

You could even combine the effects and make the however many actors do their motions at the same time, and say "Your party gave the succubus 3 potions!" or whatever.
yay i figured out how you can use "forEach" with "$gameParty.members()" & "Game_Actor"


here is an example how i removed 100 hp from all party-members

JavaScript:
$gameParty.members().forEach(function(Game_Actor) {Game_Actor.gainHp(-100)});

so you should be able to put your whole code inthere and look if that works better than unsing "for"


something like this for example:

JavaScript:
$gameParty.members().forEach(function(Game_Actor) {
          if (Game_Actor.isStateAffected(38)) {
              Game_Actor.startAnimation(231);
              Game_Actor.removeState(38);
              Game_Actor.forceAction(161,0);
              BattleManager.forceAction(Game_Actor);
          }
});
Thats just an example which you can expand to your needs but i think it should work.. atleast my first code above with "Game_Actor.gainHp(-100)" worked fine..

Also i am not sure it that kind of code action chain works better for your purposes here, but its worth a try..
also one question what is the " 1000, f)" ??
The second argument for setTimeout (1000) is how many milliseconds I want to wait to run the first argument (function(f)).
The third, fourth, fifth and so on arguments for setTimeout are variables sent for the function as arguments.
In my calling I am triggering an anonymous function after 1 second and sending the f variable as argument for such function 😂

Using this works just fine except for the state reapply:

JavaScript:
wait=0; // a counter for how many frames to wait before giving the control to the player
for(f=0;f<4 && f<$gameParty.members().length;f++){
  if($gameParty.members()[f].isStateAffected(38)){
    wait+=120; // for each affected add 1 second
    $gameParty.members()[f].startAnimation(231);
    setTimeout(function(f){
      $gameParty.members()[f].removeState(38);
      $gameParty.members()[f].forceAction(161,0);
      BattleManager.forceAction($gameParty.members()[f]);
      // a new setTimeout for reapply (not working)
      setTimeout(function(f){ $gameParty.members()[f].addState(38); }, 1000, f);
    }, 1000+2000*(f), f); // actor 0 waits for 1 sec, actor 1 waits for 3 secs,
      // actor 2 waits for 5 secs and actor 3 waits for 7 seconds
} }
this.wait(wait); // for each actor affected will wait 2 seconds before giving the control to the player

you might find it easier to program this into an action sequence.

You are right, but I can't figure out how to do it, I think I should remove the troop event page and give the succubus a skill to control the affected enemies... ok, let me try it 😂
i dont understand why you remove the state if you wanna add it back right after..
However congratz i think you are pretty close to your needed solution.
yay i figured out how you can use "forEach" with "$gameParty.members()" & "Game_Actor"

Oh, this looks better, less code, it works just like before

JavaScript:
wait=0;
$gameParty.members().forEach(function(Game_Actor) {
  if(Game_Actor.isStateAffected(38)){ wait++; // now I count how many actors are affected
    Game_Actor.startAnimation(231);
    setTimeout(function(Game_Actor){
      Game_Actor.removeState(38);
      Game_Actor.forceAction(161,0);
      BattleManager.forceAction(Game_Actor);
      setTimeout(function(Game_Actor){ Game_Actor.addState(38); }, 1000, Game_Actor);
    }, 1000+2000*(wait), Game_Actor); // I wait 1 sec for actor 0, 3 secs for actor 1, 5 for actor 2 and 7 for actor 3
} }
this.wait(wait*120); // for each actor affected will wait 2 seconds before giving the control to the player

i dont understand why you remove the state if you wanna add it back right after.
It seems like I have to remove any movement restriction in order to make .forceAction() to work, so I remove it temporary to give the potion and reapply it later to continue the restriction behavior.

you can use action sequence commands to check each party member
I think I should make the skill to effect the whole party and check every actor having the charm state in a <Whole Action> tag, how to check for states using action sequences?
action seq => sry no idea.. i dont use that much
does :
JavaScript:
if $gameParty.members()[0].isStateAffected(38)
not work?

(if that works you could probably also use the "forEach" method here to check all $gameParty.members())

thats the only hint i found about that:
----

about your code:
so the only problem left is that re-applying the state doesnt work=?
could that be related to the second "timeout"-function?? and if thats the case can it be avoided?
( i also have no exp with that timeout function,but i see no reason why adding a state should be a problem, thats why i ask )
you could probably also use the "forEach" method here to check all $gameParty.members()
That sounds great it could, but I don't know how to do it... something like this?

JavaScript:
$gameParty.members().isStateAffected(38).forEach(function(Game_Actor) {

I finally found why the state wasn't being reapplied: it seems like a restriction state can not be applied when the actor is acting, so, increassing the second setTimeout to 3 seconds (longer than the give potion duration) made it work!

JavaScript:
wait=0;
$gameParty.members().forEach(function(Game_Actor) {
  if(Game_Actor.isStateAffected(38)){
    Game_Actor.startAnimation(231);
    setTimeout(function(Game_Actor){
      Game_Actor.removeState(38);
      Game_Actor.forceAction(161,0);
      BattleManager.forceAction(Game_Actor);
      setTimeout(function(Game_Actor){ Game_Actor.addState(38); }, 3500, Game_Actor); // now it waits for 3.5 secs
    }, 1000*4*wait+1000, Game_Actor); // Also I had to increase the time between acts to 4 seconds.
wait++; } });
this.wait(wait*60*4); // Also I had to increase the total time to 4 seconds for affected actor

It works just fine, THANK YOU ALL, I can't believe how short the code is 😂
1.That sounds great it could, but I don't know how to do it... something like this?

JavaScript:
$gameParty.members().isStateAffected(38).forEach(function(Game_Actor) {


2.It works just fine, THANK YOU ALL, I can't believe how short the code is 😂
1. no.. its more like this: (example for an action sequences edit)
JavaScript:
$gameParty.members().forEach(function(Game_Actor) {
          if (Game_Actor.isStateAffected(38)) {
              "do other action sequense stuff here" // add stuff here
          }
});
.. but only if it works in act seq.. i got no idea..
------
2. however the act seq stuff doesnt matter if your solution works fine now^^
(funny that last problem was just a timing issue^^)
Congratz

Original thread: https://forums.rpgmakerweb.com/threads/trying-to-understand-how-forceaction-works.151707/