Jump to content
Sign in to follow this  
wiggum2

Spawn only objects in list ?

Recommended Posts

Hi,

is there a way to spawn only the objects in a list, and if all objects are spawned once then the spawn stops ?

something like that:

_objects = ["obj1","obj2","obj3"];

And then:

_count = count _objects;
_n = _count-1;

if (_chance >= (random 1) and spawn) then 
{	
_objecttype = (_objects select (round(random _n)));
_object = _objecttype createVehicle _pos;

[/code]

The problem is that only obj1, obj2 and obj3 (editor objects like a table) should randomly spawn. And if all three are spawned once then the hole spawn-system should stop...so no more of them will spawn.

I hope you understand what i mean.

Any ideas ?

Thanks

Share this post


Link to post
Share on other sites

For random selection, always use floor rather than round. Random 1 can return 0 but never 1. Also what are "obj1" through 3? You should refer it's type using typeOf rather than it's object reference. What is spawn? Your own boolean value? Change it's name as spawn is an actual command.

Share this post


Link to post
Share on other sites

Do you mean each spawned once? If so remove the item from the array:

_items set [_index,"DUMMY"];
_items = _items - ["DUMMY"];

Or do you mean each spawned at least once? If so, cache the already used types:

if (!(_item in _cache)) then
{
_cache set [count _cache,_item];
//do stuff
};

and exit if the _cache has the size of the _items

if (count _cache) == (count _item) exitWith {};

Share this post


Link to post
Share on other sites

@ CarlGustaffa

"spawn" was just a variable i forget to take out.

@ PvPscene

Thats pretty much what i want.

There should be a random chance that one of the 3 objects spawns.

And if all 3 (or 2 or maybe 1) is spawned the spawn script stops.

So:

_pos=this select 0;
_chance=this select 1;

if (count _cache) == (count _item) exitWith {}; 

_items set [_index,"obj1,obj2"];
_items = _items - ["obj1,obj2"]; 

if (!(_item in _cache)) then
{
_cache set [count _cache,_item];
if (_chance >= (floor 1)) then 
{	
_object = _item createVehicle _pos;
};
}; 

?

Share this post


Link to post
Share on other sites

And if all 3 (or 2 or maybe 1) is spawned the spawn script stops.

IMO you'd need a loop around the spawn code which is looped randomly from 1 to X times (x = maximum number of array entries)

_pos=this select 0;
_chance=this select 1;
_objects = ["obj1","obj2","obj3"];


_cache = _objects;

_maxloops = floor (random count _cache);
for "_i" from 0 to _maxloops do
{
   if (_chance >= (random 1) and spawn) then 
   {	
       if (count _cache) == 0 exitWith {}; 

       _objecttype = (_cache select (floor (random count _cache)));
       _objectindex = _cache find _objecttype;
       _cache set [_objectindex,"DUMMY"];
       _cache = _cache - ["DUMMY"]; 

_object = _objecttype createVehicle _pos;
   };
};

Are you sure you want this?

You could have any combination of Obj1 ,Obj2 and/or Obj3 spawned and all of them at the same position.

And about your spawn variable - what is this, some global flag you set as true or false in order to stop all spawns anytime?

Edited by ])rStrangelove

Share this post


Link to post
Share on other sites
)rStrangelove;1992620']

Are you sure you want this?

You could have any combination of Obj1 ' date='Obj2 and/or Obj3 spawned and all of them at the same position.[/quote']

No, i want to be able to set the maximum number of objects that can spawn.

So with:

_objects = ["obj1","obj2","obj3"];

I mean that randomly one of those objects will spawn, maybe obj1.

Now only obj2 and 3 are left, randomly only these to can spawn now.

So if obj3 spawns next there is only obj2 left and after that no more spawn.

All objects spawned once and thats it.

)rStrangelove;1992620']

And about your spawn variable - what is this' date=' some global flag you set as [u']true or false[/u] in order to stop all spawns anytime?

A global flag i set true or false but maybe i will not need it anyway.

Share this post


Link to post
Share on other sites

I mean that randomly one of those objects will spawn, maybe obj1.

Now only obj2 and 3 are left, randomly only these to can spawn now.

So if obj3 spawns next there is only obj2 left and after that no more spawn.

All objects spawned once and thats it.

Yeah, thats what they (should) do in my version.

You could randomly end up with:

- obj1 is spawned alone

- obj2 is spawned alone

- obj3 is spawned alone

- obj1 and obj2 are spawned only

- obj1 and obj3 are spawned only

- obj2 and obj3 are spawned only

- all are spawned

Share this post


Link to post
Share on other sites

He wants this:

1, 2 or 3 are available - 2 spawns.

1 or 3 is available - 1 spawns.

3 spawns since it's all that's available.

Share this post


Link to post
Share on other sites

@OP, you do realize that if you use chance with a list that will continue to spawn until all is spawned, there really is only 100% chance anyway...

just use this to spawn random selection and remove once spawned from array/list.

_objectToSpawn = _objects select (floor(random (count _objects)));
_objects = _objects - [_objectToSpawn];

Share this post


Link to post
Share on other sites

init.sqf:

objects = ["Land_stand_small_EP1","Land_stand_meat_EP1","Land_Crates_EP1"];

trigger:

0 = ["mrkObjSpawn"] execVM "randomSpawn.sqf";

randomSpawn.sqf:

// Objects list - fruit stand, a meat stand or some blue crates.  Global since we'll reuse it
// set in init.sqf:
// objects = ["Land_stand_small_EP1","Land_stand_meat_EP1","Land_Crates_EP1"];

// where to spawn it - string marker name
_spawnPoint = _this select 0;

// Spawn item.
_item = objects select floor(random count objects); 
_obj = _item createVehicle getMarkerPos _spawnPoint;

// Remove item from the list.
objects = objects - [_item];

hint format["Spawning %1 and %2 are left!", getText (configFile >> "cfgVehicles" >> _item >> "displayName"), objects];

Edited by kylania
bah, ninja'd.

Share this post


Link to post
Share on other sites

Thanks guys, will try this as soon as possible ! :)

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  

×