Jump to content
Leopard20

How to "call" script from Scheduled environment?

Recommended Posts

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
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
6 hours ago, 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
20 minutes ago, 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

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 ];
};

 

  • Like 1
  • Thanks 1

Share this post


Link to post
Share on other sites
22 minutes ago, 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
3 hours ago, 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
7 minutes ago, 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
Just now, 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
1 hour ago, 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.

  • Like 1

Share this post


Link to post
Share on other sites
isNil {
call MY_fnc_TEST
}

isNil executed unscheduled.

  • Thanks 1

Share this post


Link to post
Share on other sites

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
1 hour ago, 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.

  • Thanks 1

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×