Jump to content
Sign in to follow this  
SpecOp9

Assigning a script to a random player?

Recommended Posts

Would this be possible? I want the script to read a list of playable characters, pick a random player from the list, and then execute a script (giving him abilities) that only that player has.

I really need to learn scripting because I hate asking these questions.

Share this post


Link to post
Share on other sites

You learn by asking... nothing wrong with it :)

Select random player

_playerList = if (isMuliplayer) then { playableUnits; } else { switchableUnits; }; // For testing in editor mostly
_randomPlayer = _playerList call BIS_fnc_selectRandom;

_null = [_randomPlayer] execVM "thescript.sqf";

thescript.sqf

_player = _this select 0;

if (!(local _player)) exitWith {}; // Just in case


... the rest goes here ...


Edit:

That example was a bit too basic, I think... here is a bit more practical one:

Init.sqf

// Remote local script execution function
btk_fnc_MPexecVMLocal = {

private ["_player"];

_player = _this select 0;

if (local _player) then {
	_null = [] execVM "thescript.sqf";
};

};

// Let the server select a player
if (isServer) {
_playerList = if (isMuliplayer) then { playableUnits; } else { switchableUnits; }; // For testing in editor mostly
_randomPlayer = _playerList call BIS_fnc_selectRandom;

[[_randomPlayer], "btk_fnc_MPexecVMLocal"] call BIS_fnc_mp;
};

Edited by sxp2high

Share this post


Link to post
Share on other sites

Thanks sxp, I changed "thescript.sqf" to camera.sqf just for testing, is there anything else I need to change?

Much to my dismay I'm starting to be able to read code and somewhat understand what each line does.

Edited by SpecOp9

Share this post


Link to post
Share on other sites

Wrote that from the top of my head without testing, but... nope, should be good to go like that.

Share this post


Link to post
Share on other sites

I cut the mission down to 1 player and 1 playable just to narrow it down to a 50/50 chance of a camera.sqf, but it doesn't seem to wanna execute :(

Share this post


Link to post
Share on other sites

Oops, sorry, I forgot a then after isServer, and there was also a typo :D :D

Here you go:

// Remote local script execution function
btk_fnc_MPexecVMLocal = {

   private ["_player"];

   _player = _this select 0;

   if (local _player) then {
       _null = [] execVM "camera.sqf";
   };

};

// Let the server select a player
if (isServer) then {
   _playerList = if (isMultiplayer) then { playableUnits; } else { switchableUnits; }; // For testing in editor mostly
   _randomPlayer = _playerList call BIS_fnc_selectRandom;

   [[_randomPlayer], "btk_fnc_MPexecVMLocal"] call BIS_fnc_mp;
};

However, in the editor it will always execute because of the local check.

To better test it, or in general, you can change

if (local _player) then {

to

if (player == _player) then {

Edited by sxp2high

Share this post


Link to post
Share on other sites

Awesome, worked. I'll be sure to credit you in the mission credits!

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  

×