Leopard20 813 Posted April 4, 2020 Hey guys. Does anyone know a way to "call" a function/script from the scheduled environment? In other words, let's say I've already spawned a script: 0 spawn { call MY_fnc_TEST } Any function called inside this script will obviously get executed in the scheduled environment (canSuspend). But I want it to get called in the unscheduled environment. One way I know is to do something like this: 0 spawn { OnEachFrame {call MY_fnc_TEST; onEachFrame ""}; } which is quite awkward. Plus the function will not get executed before the rest of the script (it gets called in the next frame). This can be verified using: AAA = 1; 0 spawn { onEachFrame {AAA = AAA + 1; onEachFrame ""}; AAA = AAA/2 }; This gives AAA = 1.5 instead of 1. Does anyone know a better way?! Share this post Link to post Share on other sites
M1ke_SK 230 Posted April 4, 2020 script_handler = [parameters] execVM "scriptname.sqf"; waitUntil { scriptDone script_handler }; https://community.bistudio.com/wiki/scriptDone Share this post Link to post Share on other sites
Leopard20 813 Posted April 4, 2020 On 4/4/2020 at 4:17 PM, M1ke_SK said: script_handler = [parameters] execVM "scriptname.sqf"; waitUntil { scriptDone script_handler }; https://community.bistudio.com/wiki/scriptDone This is scheduled dude. Share this post Link to post Share on other sites
POLPOX 779 Posted April 5, 2020 AAA = 1; 0 spawn { addMissionEventHandler ["EachFrame",{AAA = AAA + 1; removeMissionEventHandler ["EachFrame",_thisEventHandler]}]; _frameNow = diag_frameno; waitUntil {diag_frameno != _frameNow}; AAA = AAA/2; }; Something like this? Share this post Link to post Share on other sites
Leopard20 813 Posted April 5, 2020 On 4/5/2020 at 6:41 AM, POLPOX said: AAA = 1; 0 spawn { addMissionEventHandler ["EachFrame",{AAA = AAA + 1; removeMissionEventHandler ["EachFrame",_thisEventHandler]}]; _frameNow = diag_frameno; waitUntil {diag_frameno != _frameNow}; AAA = AAA/2; }; Something like this? Technically, yeah, it works. Thanks. I guess a simple sleep 0.001 does the same thing (it waits one frame). I was hoping for a more straightforward solution for calling (besides onEachFrame). Share this post Link to post Share on other sites
gc8 981 Posted April 5, 2020 maybe this will help you: https://community.bistudio.com/wiki/Initialization_Order it lists Unscheduled environments Share this post Link to post Share on other sites
Leopard20 813 Posted April 5, 2020 On 4/5/2020 at 12:51 PM, gc8 said: maybe this will help you: https://community.bistudio.com/wiki/Initialization_Order it lists Unscheduled environments I know but I want to switch from the unscheduled env directly to the scheduled environment without terminating the script. Share this post Link to post Share on other sites
gc8 981 Posted April 5, 2020 On 4/5/2020 at 12:53 PM, Leopard20 said: I know but I want to switch from the unscheduled env directly to the scheduled environment without terminating the script. so what are you trying to accomplish with that? maybe there's another way of doing what you want Share this post Link to post Share on other sites
Larrow 2827 Posted April 5, 2020 isNil will run code unscheduled. [] spawn { TAG_fnc_function = { hint format[ "Can Suspend: %1", canSuspend ]; AAA = AAA + 1; true }; AAA = 1; waitUntil{ !isNil{ [] call TAG_fnc_function } }; systemChat format[ "AAA: %1", AAA ]; }; 1 1 Share this post Link to post Share on other sites
Leopard20 813 Posted April 5, 2020 On 4/5/2020 at 3:43 PM, Larrow said: isNil will run code unscheduled. [] spawn { TAG_fnc_function = { hint format[ "Can Suspend: %1", canSuspend ]; AAA = AAA + 1; true }; AAA = 1; waitUntil{ !isNil{ [] call TAG_fnc_function } }; systemChat format[ "AAA: %1", AAA ]; }; Is it necessary to use waitUntil? Share this post Link to post Share on other sites
Leopard20 813 Posted April 5, 2020 On 4/5/2020 at 1:14 PM, gc8 said: so what are you trying to accomplish with that? maybe there's another way of doing what you want Unscheduled codes get executed exactly in one frame (even if it means halting the game). It is essential for certain time critical scripts to get called immediately without waiting for the scheduler to queue them. The scheduler delays scripts that take more than 3 ms to run, which means if many other scripts are still running your code will take forever (in relative terms) to get executed Share this post Link to post Share on other sites
gc8 981 Posted April 5, 2020 On 4/5/2020 at 4:10 PM, Leopard20 said: Unscheduled codes get executed exactly in one frame (even if it means halting the game). It is essential for certain time critical scripts to get called immediately without waiting for the scheduler to queue them (scheduler delays scripts that take more than 3 ms to run) Ah , for that I always simply use EachFrame EH. And I think the post init stuff also runs fast.. not sure how fast though Share this post Link to post Share on other sites
Leopard20 813 Posted April 5, 2020 On 4/5/2020 at 4:18 PM, gc8 said: Ah , for that I always simply use EachFrame EH. And I think the post init stuff also runs fast.. not sure how fast though I want to run a script during the mission. Post init and other stuff are irrelevant here. Share this post Link to post Share on other sites
Leopard20 813 Posted April 5, 2020 On 4/5/2020 at 3:43 PM, Larrow said: isNil will run code unscheduled. [] spawn { TAG_fnc_function = { hint format[ "Can Suspend: %1", canSuspend ]; AAA = AAA + 1; true }; AAA = 1; waitUntil{ !isNil{ [] call TAG_fnc_function } }; systemChat format[ "AAA: %1", AAA ]; }; Man! This is exactly what I was looking for! I just gave it a test and it works wonders. You see, I have a time critical script that has a for loop with another nested for loop: for "_i" from _a to _b do { for "_j" from _c to _d do { } } I have optimized it as far as I could but it's still slower than what I wanted. I was trying to somehow improve its execution time at the cost of some performance loss. So I did this: for "_i" from _a to _b do { isNil { for "_j" from _c to _d do { }; } } And execution time has been improved by ~%35 with minimal frame loss! BTW, the waitUntil part doesn't seem to be be needed according to my tests. 1 Share this post Link to post Share on other sites
Dedmen 2723 Posted April 6, 2020 isNil { call MY_fnc_TEST } isNil executed unscheduled. 1 Share this post Link to post Share on other sites
engima 328 Posted April 6, 2020 And out of curiosity, what exactly is so time critical? What is happening inside your loops? Share this post Link to post Share on other sites
Leopard20 813 Posted April 6, 2020 On 4/6/2020 at 6:36 PM, engima said: And out of curiosity, what exactly is so time critical? What is happening inside your loops? Dynamic mesh generation for buildings, which is used for path generation inside buildings for my AI. It has to be done ASAP otherwise the AI will just stay in place waiting for the mesh to complete. 1 Share this post Link to post Share on other sites