Jump to content
Sign in to follow this  
dupa

createVehicle from a client in MP

Recommended Posts

I have a script that allows players to spawn vehicles by using an action menu (it's actually dialogs, but for the simplicity let's assume that it's an action menu). Since the action menu runs the spawn script locally, the createVehicle command is issued on the client.

Now to the problem:

In SP - all works well. When I host a game - all works well for me, but the client connecting to me is unable to spawn the vehicle - he clicks the action menu, but nothing seems to happen. The interesting thing is that any variables assigned to the vehicle that should have been spawned by using setVariable may be retrieved back with the correct values. I tried using publicVariable on the vehicle, but it did not change much.

Can anyone tell me what is the correct way of doing this? Thanks.

Share this post


Link to post
Share on other sites

Im not very knowledgeable on this but i believe its working for you because you are both client and server, give the action to the player but createVehicle on the server.

Someone correct me if im wrong..

Share this post


Link to post
Share on other sites

Creating objects/vehicles on the client side creates desync/lag and shall be avoided whenever possible. It should work though, as createvehicle has global effect. I don't know what's going wrong with your current code, without having a look at it. You might want to post all the code that is run after the client activates that action.

But let's get back to how to do it properly:

Creating vehicles should be done by the server, to achieve that the client has to tell the server that he wants to have a vehicle created. Send it a message using publicVariable

pvRequestVehicle = []; // put all info you want to send in this array
publicVariable "pvRequestVehicle";

On the server you need a publicVariable-eventhandler listening to that very global variable (same name) the client is publishing, to register the request of the client and process the request

"pvRequestVehicle" addpublicvariableeventhandler {

[indent]_info = _this select 1;   // the array with the info the client has sent
// code that creates the vehicle[/indent]


};

You said you're also setting variables in the vehicles variable space. When those variables shall set for all clients then run setVariable on the server with the boolean flag to broadcast it to all connected machines.

When only the client that sent the request shall have the variables in the namespace set, then you the same thing as above, but this time the other way around. The server sends out a message to the clients, the clients listen to this message (use a different variable name than the one above for this). To run the code only on the client that issued the request you need to send information with the request, and the server sends it back, that identifies that client unambiguous. For that you can use the players unit, or generate a random id.

EDIT:

Note: When you publish a variable and you have a pvEH running on the same machine listening to the very same variable, then the pvEH won't trigger (source). This can happen in a SP game and in MP with a client-host as server. Therefore you need to check whether the current maching is the server before sending out the request message:

if(isServer) then {
[indent]// run the code that the pvEH on the server would run in a mp game[/indent]


} else {

[indent]pvRequestVehicle = []; // put all info you want to send in this array
publicVariable "pvRequestVehicle";[/indent]


};

Edited by dengibtsschon
Added a important note to publicvariable and pvEHs

Share this post


Link to post
Share on other sites

Thanks for the info, I will give it a try when I get home.

Regarding public variable event handlers:

A quote from the wiki:

Anunnaki

Very important ! addPublicVariableEventHandler does NOT WORK on dedicated server or player server! This is probaly bug in 1.09beta.

Dennis

the event handler is not executed on JIP players machines

Has this been fixed? I never got to use addPublicVariableEventHandler.

Edited by dupa

Share this post


Link to post
Share on other sites

Well I finally solved it.... The object that I was using as position reference for createVehicle was local to the server, so on the client the vehicle was spawned at [0,0,0], because getPos _obj returned null. publicVariable on that object solved the problem. MP locality can sometimes be a PITA.

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  

×