Jump to content
Midnighters

Question regarding while{true} dependability

Recommended Posts

Hello forum users.

 

I have written a script to animate the hummingbird based on crew members and everything works fine.

One thing I fret very badly about is performance. I'm just not sure if it would be best to use something like bis_fnc_loop instead.

 

{
if((typeOf _x) == "B_Heli_Light_01_F") then {
while{true} do {
if((count (crew _x)) >= 2) then {
if(isTouchingGround _x) then {
_x animateSource["BenchR_Up",0];
_x animateSource["BenchL_Up",0];

};
};
if((count (crew _x)) <= 2) then {
if(!(isTouchingGround _x)) then {
_x animateSource["BenchR_Up",1];
_x animateSource["BenchL_Up",1];
};
};
};
};
} forEach vehicles;

 

Share this post


Link to post
Share on other sites

Yeah, that probably doesn't perform well, because it runs multiple times per frame which is pointless when you're animating something. So just add some suspension like sleep in the loop.

 

By the way, that foreach loop gets stuck on the first matching heli it finds, so if you have another one, its code never runs.

Share this post


Link to post
Share on other sites
1 hour ago, Greenfist said:

Yeah, that probably doesn't perform well, because it runs multiple times per frame which is pointless when you're animating something. So just add some suspension like sleep in the loop.

 

By the way, that foreach loop gets stuck on the first matching heli it finds, so if you have another one, its code never runs.

 

Quite correct, and on a personal note, when you share code try to format it with tabs so it becomes a lot more readable.  Please tell me this isn't how you put it in your sqf file :)

Share this post


Link to post
Share on other sites

Could maybe use the Epe contact eventHandlers. Will need a small pause in the spawned code as Contact End fires before isTouchingGround is set.

 

Spoiler

TAG_fnc_doMHBenchSeats = {
	params[ "_vehicle" ];
	
	//Small delay as ContactEnd fires before isTouchingGround changes
	sleep 0.2;
	
	//Does the Heli currently have benches attached
	if ( _vehicle animationPhase "addBenches" isEqualTo 1 ) then {
		if ( isTouchingGround _vehicle ) then {
			//Deploy benchSeats
			_vehicle animateSource[ "BenchR_Up", 0 ];
			_vehicle animateSource[ "BenchL_Up", 0 ];
		}else{
			//Flying
			//Do we have units on the Right Bench
			if ( { !isNull( _vehicle turretUnit [ _x ] ) }count[ 1, 4 ] isEqualTo 0 ) then {
				//If not fold Right Bench
				_vehicle animateSource[ "BenchR_Up", 1 ];
			};
			//Do we have units on the Left Bench
			if ( { !isNull( _vehicle turretUnit [ _x ] ) }count[ 3, 2 ] isEqualTo 0 ) then {
				//If not fold Left Bench
				_vehicle animateSource[ "BenchL_Up", 1 ];
			};
		};
	};
};

{
	_x params[ "_vehicle" ];
	
	if ( _vehicle isKindOf "B_Heli_Light_01_F" ) then {
		_vehicle addEventHandler [ "EpeContactStart", {
			params[ "_vehicle" ];
			[ _vehicle ] spawn TAG_fnc_doMHBenchSeats;
		}];
		
		_vehicle addEventHandler [ "EpeContactEnd", {
			params[ "_vehicle" ];
			[ _vehicle ] spawn TAG_fnc_doMHBenchSeats;
		}];
	};
}forEach vehicles;

 

 

Share this post


Link to post
Share on other sites
9 hours ago, Greenfist said:

Yeah, that probably doesn't perform well, because it runs multiple times per frame which is pointless when you're animating something. So just add some suspension like sleep in the loop.

 

By the way, that foreach loop gets stuck on the first matching heli it finds, so if you have another one, its code never runs.

Gotcha. I figured as much, I just wasn't sure as to the reason why.

Thanks.

Share this post


Link to post
Share on other sites
8 hours ago, celludriel said:

 

Quite correct, and on a personal note, when you share code try to format it with tabs so it becomes a lot more readable.  Please tell me this isn't how you put it in your sqf file :)

Oh no. Definitely not how I put it into my .sqf file.

I just had to make it somewhat readable, found out github messed up formatting.

Share this post


Link to post
Share on other sites
4 hours ago, Larrow said:

Could maybe use the Epe contact eventHandlers. Will need a small pause in the spawned code as Contact End fires before isTouchingGround is set.

 

  Reveal hidden contents


TAG_fnc_doMHBenchSeats = {
	params[ "_vehicle" ];
	
	//Small delay as ContactEnd fires before isTouchingGround changes
	sleep 0.2;
	
	//Does the Heli currently have benches attached
	if ( _vehicle animationPhase "addBenches" isEqualTo 1 ) then {
		if ( isTouchingGround _vehicle ) then {
			//Deploy benchSeats
			_vehicle animateSource[ "BenchR_Up", 0 ];
			_vehicle animateSource[ "BenchL_Up", 0 ];
		}else{
			//Flying
			//Do we have units on the Right Bench
			if ( { !isNull( _vehicle turretUnit [ _x ] ) }count[ 1, 4 ] isEqualTo 0 ) then {
				//If not fold Right Bench
				_vehicle animateSource[ "BenchR_Up", 1 ];
			};
			//Do we have units on the Left Bench
			if ( { !isNull( _vehicle turretUnit [ _x ] ) }count[ 3, 2 ] isEqualTo 0 ) then {
				//If not fold Left Bench
				_vehicle animateSource[ "BenchL_Up", 1 ];
			};
		};
	};
};

{
	_x params[ "_vehicle" ];
	
	if ( _vehicle isKindOf "B_Heli_Light_01_F" ) then {
		_vehicle addEventHandler [ "EpeContactStart", {
			params[ "_vehicle" ];
			[ _vehicle ] spawn TAG_fnc_doMHBenchSeats;
		}];
		
		_vehicle addEventHandler [ "EpeContactEnd", {
			params[ "_vehicle" ];
			[ _vehicle ] spawn TAG_fnc_doMHBenchSeats;
		}];
	};
}forEach vehicles;

 

 

Ooohh..! this is quite interesting, I'll take a look at this.

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

×