Jump to content
🛡️FORUMS ARE IN READ-ONLY MODE Read more... ×
Sign in to follow this  
Rogue Null

Dynamic addaction & array locality question

Recommended Posts

I have some code set up to dynamicly spawn vehicles and attachTo guns at the start of a mission. Because the guns may be on top of a tall vehicle (e.g. a bus), I add an action on the bus for a player to be setInGunner to the guns.

In singleplayer, this works fine and I spawn a UAZ and a BUS with all the guns and added actions. There is a slight problem in that an action is added for the sandbags on the bus, which suggests that (see script below) my use of GunObjectList to copy the contents of _fullObjectList at a certain point is incorrect.

However, the main issue I have is that this simply does not add the actions in multiplayer. I tried to get around this by making a list of the guns a public variable, but I think having server side and client side commands in the same script is confusing things - perhaps I need to pass variables back out from a server only script?

(Aside: the respawn script simply calls this script again if the vehicle is destroyed, so that is not the problem. Obviously, people who JIP won't have actions to mount the guns until this happens, but that is a problem for another time and the vehicles will be dying quickly anyway)

// spawnvehicletype1
private ["_VehicleType","_WeaponType","_SpawnLocation","_AttachToOffset","_vehicle","_gun","_mountaction","_vehicleTypeNum","_VehicleName","_fullObjectList"];
waituntil {!isnil "bis_fnc_init"};

if (isServer) then {
_SpawnLocation=_this select 0;   //"M2_UAZ_Marker1";
_VehicleName=_this select 1; //"M2_UAZ_01"
_vehicleTypeNum=_this select 2; //1;

switch (_vehicleTypeNum) do {
	case 1: {_VehicleType="UAZ_Unarmed_TK_CIV_EP1"; // EDIT HERE - vehicle type
				_vehicle = _VehicleType createVehicle (getMarkerPos _SpawnLocation);
				_fullObjectList = [_vehicle];

			_WeaponType="M2StaticMG_US_EP1"; // EDIT HERE - mounted weapon type
			_AttachToOffset=[0,0,1]; // EDIT HERE - attachTo location
				_gun = _WeaponType createVehicle (getMarkerPos _SpawnLocation);
				_gun AttachTo [_vehicle,_AttachToOffset];
				_gun setDir 0; //point gun in specific direction relative to vehicle
				_fullObjectList set [(count _fullObjectList), _gun]; //adds _gun to end of _fullObjectList, because count fullobjectlist = # of items in list, wheras set applies to the 0th, 1st, 2nd, etc object, e.g. count-1.		

			gunObjectList = _fullObjectList; // THIS SHOULD APPEAR AFTER LAST GUN BUT BEFORE ANY OTHER ATTACHTOS
			publicVariable "gunObjectList";
			}; 

	case 2: {_VehicleType="Ikarus_TK_CIV_EP1"; // EDIT HERE - vehicle type
				_vehicle = _VehicleType createVehicle (getMarkerPos _SpawnLocation);
				_fullObjectList = [_vehicle];

			_WeaponType="M2StaticMG_US_EP1"; // EDIT HERE - mounted weapon type
			_AttachToOffset=[0,-1,1.5]; // EDIT HERE - attachTo location
				_gun = _WeaponType createVehicle (getMarkerPos _SpawnLocation);
				_gun AttachTo [_vehicle,_AttachToOffset];
				_gun setDir 0; //point gun in specific direction relative to vehicle
				_fullObjectList set [(count _fullObjectList), _gun]; //adds _gun to end of _fullObjectList, because count fullobjectlist = # of items in list, wheras set applies to the 0th, 1st, 2nd, etc object, e.g. count-1.					

			_WeaponType="M2StaticMG_US_EP1"; // EDIT HERE - mounted weapon type
			_AttachToOffset=[0,1,1.5]; // EDIT HERE - attachTo location
				_gun = _WeaponType createVehicle (getMarkerPos _SpawnLocation);
				_gun AttachTo [_vehicle,_AttachToOffset];
				_gun setDir 0; //point gun in specific direction relative to vehicle
				_fullObjectList set [(count _fullObjectList), _gun]; //adds _gun to end of _fullObjectList, because count fullobjectlist = # of items in list, wheras set applies to the 0th, 1st, 2nd, etc object, e.g. count-1.		

			_WeaponType="M2StaticMG_US_EP1"; // EDIT HERE - mounted weapon type
			_AttachToOffset=[2,0,1.5]; // EDIT HERE - attachTo location
				_gun = _WeaponType createVehicle (getMarkerPos _SpawnLocation);
				_gun AttachTo [_vehicle,_AttachToOffset];
				_gun setDir 180; //point gun in specific direction relative to vehicle
				_fullObjectList set [(count _fullObjectList), _gun]; //adds _gun to end of _fullObjectList, because count fullobjectlist = # of items in list, wheras set applies to the 0th, 1st, 2nd, etc object, e.g. count-1.	

			gunObjectList = _fullObjectList; // THIS SHOULD APPEAR AFTER LAST GUN BUT BEFORE ANY OTHER ATTACHTOS
			publicVariable "gunObjectList";

			// non-weapon attachto example
			_WeaponType="Land_fort_bagfence_corner"; // EDIT HERE - mounted weapon type
			_AttachToOffset=[-2,0,0]; // EDIT HERE - attachTo location
				_gun = _WeaponType createVehicle (getMarkerPos _SpawnLocation);
				_gun AttachTo [_vehicle,_AttachToOffset];
				_gun setDir 180; //point gun in specific direction relative to vehicle
				_fullObjectList set [(count _fullObjectList), _gun]; //adds _gun to end of _fullObjectList, because count fullobjectlist = # of items in list, wheras set applies to the 0th, 1st, 2nd, etc object, e.g. count-1.	
			}; 								

	default {};
};

// This block of commands is applied to the assembled final vehicle only.
_vehicle setVehicleInit format ["%1 = this; this setVehicleVarName ""%1""",_VehicleName];
processInitCommands;
null = [_vehicle,_vehicleTypeNum,_SpawnLocation,_fullObjectList] execVM "vehicle_respawn_3.sqf";
};

sleep 2;

//if the person running this script is a singleplayer client or not a dedicated server, then add actions to mount the weapons (COULD do a case statement to manually add unique names for each weapon, but this takes up less space!)
if ((!isDedicated) || ( (playersNumber west + playersNumber east + playersNumber civilian + playersNumber resistance)==0 )) then {	
for "_x" from 1 to ((count gunObjectList)-1) do 
{
       //hint format ["Mount %1 %2", typeOf (gunObjectList select _x),gunObjectList select _x];
       _mountaction = _vehicle addaction [ format["Mount %1 %2", typeOf (gunObjectList select _x), _x], "Gotogun.sqf", [gunObjectList select _x] ];
};
};

Share this post


Link to post
Share on other sites

Ok, not an exact example. But I'm sure you will get the gist of it.

_vehicle=...You global object

if isServer then
{
//Create a gun local to the server here
_attachedGun=...create your gun here;

//Mount it on the vehicle
_attachedGun attachTo [_vehicle,_AttachToOffset];

//Broadcast it to all the clients
_vehicle setVariable ["RON_ATTACHED_GUNPOS1",_attachedGun,true];
}
else
{
//Initialise you variable in this scope
_attachGunPos1=objNull;

//Wait for the server to brodcast it to each client
waitUntil {_attachGunPos1=_vehicle getVariable "RON_ATTACHED_GUNPOS1" ; !(isNil "_attachGunPos1")};

//Add your action locally to each client
_attachGunPos1 addAction [format ["Get in %1",typeOf _attachGunPos1],"Gotogun.sqf"];
};

You could cut out a lot of the above, if you use setvehicleInit and processInitCommands. But those commands are best used when you want to keep a history of all the objects created during a mission, for people who JIP. As you said they will be killed off quite quickly, you're better off using a method similar to the above example.

The example also assumes you will be running the mission on a dedicated server. To also cater for a non-dedicated server, then you will need to change it slightly.

Share this post


Link to post
Share on other sites

Any actions need to be added separately on all machines (and when activated, will only activate on the machine that used the action, be it by the player, or I think also an AI controlled by the player). For dynamically created vehicles, of course, this gets a bit more difficult as you have to first broadcast the vehicle's variable and then use it on the client to add the action locally.

Share this post


Link to post
Share on other sites
Sign in to follow this  

×