Jump to content
Sign in to follow this  
Luft08

Can you pass a local variable to BIS_fnc_loop ?

Recommended Posts

I have the following method: 

LFT_startCarLoops = {
    params ["_cityAreaArray", "_percent", "_carTypeArray", "_deletionRange", "_spawnRange"];
    
    // Initialize global array to keep track of spawned cars
    if (isNil "ambientParkedCars") then {
        ambientParkedCars = [];
    };

    ["itemAdd", ["checkPlayerProximityForCarsLoop", {
        [_cityAreaArray, _percent, _carTypeArray, _spawnRange] call LFT_CheckPlayerProximityAndSpawnCars;
    }, 1]] call BIS_fnc_loop;

    ["itemAdd", ["checkCarsProximityLoop", {
        [_deletionRange] call LFT_CheckCarsProximity;
    }, 1]] call BIS_fnc_loop;
};

 

I need it to run some other code every few seconds. However, although this method gets data through the parameters (I have checked), when it gets to the BIS_fnc_loop the value of _cityAreaArray doesn't get passed and becomes "any". I need it to LFT_CheckPlayerProximityAndSpawnCars.

 

LFT_CheckPlayerProximityAndSpawnCars = {
    params ["_cityAreaArray", "_percent", "_carTypeArray", "_spawnRange"];

   <Some code>

};

Share this post


Link to post
Share on other sites
On 8/12/2024 at 3:39 AM, Luft08 said:

However, although this method gets data through the parameters (I have checked), when it gets to the BIS_fnc_loop the value of _cityAreaArray doesn't get passed and becomes "any"

 

This is because the code you pass to the BIS_fnc_loop doesn't run inside LFT_startCarLoops's scope and therefore knows nothing of the variables declared there.

 

Reading the BIKI for BIS_fnc_loop we can see that it doesn't allow passing of arguments to the code.

 

A quick and dirty fix is to rely on global vars instead, which should be ok as long as it's only used once...

 

For a more reusable approach I'd recommend scrapping the use of BIS_fnc_loop and switch to a regular while loop instead. Combine with spawn and sleep you can achieve a similar effect with the added bonus of a lot more flexibility.

 

If you need code examples let us know.

  • Like 2

Share this post


Link to post
Share on other sites

Hi, unfortunately I can't test the code at the moment, but the following should give a rough idea of how the whole thing could be rewritten with "spawn" and loops:
 

LFT_startCarLoops = {
	params ["_cityAreaArray", "_percent", "_carTypeArray", "_deletionRange", "_spawnRange"];

	//checkPlayerProximityForCarsLoop
	[_cityAreaArray, _percent, _carTypeArray, _spawnRange] spawn { //The code in Spawn runs separately from the rest of the code. This means that there is no waiting for the end of the while loop.
								//Since the private variables within the spawn are not known, we provide them as parameters.
		params ["_cityAreaArray", "_percent", "_carTypeArray", "_spawnRange"];
		while {true} do { //endless loop (It would be better if there was a real ending condition).
			[_cityAreaArray, _percent, _carTypeArray, _spawnRange] call LFT_CheckPlayerProximityAndSpawnCars;
			sleep 1; //Without a short break between loops, the game's performance would suffer greatly.
		};
	};
	
	//checkCarsProximityLoop
	[_deletionRange] spawn {
		params ["_deletionRange"];
		while {true} do {
			[_deletionRange] call LFT_CheckCarsProximity;
			sleep 1;
		};
	};
};

LFT_CheckPlayerProximityAndSpawnCars = {
    params ["_cityAreaArray", "_percent", "_carTypeArray", "_spawnRange"];

   <Some code>

};

 

  • Like 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
Sign in to follow this  

×