Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Multiplayer

CALL HOST · CALL PLAYER · SET ROOM · STOP HOST · STOP PLAYER · SYNC · crashnet · gamecontext · host · me · players


CALL HOST

Call a function when this player becomes the host (CrashNet).

CALL HOST callback, id
call_host(callback, id)

Also documents: guard, guard_fails

Parameters

  • callback AS FUNCTION - The function to call.
  • id AS STRING - Its name, for stop_call_host minus call_.

After !!

  • guard(value AS ARRAY) - Where it registers and how it runs: a list of one or more of “host”, “player”, “client”, “single” (contexts) and “once” (never two at a time), even for just one.
  • guard_fails(on = TRUE) - A guard that does not hold is an error rather than skipped.

Example

SCREEN 320, 240
FUNCTION took_over()
    PRINT "I am the host now"
END FUNCTION
// Called if this player becomes the host of a CrashNet game
CALL HOST took_over, "host_watch"
PRINT "waiting to host:", me.ishost
STOP HOST "host_watch"

Notes

It’s also the function call_host, for when you want what it gives back.

CALL PLAYER

Call a function when a player joins or leaves (CrashNet).

CALL PLAYER callback, id
call_player(callback, id)

Also documents: guard, guard_fails

Parameters

  • callback AS FUNCTION - The function to call.
  • id AS STRING - Its name, for stop_call_player minus call_.

After !!

  • guard(value AS ARRAY) - Where it registers and how it runs: a list of one or more of “host”, “player”, “client”, “single” (contexts) and “once” (never two at a time), even for just one.
  • guard_fails(on = TRUE) - A guard that does not hold is an error rather than skipped.

Example

SCREEN 320, 240
// Called whenever a player joins or leaves a CrashNet game, with the player:
// its slot, name, ishost, connected, joined (TRUE joining, FALSE leaving)
// and the game's PLAYER DATA fields
FUNCTION joined_or_left(player)
    IF player.joined
        PRINT player.name, "joined in slot", player.slot
    ELSE
        PRINT player.name, "left slot", player.slot
    END IF
END FUNCTION
CALL PLAYER joined_or_left, "lobby"
PRINT "players:", players.count
STOP PLAYER "lobby"

Notes

It’s also the function call_player, for when you want what it gives back.

SET ROOM

Move this player into a CrashNet room (“” leaves every room).

SET ROOM room
set_room(room)

Parameters

  • room AS STRING - The room.

Example

SCREEN 320, 240
// Join the room "arena"; SET ROOM "" leaves every room
SET ROOM "arena"
me.room_name = "arena"
PRINT "in", me.room_name
SET ROOM ""

Notes

It’s also the function set_room, for when you want what it gives back.

STOP HOST

Remove a call_host handler.

STOP HOST id
stop_host(id)

Parameters

  • id AS STRING - Its id.

Example

SCREEN 320, 240
FUNCTION took_over()
    PRINT "hosting"
END FUNCTION
CALL HOST took_over, "host_watch"
// No longer interested
STOP HOST "host_watch"
PRINT "stopped watching"

Notes

It’s also the function stop_host, for when you want what it gives back.

STOP PLAYER

Remove a call_player handler.

STOP PLAYER id
stop_player(id)

Parameters

  • id AS STRING - Its id.

Example

SCREEN 320, 240
FUNCTION joined_or_left(player)
    PRINT "player change in slot", player.slot
END FUNCTION
CALL PLAYER joined_or_left, "lobby"
// The game has started: stop listening
STOP PLAYER "lobby"
PRINT "lobby closed"

Notes

It’s also the function stop_player, for when you want what it gives back.

SYNC

Mark a sync point for CrashNet’s tick-based sync.

SYNC
sync()

Example

SCREEN 320, 240
me.score = 10
// A sync point: in a CrashNet game, this tick's changes go out together
SYNC
PRINT me.score

Notes

It’s also the function sync, for when you want what it gives back.

crashnet

Built-in object providing information about the current CrashNet session state. CONNECTED = 1 when networked, 0 otherwise.

crashnet

Example

SCREEN 320, 240
// 0 in a single-player game
IF crashnet.connected
    PRINT "online"
ELSE
    PRINT "playing alone"
END IF

gamecontext

Built-in pseudo-string that reports which of the four execution contexts this interpreter is running in. Returns one of:

  • "SINGLE" — single-player launch. CrashPlayer was started with a local game directory or .crashcart (no –remote flag, no crashnet:// descriptor). The interpreter is the only one running; nothing is connected over the network.
  • "HOST" — the headless host interpreter (slot 0, server-side) inside a CrashNet session. Authoritative for game state; broadcasts sprite frames; runs no renderer.
  • "PLAYER" — a player-server interpreter (slot 1+, server-side). One per connected player. Authoritative for sprites that player creates; sees STREAM-opted input from the client.
  • "CLIENT" — a local CrashPlayer interpreter connected to a CrashNet server. Renders broadcasts from the host, owns local-only sprites in the 32768+ ID space, forwards opted-in input.
gamecontext

Example

SCREEN 320, 240
PRINT "running as", gamecontext

host

The game’s own fields, which HOST DATA declares. Everyone reads them, and only the host changes them.

host

Example

SCREEN 320, 240
host.round = 1
host.round = host.round + 1
PRINT "round", host.round

me

The player this program runs for: its slot and name, whether it ishost or isclient, and the fields PLAYER DATA declares. A player’s server may change its own fields; a client only reads them.

me

Example

SCREEN 320, 240
me.score = 0
me.score = me.score + 10
PRINT me.name, me.slot, me.score, me.ishost

players

The players: players.count is how many are connected, and players(n) is player n’s record, or NULL when no one has that slot. The host may change any player’s fields.

players
players(slot AS NUMBER = NULL)

Parameters

  • slot AS NUMBER = NULL - The player’s slot.

Example

SCREEN 320, 240
players(1).team = "red"
PRINT players.count, players(1).team