Author Topic: Multiplayer Sexps  (Read 12676 times)

0 Members and 1 Guest are viewing this topic.

Offline karajorma

  • King Louie - Jungle VIP
  • Administrator
  • 214
    • Karajorma's Freespace FAQ
I got a request from CooperHawkes for an explanation of how you fix a SEXP so that it works in multiplayer so I figured I'd just post the explanation so that the other SCP coders would see how easy it is to fix them.

Firstly you must determine if the SEXP is suitable for fixing. Only the Change SEXPs work with this method. Not every change SEXP needs a fix, only those where the effect is only seen on the server but fails to make it to the client machines.

Fixing a SEXP involves using the Multi_SEXP packet system in order to send the information to the client machines. I coded this in such a way that the coder doesn't need to understand how this works though. All they have to do is call a set of send_x functions on the server and a set of get_x functions on the client. To show how this works I'll convert fade-in to a multi capable SEXP as an example.

Code: [Select]
void sexp_fade_in(int n)
{
float delta_time = 0.0f;

if(n != -1)
delta_time = eval_num(n)/1000.0f;

if(delta_time > 0.0f)
{
Fade_delta_time = delta_time;
Fade_type = FI_FADEIN;
}
else
{
Fade_type = FI_NONE;
gr_create_shader(&Viewer_shader, 0, 0, 0, 0);
}
}

Here's how the SEXP currently looks. From that you can see that the only variable the client machine needs to be aware of is delta_time.

So before the final brace we add the code for the multi call back.

Code: [Select]
// multiplayer callback
multi_start_packet();
multi_send_float(delta_time);
multi_end_packet();
}

The first and last functions say that this is the start and end of a packet of info you want sent to the client machines. Between these you want to include any variables that you need to send to the client machines (including any optional ones the SEXP may have).

And that's it as far as the server is concerned.

Client side you need to add a function that is called when this packet arrives.

Code: [Select]
void multi_sexp_fade_in()
{
float delta_time = 0.0f;

multi_get_float(delta_time);

if(delta_time > 0.0f) {
Fade_delta_time = delta_time;
Fade_type = FI_FADEIN;
}
else {
Fade_type = FI_NONE;
gr_create_shader(&Viewer_shader, 0, 0, 0, 0);
}
}

The majority of the function is a simple copy of the actions carried out in on the host side (for longer SEXPs you should spin that off into a function and call it from both the host and client functions but this one was simple enough to justify a cut and paste instead. :p)

The function starts with a list of calls to the multi_get_x functions. When dealing with optional arguments you can simply test for
Code: [Select]
if (multi_get_x) as this will return false if the optional argument wasn't present.

The final step is to find void multi_sexp_eval() and add a case for the this particular SEXP pointing back to the function to call (in this case multi_sexp_fade_in() ).

Code: [Select]
case OP_CUTSCENES_FADE_IN:
multi_sexp_fade_in();
break;

And that's it. The code handles everything else internally. So now none of you have any excuse for making SEXPs that don't work in multiplayer. :p
« Last Edit: October 27, 2012, 04:07:55 am by The E »
Karajorma's Freespace FAQ. It's almost like asking me yourself.

[ Diaspora ] - [ Seeds Of Rebellion ] - [ Mind Games ]