MV Using "Promise", to wait for the result of another function

I want to add an action to another.. i am applying this on "GameAction_apply"..
this is done before the dmg is dealt, because i want that action to be added even if the first action misses,,
(i know about "force action" ect, but my problem is about the timing, .. not about how to add the action)


So lets say we have these 2 actions, first is default,second is the added action..
now if the enemy dies from the first action.. the SV char trys to use the second action aswell and "jumps" when the death of the enemy is recognized..

So the thing is, i used an "if condition" that should make sure to only add the second action if the unit is "not dead"..
But this cant work, because when this code runs, the enemy is still alive,because the dmg wasnt dealt yet..

caethyril wrote here#10 ,..​

that "Promise" can be used to wait for another function,,, but its a bit hard to understand for me..

so here is my "if condition" that checks if the enemy is dead:

JavaScript:
if (!target[1].isDead()) // use my code for extra action ect;

but i am not sure for which "Game_Action"- function i need to wait till the dmg is applied, and more important, how to use "Promise" to ask if the "Unit is not Dead".. after the dmg applied.
-> in order to only run my "add action" code if the unit is "not dead"
// i guess the timming issue is only about a few frames..

any help on how to make this work the simplest way.. with "promise" or any other solution would be really welcome!

and hopefully you all had a nice Christmas
Except for something that requires an actual wait and the wait time varies (such as when you're loading a file into the memory or requesting a web page), I don't think JS promises would be useful here. Everything in the code is working in exact order, so you don't need it. You just need to make sure you're inserting it in the correct order.

However, you can save a function as a variable to be called later. Sort of like
JavaScript:
var addSecondAction = function() { .... }
FirstAction()
addSecondAction()

Since you did not share what is your attempt so far and your exact code on how you added the second action, we don't know
i am using force action to add the second action..

i did not know that var can store functions this way.. but however that wont solve the issue, that my code is used at "game action apply" in order to apply even if the hit misses.. but i need to know if the hit killed the enemy , in order to decide if the second action(force action) gets executed..

I my solution this all happens while the first hit runs
(at gameAction apply of the first hit)
-> i know this can trigger a never_ending chain, but i allready solved that

i think i could add global switches, and execute the action in other functions..
(if&after the dmg is executed, and in another function if the hit has missed)
.. if the switch is true..and reset the switch.

but i wanted to avoid editing other default functions, and hoped there is some kind of wait-script that lets me wait for the other function to happen before my if condition decides to execute the forceAction or not

or i could use scene battle update with a switch and "while()"..
i just thought these solutions are a bit clumsy
😴 Yea, I'm not sure I fully understood Promises back when I wrote that post. A Promise is basically just a way to program asynchronously, e.g. load a file in the background then do something once it's loaded (or when an error occurs). Like Theo says, it's designed for performing long tasks, like loading files, in the background.

It's usually a good idea to hook into the existing engine structure/methods where possible. Using a separate timer could potentially cause incompatibilities with the core scripts and/or plugins you're using. Remember that plugins can change any part of the core scripts.

If you put the force action in a Common Event effect attached to the skill/item, that should run after the action is complete. This is because the global effect (common event effect) is applied when the action starts, but events cannot start during the action phase. Otherwise...

When an action is used, the BattleManager changes to the action phase and invokes the action once per target. When an action is invoked: the target is removed from the list, counter/reflect is checked, then the action is applied to that subject/target.

Damage is applied via Game_Action#executeDamage (rpg_objects.js). This accepts two arguments, target and value, and is called from Game_Action#apply if the action hits.

If you want to wait until all aspects of the current action invocation are complete, perhaps try hooking into BattleManager.endAction (rpg_managers.js): it should run after an action has been invoked on all its targets. I think you would also need to clone the result from Game_Action#makeTargets (rpg_objects.js) into some variable for later reference, since the target array will have been erased after the action has been invoked on all targets.
thanks for the details..caethyril
i knew the common event thing, but since i wanted to make the plugin more user friendly(skillnotes) for others, thats no final solution, only an alternative..

well i allready solved that issue, by using "game_battler.refresch"
( thats where battlers can get the deathstate if hp is 0)

.. i am not sure about normal rpg projects, but in srpg battlers are conneted to event ID.
The battler refresh happens after the battler got hit , that was the timming i needed, a few frames ealier doesnt help.

So i asked "if battlerEventID is === targetEventID".. and a few other if conditions about global plugin switches, which get reseted right after this scriptpart.
(the plugin switches made sure that all this only happens when its needed)

-> and i store every needed data like skill id or event id (for user and target data), in plugin variables, so thats no issue for me^^

The thing in srpg is, the sv battle parts only happen for 1 singel action similar to "fire emblem", and my script had to run before a counter can happen but after i can ask if battler is still "alive"(so after hp loss)..
-> so most solutions you mentioned didnt work for me
(i tested them allready)

However atleast i know now that it wasnt my fault that promise doesnt help^^

I got still a few other issues to solve, but its hard to explain to other people that dont use srpg, so i guess this can be closed, thank you very much!
(also the issues left are not related to this Thread-question here)
OK, happy RPG Making! 👋

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/using-promise-to-wait-for-the-result-of-another-function.143361/