Jump to content
Sign in to follow this  
Lucky44

AddAction trouble for dedicated server mission

Recommended Posts

I appreciate any help!

I'm trying to randomize which 6 of 20 "intel objects" (e.g., files, photos, laptops) in a building need to be gathered by the players in this co-op mission. My method is to place each object on the map in the editor and give it a name. Then I'm running a script that picks a first object randomly and adds the "Take this intel for examination later" action to it. Then it continues on to pick 5 more objects (that haven't been picked yet.)

I originally ran the script that does this from an execVM from the init.sqf. Problem was that it effectively ran once for each client connected, apparently. So I put a "if (!isServer) exitwith {}" at the top of the script. But that made it only run on the dedicated server, and not on any clients. I know there's a way to accomplish this, but I'm stuck trying to find it!

Here's the code, if it helps.

if (!isServer) exitWith {}; // This is an addition for version 0.4

sleep 9;

_intel1 = [i0,i1,i2,i3,i4,i5,i6,i7,i8,i9,i10,i11,i12,i13,i14,i15,i16,i17,i18,i19]; //array of intel items

//initialize the picked variables (with obviously weird values!)
_r1=-2; _r2=-2; _r3=-2; _r4=-2; _r5=-2; _r6=-2;


//randomly choose 6 intel pieces to activate from the 20 possible/////////////////////////////////////////////////////
//1st object: ///////////////////////////////////////////////////////////
_r1 = floor(random 20); // set value for first pick
(_intel1 select _r1) addAction ["Take this intel for later examination","scripts\takeIntel.sqf"];

// 2nd roll: start _r2 //////////////////////////////////////////////////
_r2 = floor(random 20);
while {_r2==_r1} do
{
  _r2 = floor(random 20); // reroll
};
(_intel1 select _r2) addAction ["Take this intel for later examination","scripts\takeIntel.sqf"];

// 3rd roll: start _r3 /////////////////////////////////////////////////
_r3 = floor(random 20);
while {(_r3==_r1) or (_r3==_r2)} do
{
  _r3 = floor(random 20);  // reroll
};
(_intel1 select _r3) addAction ["Take this intel for later examination","scripts\takeIntel.sqf"];

// 4th roll: start _r4 /////////////////////////////////////////////////
_r4 = floor(random 20);
while {(_r4==_r1) or (_r4==_r2) or (_r4==_r3)} do
{
  _r4 = floor(random 20);  // reroll
};
(_intel1 select _r4) addAction ["Take this intel for later examination","scripts\takeIntel.sqf"];

// 5th roll: start _r5 /////////////////////////////////////////////////
_r5 = floor(random 20);
while {(_r5==_r1) or (_r5==_r2) or (_r5==_r3) or (_r5==_r4)} do
{
  _r5 = floor(random 20);  // reroll
};
(_intel1 select _r5) addAction ["Take this intel for later examination","scripts\takeIntel.sqf"];

// 6th roll: start _r6 /////////////////////////////////////////////////
_r6 = floor(random 20);
while {(_r6==_r1) or (_r6==_r2) or (_r6==_r3) or (_r6==_r4) or (_r6==_r5)} do
{
  _r6 = floor(random 20);  // reroll
};
(_intel1 select _r6) addAction ["Take this intel for later examination","scripts\takeIntel.sqf"];

Share this post


Link to post
Share on other sites

That's some weird while-loops.

Place the functions module on the map and then try this code.

if (!isServer) exitWith {};
sleep 9;

_intel1 = [i0,i1,i2,i3,i4,i5,i6,i7,i8,i9,i10,i11,i12,i13,i14,i15,i16,i17,i18,i19]; //array of intel items

for "_i" from 0 to 6 do 
{
 _int = _intel1 call BIS_fnc_selectRandom;
 _intel1 = _intel1 - [_int]; // delete the intel from the array so we don't select it again
 _int addAction ["Take this intel for later examination","scripts\takeIntel.sqf"]; // add action locally
 [nil,_int,"per",rAddAction,"Take this intel for later examination","scripts\takeIntel.sqf"] call RE; // add action globally
};

Edit: I'm uncertain if remote execution adds it locally, so if you get two actions on one object just remove the first addaction in the code

Edited by cuel

Share this post


Link to post
Share on other sites

Here is solution without Function Module, though cuel's solution might work as well

//Add code that executes when publicVar_intelList gets broadcasted
"publicVar_intelList" addPublicVariableEventHandler {
//Go through list of intel and add action to each
{
	_x addAction ["Take this intel for later examination","scripts\takeIntel.sqf"];
} forEach publicVar_intelList;
};

if (!isServer) exitWith {}; // This is an addition for version 0.4

sleep 9;

_intel1 = [i0,i1,i2,i3,i4,i5,i6,i7,i8,i9,i10,i11,i12,i13,i14,i15,i16,i17,i18,i19]; //array of intel items

//List of intel to broadcast to other players
publicVar_intelList = [];

//Loop of 6 iterations (0,1,2,3,4,5)
for "_i" from 0 to 5 do {
//Select random intel from array
_int = _intel select floor(random(count _intel1));

//Delete selected intel from array so it wouldn't get picked twice
_intel1 = _intel1 - [_int];

//Add selected intel into public variable for broadcasting it
publicVar_intelList = publicVar_intelList + [_int];
};

//Broadcast variable with list of intel to other players
publicVariable "publicVar_intelList";

Since I didn't test the code, there are few notes about it:

- It might not add actions on server (only on clients)

- It might not add actions for join in progress players

If any of above is correct, then i'll explain how to fix it.

Share this post


Link to post
Share on other sites

Thanks very much to both of you for taking the time to help out. I'll try Cuel's approach first and see how that goes.

I didn't know about BIS_fnc_selectRandom ! How great is that!! Thanks.

Share this post


Link to post
Share on other sites

OK, quick question: why add the actions both locally and globally? Why not just add them globally?

Share this post


Link to post
Share on other sites

As I said, I'm not sure if the remote execution runs on the computer that does it. Just like publicVariableEventHandlers (which I think remote execution uses?)

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  

×