Getting object by string name

2 posts in this topic

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?


Share this post

Link to post
Share on other sites

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

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