Jump to content
SOVIET_IDIOT

How to write a script for hideObject on dozens of units?

Recommended Posts

I have dozens of units which are Slow Zombies from Zombies and Demons mod they're not grouped together. I thought of writing zombiename hideObject true; on every single one of them in the sqf file but there's a lot. I want to avoid using the show/hide module due to syncing them cause having multiple modules gets confusing on EDEN. Is there a way to list all of the zombies and then just writing that list to the hideObject code. The problem is they have different different class names. Thanks in advance.

Share this post


Link to post
Share on other sites

I solved it I have to add enableSimulation as well so they won't be just invisible. Is there a way to have the array just part of the name of object like instead the whole "zomb1", "zomb2" it will be just every object with "zomb" on the name?

Share this post


Link to post
Share on other sites

Hey there SOVIET_IDIOT. A couple of simple solutions would be to do something like (copying code provided by UnDeaD.)

// Get all the objects whose name starts with "zomb"
private _objArr = (allMissionObjects "All") select {(vehicleVarName  _x) find "zomb" == 0};

// Disable simulation to all of them
_objArr apply {_x enableSimulation false};

Alternatively, you could create the array and then go through it like

// Initialise array
private _objArr = "_zombArr = [";

// Create an array as a string (go from 0 to 99)
for[{private _i = 0}, {_i < 100}, {_i = _i + 1}] do {
	// Append the "zombX" string,
	// where X is equal to the current value of _i
	_zombArr = _zombArr + ("zomb" + (str _i));
                                 
	// Add a comma
	if(_i < 99) then {
		_zombArr = _zombArr + ", ";
	};
};

// Add the closing bracket and the assignment to a variable
_zombArr = _zombArr + "];";

// Now you can compile and call that to be an array
call (compile _zombArr);

// Next step, disable simulation to all those objects
_zombArr apply {_x enableSimulation false;};

This may seem a wee bit of an overburden compared to the first method, but this may be a wee bit more flexible if you would like to do something to only a "subset" of all the "zombie range". You could also change the "zomb" part and the initial or the range to get the objects for resulting in an array starting at "zomb2" and going up to "zomb5" for example.

 

This morning I happened to create such a function which you could use as-is, adapt, or keep parts of it without having to credit or reference anyone (it is licenced under The Unlicenced). I will copy it here for your convenience (see below). Additionally, you could have a look at this (recent) post in these ArmA 3 forums.

 

Spoiler

/*
 * ============================================================
 * Function to create an array with custom named elements.
 * The elements' naming convention is provided as a string
 * string appended by an increasing value.
 *
 * Example: [obj0, obj1, obj2, obj3]
 * ============================================================
 * Author: Achilles
 * email: axilleaz@hotmail.com
 * Date: 12/01/2021 (DD/MM/YYYY)
 * Licence: The Unlicenced
 * ============================================================
 * Input
 * ==========
 * _numOfElems [Number]: How many elements the array will have.
 * _prependStr [String] (Optional): The string to prepend to the
 *                                  numbers. [Default: p]
 * _startIdx [Number] (Optional): The index from which to start
 *                                 counting. [Default: 0]
 * Output
 * ==========
 * The created array
 * ============================================================
 * How to use
 * ==========
 * Call the function/script
 * ==========
 * Example: Create an array with elements [unit0, unit1, unit2, unit3, unit4]
 *
 * private _unitArr = [5, "unit"] call YOU_fnc_createArray;
 * ==========
 * Example: Create array with elements [car5, car6, car7, car8, car9, car10, car11]
 *
 * private _carArr = [7, "car", 5] call YOU_fnc_createArray;
 */
/*
 * Get parameters
 */
params[["_numOfElems", 0, [0], // Number of elements
       ["_prependStr", "p", [""]], // String to prepend to numbers
       ["_startIdx", 0, [0]]]; // Element to start counting from

/*
 * Check arguments
 */
// Check _numOfElems
if(_numOfElems <= 0) exitWith {
  ["Number of elements must be non negative."] call BIS_fnc_error;
};

// Check _prependStr
if(_prependStr == "") exitWith {
  ["String to prepend should be non empty."] call BIS_fnc_error;
}

// Check _startElem
if(_startIdx < 0) exitWith {
  ["First element index must be non negative."] call BIS_fnc_error;
}

/*
 * Initialise some variables
 */
private _maxIter = _startIdx + _numOfElems;
private _arr = "_arr = [";

/*
 * Create the array
 */
// Add the elements
for[{private _i = _startIdx}, {_i < _maxIter}, {_i = _i + 1}] do {
  // Add element
  _arr = _arr + _prependStr + (str _i);

  // Add comma
  if(_i < (_maxIter - 1)) then {
    _arr = _arr + ", ";
  };
};

// Add closing bracket
_arr = _arr + "];";

// Compile and call
call (compile _arr);

// Return the array (may be redundant)
_arr;

 

Please note that I haven't personally tested any of those solutions (not even the function I provide in the end) so you should treat them with caution and of course test before use.

 

Hope this helps somehow and if you need further assistance please don't hesitate to ask.

Share this post


Link to post
Share on other sites

 @ZaellixA  No, your "unlicenced" function can't work. If you call compile stupidString from such array, what you'll get is just a bunch of <any> elements.

To make it short:

globVar = "aString";

result = compile globVar ; // {astring}  // that looks like a code

result = call compile globVar:  // any   as astring is not a command or even something existing.

 

What is working is:

_arr = ["existingObj1","existingObj2",...];
_arr = _arr apply {call compile _x};     // returns  [existingObj1,existingObj2,...];

with defined objects (dead or alive) (or a working stringified code but it's not the topic here)

Share this post


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

 @ZaellixA  No, your "unlicenced" function can't work. If you call compile stupidString from such array, what you'll get is just a bunch of <any> elements.

To make it short:

globVar = "aString";

result = compile globVar ; // {astring}  // that looks like a code

result = call compile globVar:  // any   as astring is not a command or even something existing.

 

What is working is:

_arr = ["existingObj1","existingObj2",...];
_arr = _arr apply {call compile _x};     // returns  [existingObj1,existingObj2,...];

with defined objects (dead or alive)

Hhhmmm, not sure I understand... isn't a string like "_zombArr = [zomb0, zomb1, zomb2, ...];" supposed to be callable code?

 

I can see the difference in your code (_arr apply {call compile _x};) compared to mine but I am wondering why mine wouldn't compile into something "meaningful". Could you please shed some light on it, as I seem to lack intuition on here... 😟

Share this post


Link to post
Share on other sites

zomb0 zomb1 zomb2  must be existing objects. If not... your compiled array (mine also, it was another mean) will return [<any>,<any>,..] So no error but nothing to do with that.

  • Like 1

Share this post


Link to post
Share on other sites

As i understand, you have preplaced them in editor? If so, i would add them to layer, and list them with "getMissionLayerEntities". 

  • 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

×