Jump to content
beako

Spawning units in a real building (not on top of electrical wires ...etc.)

Recommended Posts

HI All,

 

I'm trying to write a script that spawns units in a nearby building (like houses, farms, hangers, anything with a roof and door), but don't want them to spawn on telephone lines or electrical lines or any weird stuff...

 

#1 I thought BIS_fnc_isBuildingEnterable would help filter these type of buildings out,  but it didn't...how can I avoid spawn from these types of places?

 

Here is my script.

 

#2 Also, I keep getting a "Error undefined variable in expression: _building" on line 50, whenever my player leaves by 250m, but thought I defined it?

 

Any ideas how to solve these 2 problems?

 

thanks

 

onMapSingleClick "player setPosATL _pos";
_civilianCount = 0;
_nearbyLocations = [];
_buildingArray = [];
_realBuildingArray = [];
while {true} do {
	sleep 3;
	waitUntil {		
		// find "Building" near player to spawn civilians
		_buildingArray = nearestObjects [player, ["House"], 250];
		{
			if ([_x, 3] call  ) then {_realBuildingArray pushBackUnique _x};
		} forEach _buildingArray;
		
		// player is near a location
		_building = objNull;
		if !(_realBuildingArray isEqualTo []) then { 		
			_realBuildingArray = nearestObjects [player, ["House"], 250];
			_building = _realBuildingArray select ((floor random (count _buildingArray)) + 1);
			systemChat format ["_building: %1", typeOf _building];
		
			// create groups of civilians
			_grp = createGroup civilian;		
			for "_i" from 1 to floor random 6 do {
				_unit = _grp createUnit ["C_scientist_F", _building, [], 0, "NONE"];				
				_wp =_grp addWaypoint [position player, 10];
				_wp setWaypointType "MOVE";
				_wp setWaypointSpeed "LIMITED";

				// spawn function for groups to keep moving to player then return then get deleted
				[_unit, _grp, _wp, _realBuildingArray] spawn {
					params ["_unit", "_grp", "_wp", "_realBuildingArray"];
					waitUntil {
						sleep 3;
						// new waypoint for player that moves
						if ((player distance2D waypointPosition _wp) > 5) then {
							_wp =_grp addWaypoint [position player, 5];
							_grp setCurrentWaypoint _wp; 
						};
						( ((_unit distance2D player) < 10) or ((player distance2D _unit) > 250) ) 
					};
					
					// select building to return to
					_building = objNull;
					if !(_realBuildingArray isEqualTo []) then {
						_building = _realBuildingArray select ((floor random (count _realBuildingArray)) + 1);
						_wp =_grp addWaypoint [_building, 0];
						_grp setCurrentWaypoint _wp; 		
					};
					// return to building and get deleted
					waitUntil {
						sleep 3;
						(((_unit distance2D waypointPosition _wp) < 2) or ((player distance2D _unit) > 250) )
					};
					deleteVehicle _unit; 
				};				
			};	
			sleep floor random 5;	
		};
	};
};

 

Share this post


Link to post
Share on other sites

Hiho, 

 

It did not find a building to create the information for_building i assume. 

You need to add an option that exits the code if no building is nearby. 

 

 // find "Building" near player to spawn civilians

  _buildingArray = nearestObjects [player, ["House"], 250];

//insert below:

If (_buildingArray isequalto []) exithwith {} ;

 

You could also work with a loop here, that keeps checking ubtil there is a building in range. 

 

Cheers

Vd

 

 

  • Like 1

Share this post


Link to post
Share on other sites

Maybe not your question, but this is a solid easy to use "spawn units in building script" that I always use:  

 

  • Like 1

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

×