Jump to content
marki980908

Is there a way to create this array? object0 to object99

Recommended Posts

I would like to do this array

objArr = ["_object0","_object1","_object2"]; {_X enableSimulation false;} forEach objArr;

at the end of a code which is used to spawn shit load of objects. Is there a way to make this array go from _object0 to _object99 for example, without typing out every single one of them?
if not, is there any other way to solve this issue?
Situation: I have an sqf code from Zeus Enhanced to spawn shit load of objects, I want to add a code at the end of it to make them all have disabled simulations.

Share this post


Link to post
Share on other sites

Hi,

this solution worked for me:

 

objArr = ((allMissionObjects "All") select {(vehicleVarName  _x) find "_object" == 0});

{_x enableSimulation false} forEach objArr;

 

What this does:

Checks for every object that has _object in the begining of it's variable name.

If you want to check objects that have _object not only at the begining but in the middle for example, then just change ==  to >=

Also make sure the objects have distinguishable name, so the script won't run on unwanted objects.

 

Hope could help 😉

 

  • Like 1

Share this post


Link to post
Share on other sites

Hey there, you already have a nice working solution. I would like to suggest another one where you actually "synthesise" the array yourself as a string and then you make it an actual array. This could look like

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

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

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

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

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

The same limitation with the object variable names applies here too. You have to make sure they are uniquely named and that their names match those of the array of course.

 

Although the above code snippet may seem like an overburden in many ways (coding and extra work which can be done with fewer and possibly faster commands), it does provide a quite nice way to create objects "on the fly". You could use the array to create the objects via script commands (in the same way you use enableSimulation command).

 

