Jump to content
Sign in to follow this  
Rhaonoa

Need help with; Random selection from List of choices.

Recommended Posts

Hello once again; I've come across another thing that I was wondering how could I do.
I'm working in a mission currently; and there's a loadout that you always spawn with, right? - I kind of don't like that I'm or the people playing would always spawn with the same uniform everytime they spawn/respawn; so I was trying to use "BIS_fnc_selectRandom" but I couldn't really understand how to apply it.

Here I'm using a loadout script (Kill_Phil's; https://forums.bistudio.com/topic/142113-respawn-with-custom-loadout/),although, everything works fine and all; but I wanted to know or get some help towards how to make this work making it choose a random uniform off of a list; and maybe same for Headgear.

here's the script;

loadout.sqf (All credit to Kill_Phil for the "template".)

waitUntil {!isNull player};      

 
_unit = _this select 0;
removeallweapons _unit;
removeallassigneditems _unit;
removeallcontainers _unit;
removeHeadgear _unit;
removeGoggles _unit;
 
_unit forceaddUniform "U_BG_Guerilla2_3";
_unit addmagazines ["12Rnd_mas_45acp_Mag", 1]; 
_unit addmagazines ["12Rnd_mas_45acp_Mag", 1]; 
_unit addweapon "hgun_mas_acp_F"; 
_unit addHandgunItem "acc_mas_flash_gun";
_unit addItem "Ace_CableTie";
_unit addItem "Ace_CableTie";
_unit addItem "Ace_CableTie";
_unit addItem "ACE_quikclot";
_unit addItem "ACE_elasticBandage";
_unit addItem "ACE_elasticBandage";
_unit addItem "ACE_morphine";
_unit addItem "ACE_epinephrine";
*/
 
if(true) exitWith{};



So as you may see, there's a highlighted command; that's the command that forces the uniform on the unit (since the units we're talking about are from a different faction.)
I'm struggling to figure out how could I extend this list, for example; ["U_BG_Guerrilla2_3","Xitem1","Xitem2"] and make it randomly choose one of these for the unit (*player*) that spawns/respawns.

Once again, thanks in advance.

Share this post


Link to post
Share on other sites

Hi kyootlaer

 

Try this:

_uniforms = [ "U_BG_Guerrilla2_3","Xitem1","Xitem2"];
player forceAddUniform (_uniforms select  (floor (random (count _uniforms)))); 
  • Like 1

Share this post


Link to post
Share on other sites

 

Hi kyootlaer

 

Try this:

_uniforms = [ "U_BG_Guerrilla2_3","Xitem1","Xitem2"];
player forceAddUniform (_uniforms select  (floor (random (count _uniforms)))); 

 It's perfect  :) .

Thanks you very much.

Share this post


Link to post
Share on other sites

This is what I used to use

 

   missions        = ["killcivmission","rescuemission", "capturemission];
    _missionToSpawn = missions call BIS_fnc_selectRandom;

 

It's a BIS function so should ... be faster
 

  • Like 1

Share this post


Link to post
Share on other sites

It's a BIS function so should ... be faster

This is the function:

if (count _this > 0) then {
    _this select (floor random (count _this));
} else {
    ["Array has no elements."] call bis_fnc_error;
    nil
};

So probably not any faster. :)

In fact, in some cases the BIS functions are unnecessarily complex so it may be more efficient to write your own for your specific situation.

  • Like 2

Share this post


Link to post
Share on other sites

This is the function:

if (count _this > 0) then {
    _this select (floor random (count _this));
} else {
    ["Array has no elements."] call bis_fnc_error;
    nil
};

So probably not any faster. :)

Idd prob not

Share this post


Link to post
Share on other sites

Well, after some testing in multiplayer and etc, etc; It seems to work just fine. 
It seems to sometimes spawn players other than the host naked for some reason though.

On a side topic, which is still about selecting from random choices, how could I get this to work with vehicles being randomly selected from a list of choices?

zlt_civveh.sqf

// v1d Civilian Vehicles from [sTELS]Zealot

if (not isserver) exitwith {};
 
zlt_civveh_round = 0;
zlt_civveh_vehicles = [];
zlt_civveh_spawnedloc = [];
 
zlt_rnd_seed = 3;
 
