UltimateBawb 1 Posted July 29, 2013 I've been making a mission with dynamic AI and vehicle spawning and have been having a problem with the creation of vehicles in IF statements. I'm aware that local variables created in IF statements do not apply for the rest of the block and would normally pre-define say an integer or string variable at the top of the block, but how can I do this with a vehicle variable? The code I currently have is: //Decides which vehicle to place based on multipliers and probability _prob = floor(random 100) * defendingMultiplier; if (_prob <= 20) then { _heli = createVehicle ["O_Heli_Attack_02_F", _targetMarkerA, [], 0, "NONE"]; ["Resources", 0.1] execVM "personalityHandler.sqf"; } else { _heli = createVehicle ["O_Heli_Light_02_F", _targetMarkerA, [], 0, "NONE"]; ["Resources", 0.05] execVM "personalityHandler.sqf"; }; _heliGroup addVehicle _heli; This results in errors when attempting to use _heli later. Share this post Link to post Share on other sites
mindstorm 8 Posted July 29, 2013 _varName are local variables and will be cleared after the code hids an ending bracket ( }; ). In this case you should add the private statement to declare a variable before the brackets. private "_heli"; if (_prob <= 20) then { _heli = createVehicle ["O_Heli_Attack_02_F", _targetMarkerA, [], 0, "NONE"]; ["Resources", 0.1] execVM "personalityHandler.sqf"; } else { _heli = createVehicle ["O_Heli_Light_02_F", _targetMarkerA, [], 0, "NONE"]; ["Resources", 0.05] execVM "personalityHandler.sqf"; }; _heliGroup addVehicle _heli; Share this post Link to post Share on other sites
Sniperwolf572 758 Posted July 29, 2013 I've been making a mission with dynamic AI and vehicle spawning and have been having a problem with the creation of vehicles in IF statements. I'm aware that local variables created in IF statements do not apply for the rest of the block and would normally pre-define say an integer or string variable at the top of the block, but how can I do this with a vehicle variable? The code I currently have is: //Decides which vehicle to place based on multipliers and probability _prob = floor(random 100) * defendingMultiplier; if (_prob <= 20) then { _heli = createVehicle ["O_Heli_Attack_02_F", _targetMarkerA, [], 0, "NONE"]; ["Resources", 0.1] execVM "personalityHandler.sqf"; } else { _heli = createVehicle ["O_Heli_Light_02_F", _targetMarkerA, [], 0, "NONE"]; ["Resources", 0.05] execVM "personalityHandler.sqf"; }; _heliGroup addVehicle _heli; This results in errors when attempting to use _heli later. Assuming this is the entire script, you'd do something like private["_prob", "_heli"]; //Decides which vehicle to place based on multipliers and probability _prob = floor(random 100) * defendingMultiplier; if (_prob <= 20) then { _heli = createVehicle ["O_Heli_Attack_02_F", _targetMarkerA, [], 0, "NONE"]; ["Resources", 0.1] execVM "personalityHandler.sqf"; } else { _heli = createVehicle ["O_Heli_Light_02_F", _targetMarkerA, [], 0, "NONE"]; ["Resources", 0.05] execVM "personalityHandler.sqf"; }; _heliGroup addVehicle _heli; Share this post Link to post Share on other sites
kylania 568 Posted July 29, 2013 Never repeat code. :) //Decides which vehicle to place based on multipliers and probability _prob = floor(random 100) * defendingMultiplier; _selectedType = if (_prob <= 20) then [{["O_Heli_Attack_02_F", 0.1]}, {["O_Heli_Light_02_F", 0.05]}]; ["Resources", _selectedType select 1] execVM "personalityHandler.sqf"; _heli = createVehicle [_selectedType select 0, _targetMarkerA, [], 0, "NONE"]; _heliGroup addVehicle _heli; Share this post Link to post Share on other sites
UltimateBawb 1 Posted July 29, 2013 Ah, Kylania, that's a great idea! Also, thanks Mind and Sniper for showing how to define variables. Share this post Link to post Share on other sites