Jump to content
chemicalcosh

Spawning Units in Multiplayer / Dedicated Server

Recommended Posts

Hi guys I am trying to test out some multiplayer specific coding, and am a bit confused why the following is not working. Basically I am trying to:

 

1) Player goes to NPC and picks spawn action from scroll menu

2) Server spawns a unit close to the NPC, and assigns it to the group of the player who requested it

 

Am I correct in thinking that the server should spawn the unit, so everyone connected can then see the unit? If it is done just on the client only the client would see it correct? Or have I got this wrong and the server is spawning it but only the server can see it.....

 

Here is my code, it works perfectly fine if i test in the editor and the unit spawns as expected. When I run it on dedicated server I get nothing, no spawn no error on screen or in RPT.

 

Any help or pointers would be very much appreciated!

 

initPlayerLocal.sqf

NpcSmuggler addAction ["Add AI Smuggler","NpcSmuggle\Smuggle.sqf",nil,6]; 

NpcSmuggle\Smuggle.sqf

_group = group player;
[_group] remoteExec ['Srv_IIS_Start_Smuggle',2,FALSE];
 
Srv_IIS_Start_Smuggle = {
 private ["_group","_distance","_direction"];
 _group = _this select 0; 
 
 _distance = [1,20] call BIS_fnc_randomInt; 
 _direction = [0,359] call BIS_fnc_randomInt; 
 _randomSpawnPos = [NpcSmuggler, _distance, _direction] call BIS_fnc_relPos; 
 _smuggler = _group createUnit ["B_Soldier_F", _randomSpawnPos, [], 0, "FORM"];
};

Share this post


Link to post
Share on other sites

Hey chemicalcosh,

 

generally speaking you assumption is correct, however, the effect of createUnit is global, means, the units created with this command will be seen for every client. effects_global.gif

_group = group player;
[_group] remoteExec ['Srv_IIS_Start_Smuggle',2,FALSE];
 

Srv_IIS_Start_Smuggle = {
 private ["_group","_distance","_direction"];
 _group = _this select 0; 
 
 _distance = [1,20] call BIS_fnc_randomInt; 
 _direction = [0,359] call BIS_fnc_randomInt; 
 _randomSpawnPos = [NpcSmuggler, _distance, _direction] call BIS_fnc_relPos; 
 _smuggler = _group createUnit ["B_Soldier_F", _randomSpawnPos, [], 0, "FORM"];
};

This code won't work for two reasons. Firstly, you define the function after the remoteExec execution. At this point the function is unknown. Secondly, it could be that your function would need to be whitelisted in CfgFunctions before you can spawn is with remoteExec.

 


private _randomSpawnPos = NpcSmuggler getPos [(random 20) + 1,random 359];

private _smuggler = (group player) createUnit ["B_Soldier_F", _randomSpawnPos, [], 0, "FORM"];

Didn't test this, but in theory it should work. I also replaced BIS_fnc_relPos with the much faster equivalent getPos  and put the _distance and _direction into this line too.

Share this post


Link to post
Share on other sites

Once you group the AI unit to the players group, the unit becomes local to that players PC, so spawning on the dedicated will likely not make a lot of difference.

Addactions run locally on the machine that calls it.

 

Due to using 'player' in the script (there is no player object on a dedicated server) this will be why it appears there is nothing happening.

edit: as R3vo pointed out the order was wrong, missed that!! ^^  ^^

 

your smuggle.sqf could be simply

private ["_group","_distance","_direction"];
 _group = group (_this select 1);    //the addaction passes three variables to the script called. refer https://community.bistudio.com/wiki/addAction

 _distance = [1,20] call BIS_fnc_randomInt; 

 _direction = [0,359] call BIS_fnc_randomInt; 

 _randomSpawnPos = [NpcSmuggler, _distance, _direction] call BIS_fnc_relPos; 

 _smuggler = _group createUnit ["B_Soldier_F", _randomSpawnPos, [], 0, "FORM"];

Share this post


Link to post
Share on other sites

This code won't work for two reasons. Firstly, you define the function after the remoteExec execution. At this point the function is unknown. Secondly, it could be that your function would need to be whitelisted in CfgFunctions before you can spawn is with remoteExec.

 

 

Even more , function need to be global (local for everyone)if want to remotexec from one PC to another.

  • Like 1

Share this post


Link to post
Share on other sites

Wow thank you for such quick replies, very much appreciated!

 

Just so I can double check I have understood your replies correctly:

 

1) Because createUnit is already global, my messing around with remoteExec and server functions is pointless - createUnit being global already takes care of all that (propagation) for me?

2) Even if I did need to use remoteExec it wouldn't work anyway because I made syntax errors with function position and forgot to whitelist it in CfgFunctions (doh, I understand now.)

3) "Once you group the AI unit to the players group, the unit becomes local to that players PC" - I'm a bit confused what this means, I thought createUnit was global? 

 

I think I am going to go and re-read the wiki again :p

  • Like 1

Share this post


Link to post
Share on other sites

Ok well I have tested all these solutions on dedicated server now and all clients can see the spawned units fine, I used the solution provided by r3vo as I liked the compactness and the faster use of getPos. So thanks r3vo, and also to everyone else that replied.

 

Working code:

private _randomSpawnPos = NpcSmuggler getPos [(random 20) + 1,random 359];  
_smuggler = "B_Soldier_F" createUnit [_randomSpawnPos, (group player), "", 0.5];
  • Like 1

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

×