One more minor detail about the code above is that I used apply instead of forEach, which according to BIKI it is a wee bit faster (I don't believe you will notice some serious improvement though).

 

Finally, please note that I have used similar code in the past but this specific snippet is not tested, so treat with caution. I do hope that this may help somehow.

  • Like 2

Share this post


Link to post
Share on other sites

I got a generic expression error, let me showcase the situation
 

_object0 = createVehicle ["B_Heli_Transport_03_unarmed_F", [0, 0, 0], [], 0, "CAN_COLLIDE"];
_object0 setVectorDirAndUp [[0.000118554,0.999834,0.0181859],[-1.05015e-005,-0.0181859,0.999834]];
_object0 setPosASL [6223.83,6934.69,45.3438];
[_object0, ["Black",1], [], true] call BIS_fnc_initVehicle;
{_object0 removeMagazineTurret (_x select [0, 2])} forEach magazinesAllTurrets _object0;
{_object0 addMagazineTurret _x} forEach [["168Rnd_CMFlare_Chaff_Magazine",[-1],168]];
_object1 = createVehicle ["B_Heli_Transport_03_unarmed_F", [0, 0, 0], [], 0, "CAN_COLLIDE"];
_object1 setVectorDirAndUp [[-0.000188412,0.999817,0.0191205],[1.06743e-005,-0.0191205,0.999817]];
_object1 setPosASL [6203.69,6936.69,45.3438];
[_object1, ["Black",1], [], true] call BIS_fnc_initVehicle;
{_object1 removeMagazineTurret (_x select [0, 2])} forEach magazinesAllTurrets _object1;
{_object1 addMagazineTurret _x} forEach [["168Rnd_CMFlare_Chaff_Magazine",[-1],168]];

This is the code created by a mod called "Zeus Enhanced". This code saved 2 helicopters ~10 meters above the ground. When I will spawn them in, I need them to have their simulations disabled. As you can see, the mod names all the objects _object# in increasing numerical order.
For this example I could place 2 codes which would disable sims behind each object in this situation, but however if I will save huge number of objects, that thing, wont be practical anymore.

For some reason even manual solution like:
 

_object0 = createVehicle ["B_Heli_Light_01_F", [0, 0, 0], [], 0, "CAN_COLLIDE"];
_object0 setVectorDirAndUp [[-1.54855e-005,0.999999,0.00154038],[-1.99978e-007,-0.00154038,0.999999]];
_object0 setPosASL [6197.96,6936.02,51.7186];
[_object0, [], ["AddTread",1,"AddTread_Short",0], true] call BIS_fnc_initVehicle;
{_object0 removeMagazineTurret (_x select [0, 2])} forEach magazinesAllTurrets _object0;
{_object0 addMagazineTurret _x} forEach [];
_object1 = createVehicle ["B_Heli_Light_01_F", [0, 0, 0], [], 0, "CAN_COLLIDE"];
_object1 setVectorDirAndUp [[-1.5478e-005,0.999999,0.00154037],[-2.4903e-007,-0.00154037,0.999999]];
_object1 setPosASL [6223.71,6933.9,51.7186];
[_object1, [], ["AddTread",1,"AddTread_Short",0], true] call BIS_fnc_initVehicle;
{_object1 removeMagazineTurret (_x select [0, 2])} forEach magazinesAllTurrets _object1;
{_object1 addMagazineTurret _x} forEach [];

_objArr = ["_object0","_object1"]; {_X enableSimulation false;} forEach _objArr;

For some reason doesnt work, I am getting generic error even with this

Share this post


Link to post
Share on other sites

You can loop through the _objectX variables like this:

 

for "_i" from 0 to 10000 do
{
 _varname = format["_object%1",_i];
 if(isnil _varname) exitwith {};

 _var = call compile _varname;
 _var enableSimulation false;
};

 

  • Like 3

Share this post


Link to post
Share on other sites
On 1/11/2021 at 3:12 PM, gc8 said:

You can loop through the _objectX variables like this:

 


for "_i" from 0 to 10000 do
{
 _varname = format["_object%1",_i];
 if(isnil _varname) exitwith {};

 _var = call compile _varname;
 _var enableSimulation false;
};

 

It worked, I love you

Share this post


Link to post
Share on other sites
On 1/11/2021 at 2:56 PM, marki980908 said:

I got a generic expression error, let me showcase the situation
 


_object0 = createVehicle ["B_Heli_Transport_03_unarmed_F", [0, 0, 0], [], 0, "CAN_COLLIDE"];
_object0 setVectorDirAndUp [[0.000118554,0.999834,0.0181859],[-1.05015e-005,-0.0181859,0.999834]];
_object0 setPosASL [6223.83,6934.69,45.3438];
[_object0, ["Black",1], [], true] call BIS_fnc_initVehicle;
{_object0 removeMagazineTurret (_x select [0, 2])} forEach magazinesAllTurrets _object0;
{_object0 addMagazineTurret _x} forEach [["168Rnd_CMFlare_Chaff_Magazine",[-1],168]];
_object1 = createVehicle ["B_Heli_Transport_03_unarmed_F", [0, 0, 0], [], 0, "CAN_COLLIDE"];
_object1 setVectorDirAndUp [[-0.000188412,0.999817,0.0191205],[1.06743e-005,-0.0191205,0.999817]];
_object1 setPosASL [6203.69,6936.69,45.3438];
[_object1, ["Black",1], [], true] call BIS_fnc_initVehicle;
{_object1 removeMagazineTurret (_x select [0, 2])} forEach magazinesAllTurrets _object1;
{_object1 addMagazineTurret _x} forEach [["168Rnd_CMFlare_Chaff_Magazine",[-1],168]];

This is the code created by a mod called "Zeus Enhanced". This code saved 2 helicopters ~10 meters above the ground. When I will spawn them in, I need them to have their simulations disabled. As you can see, the mod names all the objects _object# in increasing numerical order.
For this example I could place 2 codes which would disable sims behind each object in this situation, but however if I will save huge number of objects, that thing, wont be practical anymore.

For some reason even manual solution like:
 


_object0 = createVehicle ["B_Heli_Light_01_F", [0, 0, 0], [], 0, "CAN_COLLIDE"];
_object0 setVectorDirAndUp [[-1.54855e-005,0.999999,0.00154038],[-1.99978e-007,-0.00154038,0.999999]];
_object0 setPosASL [6197.96,6936.02,51.7186];
[_object0, [], ["AddTread",1,"AddTread_Short",0], true] call BIS_fnc_initVehicle;
{_object0 removeMagazineTurret (_x select [0, 2])} forEach magazinesAllTurrets _object0;
{_object0 addMagazineTurret _x} forEach [];
_object1 = createVehicle ["B_Heli_Light_01_F", [0, 0, 0], [], 0, "CAN_COLLIDE"];
_object1 setVectorDirAndUp [[-1.5478e-005,0.999999,0.00154037],[-2.4903e-007,-0.00154037,0.999999]];
_object1 setPosASL [6223.71,6933.9,51.7186];
[_object1, [], ["AddTread",1,"AddTread_Short",0], true] call BIS_fnc_initVehicle;
{_object1 removeMagazineTurret (_x select [0, 2])} forEach magazinesAllTurrets _object1;
{_object1 addMagazineTurret _x} forEach [];

_objArr = ["_object0","_object1"]; {_X enableSimulation false;} forEach _objArr;

For some reason doesnt work, I am getting generic error even with this

Not sure this was the problem but you used a capital "x" instead of a small one. Since .sqf is case sensitive _X is different than _x, where the latter is the magic variable that corresponds to each object and (most probably) _X is undefined.

 

Nevertheless, gc8 have you a nice solution to your problem. Alternatively, you could create the array in the way I showed you above and compile once in the end (instead of compiling the variable in each iteration) and use either a forEach to go through it, or apply to disable the simulation to all elements.

 

Not sure which approach is faster since in gc8's solution you have to compile each time but in the one I suggest here you will have to go through the array twice (one time to create it and one to disable simulation).

Share this post


Link to post
Share on other sites

Not sure to understand why you don't use the local variables if you can add a line in the same scope of all that stuff...

_objArr = [_object0,_object1,_object2]; {_x enableSimulation false} forEach _objArr;

 

To clarify, see also this post,  if _object0,_object1... are defined in this scope, you don't need to stringify them for call compiling them in the same scope ... It's just a waste to code. On the other hand, if you are trying to call compile such "_object0" "_object1", inside a scope ignoring what _object0 _object1 are referring to (so, in an other parallel scope) , your Goldberg machine will fail.

Share this post


Link to post
Share on other sites
14 hours ago, ZaellixA said:

Since .sqf is case sensitive

Erm, no its not.

  • Like 1

Share this post


Link to post
Share on other sites

Agreeing with what Pierre said. But also, why not just add the unit to an array as you spawn it:

_objectArray = [];
for "_i" from 1 to 2 do {
 _object = createVehicle ...
 //the rest of your code
 _objectArray pushBack _object;
};

{
 _x enableSimulation false;
} forEach _objectArray;

OR, just disable the simulation when it spawns (probably much better if there's a lot to spawn):

for "_i" from 1 to 2 do {
 _object = createVehicle ...
 //the rest of your code
 _object enableSimulation false;
};

With the second method you can still add them to an array to manipulate them later, incase you want to re-enable their simulation. Just use pushBack or append (I'm not sure which is faster) as in the first example.

 

And remember, enableSimulation takes an object, not a string, so trying to do "objectName" enableSimulation false is never going to work.

  • Like 2

Share this post


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

Erm, no its not.

I guess you are right... I, myself have typed forEach (and many more) like foreach many times... Not sure why I had this feeling that .sqf is case sensitive... I strongly apologize for that... Thanks a lot.

  • 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

×