zlt_fnc_random = {
  private ["_a","_c","_m"];
  _a = 75;
  _c = 0;
  _m = 65537;
  zlt_rnd_seed = ( zlt_rnd_seed * _a + _c ) mod (_m);
  (zlt_rnd_seed / _m) ;
};
 
 
zlt_fnc_selectrandom = {
_this select floor ( ([] call zlt_fnc_random) * count (_this));
};
 
zlt_fnc_civvehs = {
_debug = [_this, 1, false] call bis_fnc_param;
_lrange = _this select 2;
_cars = _this select 3;
_t1 = diag_ticktime;
 
 
_startpos= _this select 0;
_houses = _startpos nearobjects ["House",_lrange];
//_vehmax =  _cars;
_vehmax = (round ((sqrt (count _houses)) * 0.5)) max 1;
 
_classes = ["C_Offroad_01_F"];
 
_vehs = 0;
_attemps = 0;
 
_class = _classes select 0;
zlt_rnd_seed =  ((_startpos select 0) + (_startpos select 1) mod 65537);
while {_vehs < _vehmax and _attemps < 10} do {
//diag_log ["CV",_startpos, _lrange, _houses];
 
_house = _houses call zlt_fnc_selectrandom;
 
_housepos = _house modeltoworld [0,0,0];
_newpos = _housepos findEmptyPosition [ 3 , 15, _class ];
_houses = _houses - [_house];
if (count _newpos == 0) then {
_attemps = _attemps + 1;
} else {
_veh = _class createVehicle (_newpos ) ;
_veh setvariable ["zlt_civveh", true];
zlt_civveh_vehicles set [count zlt_civveh_vehicles, _veh];
 
_direction = (random 360);
_nearRoads = _newpos nearRoads 10;
      
if(count _nearRoads > 0) then
{
_road = _nearRoads select 0;
_roadConnectedTo = roadsConnectedTo _road;
if (count _roadConnectedTo > 0) then {
_connectedRoad = _roadConnectedTo select 0;
_direction = [_road, _connectedRoad] call BIS_fnc_DirTo;
};
}; 
 
_veh setdir _direction;
_attemps = 0;
_vehs = _vehs + 1;
if (_debug) then {
["crt"+str(_newpos), _newpos] call zlt_fnc_createmarkerlocal;
};
};
};
_t2 = diag_ticktime;
if (_debug) then {
diag_log str ["civeh.sqf", _t2-_t1];
};
};
 
 
zlt_civ_checkloop = {
 
 
zlt_civveh_notemptyloc = [];
 
{
_veh = vehicle _x;
_isInVeh = (vehicle _x != _x);
 
 
// or _veh getvariable ["zlt_civ_round", -1] != zlt_civveh_round
if (!_isInVeh || {_veh isKindOf "LandVehicle"}) then {
_dist = 1000;
_locations = nearestLocations [(getpos vehicle _x), ["NameCityCapital","NameCity","NameVillage"], _dist];
_locations2 = nearestLocations [(getpos vehicle _x), ["NameCityCapital","NameCity","NameVillage"], 2500];
{
if ( not (_x in zlt_civveh_spawnedloc) ) then {
_lrange = 50; _cars = 1;
switch (type _x) do {
case ("NameCityCapital") : {_lrange = 250; _cars = 7;};
case ("NameCity") : {_lrange = 150; _cars = 3;};
};
_pos = position _x;
[ [_pos select 0, _pos select 1, 0], false, _lrange, _cars] call zlt_fnc_civvehs;
zlt_civveh_spawnedloc pushBack _x; 
// _x setside east;
};
} foreach _locations;
{
zlt_civveh_notemptyloc pushBack _x;
} foreach _locations2;
if (_isInVeh) then { _veh setvariable ["zlt_civ_round", zlt_civveh_round];};
};
} foreach (if (player in playableunits) then {playableunits} else {[player]});
 
 
{
_pos = [(position _x) select 0, (position _x) select 1, 0];
_ent = ( (_pos) nearEntities ["C_Offroad_01_F", 300] );
{
if ( _x getvariable ["zlt_civveh", false] and {count crew _x == 0 and fuel _x == 1}) then {
deletevehicle _x;
};
} foreach _ent;
zlt_civveh_spawnedloc = zlt_civveh_spawnedloc - [_x];
} foreach (zlt_civveh_spawnedloc-zlt_civveh_notemptyloc);
 
zlt_civveh_round = zlt_civveh_round + 1;
};
 
while {true} do {
//scan loop
 
sleep 3.4; //magic number
[] call zlt_civ_checkloop;
 
};

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  

×