Jump to content
Sign in to follow this  
Charles Darwin

Randomly Select Unit in Trigger

Recommended Posts

Ok so trying to randomly select a player unit inside a trigger and execute a small bit of code.

in the init:

[] execVM "spy.sqf";

spy.sqf:

if (isserver) then{
_triglist = list triggerone;
_count = count _triglist;
_num = round(random _count);
_spy = _triglist select _num;
};
hint format["%1 is a spy!", _spy];
_spy setvehiclevarName "spy";

code to be executed:

if("spy" == vehiclevarName player)then{bugaction = CO addAction ["Bug Commander", "bug.sqf"]; ["You are the Spy!"] spawn BIS_fnc_infoText; sleep 7;["Plant the GPS Bug","on your Commander!"] spawn BIS_fnc_infoText;};
_index = spy addMPEventHandler ["mprespawn", {bugaction = CO addAction ["Bug Commander", "bug.sqf"];}]; 

but it doesnt seem to find anyone in the trigger area as it always says null or any is spy

Share this post


Link to post
Share on other sites

Have you checked to see what's in _triglist ?

If _triglist is any then try initializing it before the if statement with _triglist = [];

Share this post


Link to post
Share on other sites

triglist should be any opfor unit in triggerone, thats the problem

---------- Post added at 08:27 PM ---------- Previous post was at 08:26 PM ----------

Here's what you can use to pick one unitrandomly from an array:

_spy = *array containing players* call BIS_fnc_selectRandom;

http://community.bistudio.com/wiki/BIS_fnc_selectRandom

Make sure you have a Functions module.

tried that said it didnt work for objects

Share this post


Link to post
Share on other sites

Works just fine for objects.

Place Functions module. Put down 3 BLUFOR units. Named them p1, p2 and p3. Put down a BLUFOR PRESENT ONCE trigger with this as onAct:

spy = thisList call BIS_fnc_selectRandom; hint format["The Spy is: %1", spy];

Each time I start up the mission I get a random Spy object.

Share this post


Link to post
Share on other sites

My bad. Disregard this post..

Try this....

In the triggers init...

[s]nul = [thislist] execVM "spy.sqf";[/s]

...and for spy.sqf....

[s]if (isserver) then	{
_triglist = _this;
_count = count _triglist;
_num = floor (random _count);
_spy = _triglist select _num;
};

hint format["%1 is a spy!", _spy];

_spy setvehiclevarName "spy";[/s]

Tested and working fine. Oops! Not working.

Edited by twirly
Clarity...and fixed an error

Share this post


Link to post
Share on other sites
Works just fine for objects.

Place Functions module. Put down 3 BLUFOR units. Named them p1, p2 and p3. Put down a BLUFOR PRESENT ONCE trigger with this as onAct:

spy = thisList call BIS_fnc_selectRandom; hint format["The Spy is: %1", spy];

Each time I start up the mission I get a random Spy object.

ah i wasnt naming them before that could be problem..is there way to get the player's name..ie not the variable name but the player's profile name when the unit has a variable name?

Id like to west team to know which enemy team member is the spy so right now it says

"p1 is a spy!"

id like it to say "Charles Darwin is a spy!"

Share this post


Link to post
Share on other sites

Works without naming them, but then you get "The Spy is B 1-1-A:3" or something. To give the player name just use name spy at the end instead of just spy.

Share this post


Link to post
Share on other sites

awesome seems to be working perfectly now! thanks guys not sure why the bis function didnt work the first time, but it does now!

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  

×