Jump to content
Sign in to follow this  
Migal

magic variable out of cicle

Recommended Posts

Hi, I got this code from the legacy BIS_fnc_synchronizedObjects function.

 

Quote

private _condition = if (_preciseMatch) then
{
    {typeOf _synced == _x};
}
else
{
    {_synced isKindOf _x};
}; 

What the "_x" variable means here? I just know the meaning inside loops.

 

For completeness there the full function.

 

Thank you

 

/*
	Author: Jiri Wainar

	Description:
	Get all objects of given type directly synchronized to the given object. Works in EDEN too.

	Parameter(s):
	0: OBJECT - parent object
	1: STRING or ARRAY - class name filter; 1 or more classnames of objects we are interested about
	2: BOOL - precise match; default: true - only precise class names are considered, if false, inheritance is considered

	Returns:
	ARRAY - array of synchronized objects

	Example:
	[BIS_Poliakko,"LocationArea_F"] call BIS_fnc_synchronizedObjects;
*/

private _parent 		= _this param [0,objNull,[objNull]];
private _classnames 	= _this param [1,[],[[],""]];
private _preciseMatch 	= _this param [2,true,[true]];
private _output 		= [];
private _synced 		= objNull;

if (_classnames isEqualType "") then
{
	_classnames = [_classnames];
};

private _condition = if (_preciseMatch) then
{
	{typeOf _synced == _x};
}
else
{
	{_synced isKindOf _x};
};

private _objects = if (is3DEN) then
{
	(get3DENConnections _parent select {_x select 0 == "Sync"}) apply {_x select 1}
}
else
{
	synchronizedObjects _parent
};


{
	_synced = _x;

	if (_condition count _classnames > 0) then
	{
		_output pushBack _synced;
	};
}
forEach _objects;

_output

 

Share this post


Link to post
Share on other sites

_X entry from Biki entry on Magic Variables:

 

Quote

_x

Represents the current element during a loop with: apply, count, configClasses, configProperties, findIf, forEach, select.

 

Not all loops are as easy to recognize/understand as a forEach, for example.

 

Example 6 from Biki entry on select:

_even = [1,2,3,4,5,6,7,8,9,0] select { _x % 2 == 0 }; // returns [2, 4, 6, 8, 0]

Select is iterating through the array and applying the given expression to each element.

 

As for your first example using typeOf, it's taking all of my meager brain power to parse exactly what is happening, but _x is clearly the class string passed to the function. 

 

private _condition = if (_preciseMatch) then //if the string is the exact className
{
    {typeOf _synced == _x}; //then _synced is that type of className
}
else
{
    {_synced isKindOf _x}; //otherwise _synced is just a kind of the parent class
}; 

Or something like that.

  • Like 2

Share this post


Link to post
Share on other sites

The _x is the element being cycled within the count, when _classnames are counted.

 

{typeOf _synced == _x} count _classnames > 0
- or -
{_synced isKindOf _x} count _classnames > 0

 

The count condition creates a separate magic variable scope within the forEach.  That's why the cycled object _x gets redefined as _synced.  Then you can have a comparison that in a way says "if (that _x isKindOf this _x) ....".

  • Like 2
  • Thanks 1

Share this post


Link to post
Share on other sites
On 1/8/2022 at 12:43 AM, opusfmspol said:

The _x is the element being cycled within the count, when _classnames are counted.

 

{typeOf _synced == _x} count _classnames > 0
- or -
{_synced isKindOf _x} count _classnames > 0

 

The count condition creates a separate magic variable scope within the forEach.  That's why the cycled object _x gets redefined as _synced.  Then you can have a comparison that in a way says "if (that _x isKindOf this _x) ....".

 

Thank .

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  

×