Server-only/Client-only code

How to strip code from the server or client, making code server or client only.

When developing a game in Airship, it may be prudent to not have server logic on the client. For example, if you have an anti-cheat system or other code that the client should not know about.

triangle-exclamation

Method 1: Server-only/Client-only methods

Inside AirshipBehaviour classes, you can use the @Server() and @Client() method decoratorsarrow-up-right to tell the compiler you want the given methods to be server or client only.

export default class ServerAndClientOnlyMethods extends AirshipBehaviour {
    @Server()
    public ServerOnlyMethod() {
        print("This will only be on, and callable by the server!")
    }
    
    @Client()
    public ClientOnlyMethod() {
        print("This will only be on, and callable by the client!");
    }
}
circle-exclamation

Server/Client only lifecycle methods

Lifecycle methods may also be designated as server or client only. In the example below, the Start lifecycle method automatically runs on the server but not on the client.

Method 2: Conditional directives

There may be cases you want to mark blocks of code server/client only rather than entire functions. This will work even if it's not inside an AirshipBehaviour. To do this, we use directives. There are two directives:

  • $SERVER - when in a condition, will check if it's the server

  • $CLIENT - when in a condition, will check if it's the client

circle-exclamation

You can mix these with the decorators as well, to do cool things like create server/client lifecycle event methods:

Last updated