Jump to content

- - - - -

Getting object by string name

  • Please log in to reply
1 reply to this topic

#1 ChronicSilence



  • Members
  • 83 posts

Posted 01 December 2013 - 04:27 AM

I created a vehicle in the mission editor. Knowing the name of that vehicle, how can I get an object handle to it in a script? I've tried:

call compile "_vehObj = "+objectName";

but no go. In theory, I guess you could loop through every vehicle in the mission until you find the one that has the right name, but that is extraordinarily inefficient. Any thoughts?


#2 Mattar_Tharkari


    First Sergeant

  • Members
  • 964 posts

Posted 01 December 2013 - 12:41 PM

well (if I understand the question??) generally you would do this:
_hdl = [objectName] execVM "someScript";
[objectName] call someFunction;
[objectName] spawn {someThing};

then in the script:
_object = _this select 0;

another way is to save the object as a variable:

_veh = createVehicle ["B_G_Quadbike_01_F", position player, [], 0, "NONE"];
missionNamespace setVariable ["refString",_veh];//"refString" can be anything you like but try to make it unique so it doesn't get overwritten
In a script
_veh = missionNamespace getVariable "refString";
_veh setDamage 1;
will return the object reference

If you create multiple vehicles:
_vehArr = []
_vehrefArr = []

for "_i" from 0 to 9 do {
_veh = createVehicle ["B_G_Quadbike_01_F", position player, [], 0, "NONE"];
_ref = format ["quad%1",_i];
missionNamespace setVariable ["_ref",_veh];

_vehArr set [count _vehArr,_veh];
_vehrefArr set [count _vehrefArr,_ref];

missionNamespace setVariable ["quadVehicles",_vehArr];
missionNamespace setVariable ["quadVehRefs",_vehrefArr];

You now have 3 ways of accessing the stored data in another script:
_vehref = missionNamespace getVariable "quad9"; //the individual vehicles - strings "quad0" - "quad9"
_vehArr = missionNamespace getVariable "quadVehicles";//array of vehicles _vehArr select 0 to _vehArr select 9;
_vehrefArr = missionNamespace getVariable "quadVehRefs";//array of reference strings ["quad0" ..."quad9"]
_my1stVeh = missionNamespace getVariable (_vehrefArr select 0);

Edited by Mattar_Tharkari, 01 December 2013 - 11:59 PM.

"I believe everything. And l believe nothing. I suspect everyone. And l suspect no one. I gather the facts, examine the clues... and before you know it, the case is sol-ved!"
Arma III http://www.arma3.com/