S&box Wiki

Revision Difference

RPCs#528742

<cat>Dev.Intro</cat>⤶ <title>RPCs</title>⤶ ⤶ # What Are RPCs⤶ ⤶ They're just network messages.⤶ ⤶ The server sends a message to the client, when the client receives the message it calls a function. It's that simple.⤶ ⤶ # Example⤶ ⤶ On entities you can mark a function as an RPC⤶ ⤶ ```⤶ [Client]⤶ public void ShowDeathScreen( string killedName )⤶ {⤶ Log.Info( "TODO SHOW DEATH SCREEN" );⤶ }⤶ ```⤶ ⤶ Here Client tells us that this method should only ever be called on the client.⤶ ⤶ So if you're on the server and you do this:⤶ ⤶ ```⤶ entity.ShowDeathScreen( "Killa04" );⤶ ```⤶ ⤶ It gets called on the client version of the entity. We've done some magic in the background. ⤶ ⤶ <note>During compilation we've added an extra step in the RPC function that checks if we're being called serverside, and if we are it'll broadcast a message to every client which will call the method.</note>⤶ ⤶ ⤶ # Targetting Players⤶ ⤶ We have a problem with the above example. We wouldn't want to show the death screen to every player. We'd want to show it to just one. ⤶ ⤶ The code generator has got you covered, it's created another function in the background which takes a target player as the first argument.⤶ ⤶ ```⤶ entity.ShowDeathScreen( deadPlayer, "Killa04" );⤶ ```⤶ ⤶ Calling this will only call `ShowDeathScreen` on `deadPlayer`'s client.⤶ ⤶ ⤶ # todo⤶ ⤶ * Sending messages from client to server⤶ * Sending to groups of players (teams etc)⤶ * Multicast messages⤶