Jump to content
Sign in to follow this  
Sokoloft

Spawning & De-Spawning AI Units into a Vehicle via a script

Recommended Posts

Hello,

 

In the mission I am currently working on I need the ability to spawn pre-defined units via their string name, to a vehicle defined by either it's editor given variable name, or it's string name. Preferably the vehicles variable name. Then, I'm going to call the script via a trigger that is triggered via the Radio menu. As well as if it is possible, I'd like to then have another script that does the opposite, and de-spawns them, and will be called the same way via a trigger, and they will be On-Off options.

 

Oh and yes, text via side to say that it has been toggled on or off!

 

Any info is much appreciated, as I don't even know were to start.

 

PS: As well as could you explain how to do the same for objects if there is a difference? I could put this information to good use to put the USS Nimitz On station and Off station.

Share this post


Link to post
Share on other sites

Save the following as a function and use it with group1 = vehicle1 call fnc_something

params ["_veh"];
_group = createGroup east;
_unit1 = _group createUnit ["O_T_Crew_F", [0,0,0], [], 0, "NONE"];
_unit2 = _group createUnit ["O_T_Crew_F", [0,0,0], [], 0, "NONE"];
_unit3 = _group createUnit ["O_T_Crew_F", [0,0,0], [], 0, "NONE"];
{_x moveInAny _veh} forEach units _group;
_group

(It would be best to write a check for how many places to fill but I'm too lazy for that.)

 

To achieve what you want with the trigger, save all the vehicles you want to fill in an array and use

spawnedUnitsArray = [];
{spawnedUnitsArray append (units (_x call fnc_something))} forEach vehicleArray;

To despawn them simply use

{deleteVehicle _x} forEach spawnedUnitsArray;
  • Like 1

Share this post


Link to post
Share on other sites

Sorry, I can't get it to work :( Been trying for the past couple hours, I'll PM you with more details, and we'll post a solution here.

Share this post


Link to post
Share on other sites

There are several (easy to find) AI caching scripts available, you could try utilizing one of those.

Share this post


Link to post
Share on other sites

There are several (easy to find) AI caching scripts available, you could try utilizing one of those.

 

I wish it were that easy, however it isn't. AI caching isn't what I'm looking for. Thanks for the suggestion though! What @theend3r has is exactly what I need, only if I could get it to work >_<

Share this post


Link to post
Share on other sites

The whole solution (not tested):

//init.sqf

fnc_boatInf = compile preprocessFile "fnc_boatInf.sqf";

vehicleArray = [LCVP_3]; //you can add vehicles by putting this in their init: vehicleArray pushback this

//end init.sqf

//fnc_boatInf.sqf:
params ["_veh"];
_group = createGroup guerrila;
_unit1 = _group createUnit ["LIB_US_first_lieutenant", [0,0,0], [], 0, "NONE"];
_unit2 = _group createUnit ["LIB_US_second_lieutenant", [0,0,0], [], 0, "NONE"];
_unit3 = _group createUnit ["LIB_US_Submachinegunner", [0,0,0], [], 0, "NONE"];
{_x moveInAny _veh} forEach units _group;
_group
//end of fnc_boatInf.sqf:

//On Activation:

spawnedUnitsArray = [];
{spawnedUnitsArray append (units (_x call fnc_boatInf))} forEach vehicleArray;

//On deactivation:

{deleteVehicle _x} forEach spawnedUnitsArray;
  • Like 1

Share this post


Link to post
Share on other sites

Same here (also not tested):
 

fnc_toggleCrew = {
	private ["_veh","_cache","_type","_grp","_unit"];
	_veh = _this select 0;

	_cache = _veh getVariable ["cachedCrew",[]];
	if (count _cache < 1) then {
		{
			if (!isPlayer _x) then {
				_type = typeOf _x;
				_veh deleteVehicleCrew _x;
				_grp = group _x;
				_cache pushBack [_type,_grp];
			};
		} forEach crew _veh;
		_veh setVariable ["cachedCrew",_cache,true];
	} else {
		{
			_type = _x select 0;
			_grp = _x select 1;
			if (_type != "") then {
				_unit = _type createUnit [getPos _veh,_grp];
				_unit moveInAny _veh;
			};
		} forEach _cache;
		_veh setVariable ["cachedCrew",[],true];
	};
};

[yourvehicle] call fnc_toggleCrew;


Saves any crew that is currently in the vehicle, removes them and the next time you run the function it will restore them.

Share this post


Link to post
Share on other sites

So happy @theend3r took a bit of messing with and some operator error later I got it to work, going to use his since I currently have it working! Plus I don't need chacheing as I said earlier (Not sure if your script has that or not but I saw _cache somewere) Still new to this scripting thing, done lua/html and stuff in the past but never really stuck with it. But as I CTRL+C CTRL+V I learn at the same time, what each parameter does/syntax. As well as Thanks @Tajin , the more reference material we have here on the forums the easier it'll be for the community to implement features like these!

 

Now I have another question that has been plaguing me for a while!

 

I'd like to make another fnc like this one, that is called in the same way via the Alpha Radio option in a trigger, that will change the presence of the defined editor variable given to an object, so I name the object Disappear,Disappear_1,ect

 

Edit:

 

How could I make it to were it does it to any currently spawned LCVP? I tried the following and it didn't work :(

vehicleArray = ["LIB_LCVP",LCVP_1,LCVP_2,LCVP_3];

Share this post


Link to post
Share on other sites

As well as I can not figure out how to determine the "vehicleArray = [LCVP_1,LCVP_2,LCVP_3];" via the script yet, that would be nice since I plan on using it multiple times on different variable names for different vehicles.

 

Edit:

Think I fixed it, this is on the trigger now:

vehicleArray = [LCVP_1,LCVP_2,LCVP_3];
spawnedUnitsArray = []; 
{spawnedUnitsArray append (units (_x call fnc_boatInf))} forEach vehicleArray;

Edit2:

 

Having troubles, can you lay out exactly what the params on this string? I know the "" is the unit string, but the 0,0,0 has got to be x/y/z, what are the 2 others, because I need to set them to groups, so I can execute setBehaviour to combat, and is the "NONE" there skill or?? That might explain why they're not shooting O_O

["LIB_GER_rifleman", [0,0,0], [], 0, "NONE"]
  string name model    x/y/z   ?   ?    ???

Share this post


Link to post
Share on other sites

UPDATE: Fixing it currently, going to update this post soon as it's 100%.

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
Sign in to follow this  

×