| Both sides previous revisionPrevious revisionNext revision | Previous revision |
| client_server_detection [2019/08/26 18:36] – korshun | client_server_detection [2019/09/05 20:54] (current) – [Writing robust multiplayer code] korshun |
|---|
| { | { |
| // Draw hud, spawn effects. | // Draw hud, spawn effects. |
| | } |
| | </code> |
| | |
| | You can also make sure that a serverside-only function is not called on the client, and a clientside-only function is not called on the server. |
| | |
| | <code> |
| | function ServersideOnlyFunction(void) |
| | { |
| | // writing if (IsClient()) would incorrectly display the warning in singleplayer. |
| | if (!IsServer()) |
| | printbold(s:"Error: ServersideOnlyFunction was called clientside"); |
| | |
| | ... |
| | } |
| | |
| | function ClientsideOnlyFunction(void) |
| | { |
| | // writing if (IsServer()) would incorrectly display the warning in singleplayer. |
| | if (!IsClient()) |
| | printbold(s:"Error: ClientsideOnlyFunction was called serverside"); |
| | |
| | ... |
| } | } |
| </code> | </code> |
| ''IsClient()'' simply checks if [[zdoom>ConsolePlayerNumber]] is not negative. | ''IsClient()'' simply checks if [[zdoom>ConsolePlayerNumber]] is not negative. |
| |
| ''IsServer()'' is more complicated. It returns true if [[zdoom>ConsolePlayerNumber]] is negative. But if it's not negative, it needs to distinguish between multiplayer emulation and real multiplayer. The function needs to return true in multiplayer emulation but false in real multiplayer. | ''IsServer()'' is more complicated. It returns true if [[zdoom>ConsolePlayerNumber]] is negative. But if it's not negative, it needs to distinguish between multiplayer emulation and real multiplayer. It checks if the multiplayer is real using [[zan>IsNetworkGame]]. But calling this function in ZDoom/GZDoom crashes the engine. Therefore, before calling ''IsNetworkGame'', it checks if the engine is Zandronum using [[source_port_detection|IsZandronum]]. |
| | |
| It checks if the multiplayer is real using [[zan>IsNetworkGame]]. But calling this function in ZDoom/GZDoom crashes the engine. Therefore, before calling ''IsNetworkGame'', it checks if the engine is Zandronum using [[source_port_detection|IsZandronum]]. | |
| |
| If the engine is not Zandronum, the function returns true, because in ZDoom/GZDoom all players run gameplay code. | If the engine is not Zandronum, the function returns true, because in ZDoom/GZDoom multiplayer all players run gameplay code. |
| |
| If the engine is Zandronum, the functions returns false if it's real multiplayer, true if it's multiplayer emulation. | If the engine is Zandronum, the function returns false if it's real multiplayer, true if it's multiplayer emulation. |
| |
| ''IsServerOnly'' and ''IsClientOnly'' simply return ''IsServer() && !IsClient()'' and ''IsClient() && !IsServer()'', respectively. | ''IsServerOnly'' and ''IsClientOnly'' simply return ''IsServer() && !IsClient()'' and ''IsClient() && !IsServer()'', respectively. |
| |