Multiplayer & networking

Networking allows you to send variables to the server, which can then send it back to other players, or store it in a database.

WARNING

Networking must be optimized at all costs

  • Data: 2000 bytes per message
  • Broadcast (server to all): 10 msg per second
  • Send (server to client): 20 msg per second per user
  • Send (client to server): 40 msg per second
  • Message id string: 10 Characters

Sending data server-side client-side

First off you have to send your variable to either the server or the client, they will be able to listen for the variable so they can use it.

Sending is used for sending data to one specific user.

Broadcasting data server-side

Broadcasting allows you to send data to all connected clients.

# Send data to all clients
GAME.NETWORK.broadcast(
    "test",     # str message name
    {a: 1}      # obj data
);
1
2
3
4
5

Receiving data server-side client-side

Once the message has been sent, we can listen for it and use its given object.

Detect rate limiting server-side client-side

KrunkScript rate limits you to prevent spam over the network.

# Will return a "success" boolean, if false you are being rate limited
if (GAME.NETWORK.broadcast("hi there", {a: 1}) {
    # message was sent
} 
else {
    # message rate limited
}
1
2
3
4
5
6
7

Sending & broadcasting chat messages server-side

You can send chat messages from the server.

WARNING

  • Send: 2 msg per second
  • Broadcast: 2 msg per second
# Send message to specific player
GAME.CHAT.send(
    player.id,      # str player id
    "hello world",  # str message
    "#FFF"          # str color
);
1
2
3
4
5
6
# Broadcast message to all players
GAME.CHAT.broadcast(
    "hello world",  # str message
    "#FFF"          # str color
);
1
2
3
4
5

Movement syncing client-side

Every second the server sends the players position to the client. Once that information arrives at the client, the client retraces its steps since that point in time to now, using the corrected information that it has received from the server.

If your movement code includes custom playervalues, those have to be registered, to be included in the sync.

TIP

Check out this example to add basic double jumping using server syncing

WARNING

  • To sync a player property, the property must exist first (currently it gives a wrong error)
  • Syncing objects is not yet possible
  • Sync values can be up to 16 characters long
  • You can have up to 20 sync variables at once per player
# Create player reference
obj players = GAME.players.getSelf();

# Set property
players.hasDoubleJump = true;

# Start syncing player value
player.registerSyncValues(
    "hasDoubleJump"     #str object key
);
1
2
3
4
5
6
7
8
9
10
# Stop syncing player value
player.unregisterSyncValues(
    "hasDoubleJump"     #str object key
);
1
2
3
4

Open a window to a different game or platform client-side

GAME.URLS.openDiscord(
    "YBnq2um"                            # str discord invite url
);
1
2
3
GAME.URLS.openOpensea(
    "https://opensea.io/SwatDoge"        # str openSea wallet url
);
1
2
3
GAME.URLS.openTwitter(
    "https://twitter.com/swatdoge"       # str twitter url
);
1
2
3
GAME.URLS.openYoutube(
    "https://www.youtube.com/channel/UCkQM8ePPA8me73585aLJvmw"       # str youtube url
);
1
2
3
GAME.changeGame(
    "DQ_Battleboats"                    # str game name
);
1
2
3

Getting host server-side

WARNING

You can only retrieve host player object if player is spawned in

# Get player object of host
obj player = GAME.CONFIG.getHost();
1
2