Jump to content
Sign in to follow this  
sasij

Random loot spawning woes.

Recommended Posts

Trying to get random loot to spawn in buildings at a random position in the building. The script works in the form of spawning in the loot just fine when I don't have it set to pick a random spot in the building. Hard to explain so I'll just show my simplistic code and hope somebody can give em a good slap in the face at whatever obvious mistake I'm making. If I uncomment the setPos line which sets position according to _i which is just 0,1,2,3 etc the code works fine and will spawn items in the first 4 spots on the building. If I try using the setPos using a random number from the building positions count it doesn't work properly. I will run into buildings and sometimes see a loot pile. Most of the time I see none then I change the one line and see all my loot piles. Sorry for babbling, to the code then..

_buildings    = [List of building classes...];
_loot_weapons = [List of gun classes...];
_loot_ammo    = [List of ammo classes...];
_posCount   = 0;
_max 	    = 4;
_total_loot = 0;

_nearObjects = nearestObjects [[10000,10000,0], _buildings, 20000];


{
while {format ["%1", _x buildingPos _posCount] != "[0,0,0]"} do 
{
	_posCount = _posCount + 1
};

_i = 0;
while {_i<_max} do
{
	_pos           = floor(random _posCount);
	_rand_w        = floor(random (count _loot_weapons-1));
	_rand_a        = floor(random (count _loot_ammo-1));
	_gun_loot      = _loot_weapons select _rand_w;
	_ammo_loot     = _loot_ammo    select _rand_a;
	_spawnedObject = createVehicle ["GroundWeaponHolder", [0,0,0], [], 0, "CAN_COLLIDE"];
	_spawnedObject addWeaponCargoGlobal [_gun_loot,1];
	_spawnedObject addMagazineCargoGlobal [_ammo_loot, 3];
	//_spawnedObject setPos (_x buildingPos _i);
	_spawnedObject setPos (_x buildingPos _pos);
	_total_loot    = _total_loot+1;

	_i = _i+1;
};

sleep 0.1;
}forEach _nearObjects;

hintC format["Spawned %1 Loot Piles.", _total_loot];

---------- Post added at 01:30 ---------- Previous post was at 23:38 ----------

Oops I forgot to reset _posCount to 0 each iteration of the array. Well I guess that solves that.

Share this post


Link to post
Share on other sites

There's a semicolon missing btw:

while {format ["%1", _x buildingPos _posCount] != "[0,0,0]"} do 
{
	_posCount = _posCount + 1
};

Share this post


Link to post
Share on other sites
There's a semicolon missing btw:

while {format ["%1", _x buildingPos _posCount] != "[0,0,0]"} do 
{
	_posCount = _posCount + 1
};

You wouldnt need a semicolon there.

Share this post


Link to post
Share on other sites

Hi!

Very nice script sasij.

I'm not overly familiar with arma scripting but trying to learn some just to create some missions for myself.

I used the script to spawn in stuff with a trigger when player gets near some building, but how would one despawn the stuff after the player leaves the area?

Any help appreciated.

Share this post


Link to post
Share on other sites

Ok after about ten hours I got it working. Just for whoever is intrested.

I execute the script with trigger. To the triggers OnAct. field i write _null = [trigger1] execVM "lootspawn.sqf";

trigger1 is the name of the trigger. I use the name to clear the area of spawned loot when player leaves the area. Basicalli just added the code:

_mytrigger = _this select 0;

waitUntil { ( player distance _mytrigger ) > 30};

_objektid = nearestObjects [ _mytrigger , ["GroundWeaponHolder"], 50 ];
{deletevehicle _x} forEach ( _objektid );

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  

×