Jump to content
beno_83au

BIS_fnc_compatibleMagazines and _muzzle

Recommended Posts

Hello.

 

Little bit of a time sink this one was, but when using _muzzle with BIS_fnc_compatibleMagazines, _muzzle keeps being re-defined from a string into a config entry:

systemChat str ["CHECK 1",_muzzle];		//["CHECK 1","LMG_coax"] 
_compatMags = [_muzzle] call BIS_fnc_compatibleMagazines;
systemChat str ["CHECK 2",_muzzle];		//["CHECK 2",bin\config.bin/CfgWeapons/LMG_coax]

Also, trying to save _muzzle into a temporary variable results in _muzzle still being changed:

_muzzleTemp = _muzzle;
systemChat str ["CHECK 1",_muzzle,_muzzleTemp];		//["CHECK 1","LMG_coax","LMG_coax"] 
_compatMags = [_muzzleTemp] call BIS_fnc_compatibleMagazines;
systemChat str ["CHECK 2",_muzzle,_muzzleTemp];		//["CHECK 2",bin\config.bin/CfgWeapons/LMG_coax,"LMG_coax"]

So basically, is _muzzle a special/reserved variable? I've used it before without a problem, so I initially thought it was a bug with the function but couldn't/still can't understand why it would be, so I made a ticket for it.

Share this post


Link to post
Share on other sites

 

Yes, I worked a lot around this function and created my own derived function for my script (or module).

The fact is BIS_fnc_compatibleMagazines works with _muzzle as local variable without make it private.

The inner line should be:

private _muzzle = if (_x == "this") then {_weaponCfg} else {_weaponCfg >> _x};
(same for _magazinesList...)

but private is missing!
So, if you are using this function after defining your own local variable _muzzle, the called function will override your variable.

In debug console:

0 = [] spawn {
  private _muzzle = "LMG_coax";
  systemChat str ["CHECK 1",_muzzle];
  _compatMags = [_muzzle] call BIS_fnc_compatibleMagazines;
  systemChat str ["CHECK 2",_muzzle];
};


is not same as:
 

0 = [] spawn {
  private _myMuzz = "LMG_coax";
  systemChat str ["CHECK 1",_myMuzz];
  _compatMags = [_myMuzz] call BIS_fnc_compatibleMagazines;
  systemChat str ["CHECK 2",_myMuzz];
};

 

A simple workaround is to spawn the function instead of calling it.


A called code don't re-initialize all defined local variables before. If a local variable exists when you call a code, this local variable can be used (then modified) as is, inside it, and then can be altered as you showed, on the contrary of a spawned code which only works with passed parameters or inner variables.

That makes called code more like if then scope. If you take the habit to make local variables private everywhere, there is no more such problem.

The BI function could be easily corrected...
 

  • Thanks 1

Share this post


Link to post
Share on other sites
6 minutes ago, pierremgi said:

A simple workaround is to spawn the function instead of calling it.

 

Nice, and thanks for the explanation. At the moment I'm working around it by saving and redefining the variable after function, but I'll see if using scriptDone after spawning makes much difference instead.

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

×