Function Definitions¶
- A function definition consists of an opening, body, and closer
Function Opening Statement¶
DEF
, followed by a series of variables (arguments), followed by a colon (:
) to end the statement:
1 | DEF fib $n:
|
- The variables referenced in the opening statement will be created as function scope variables (that is, not accessible from outside the function) with the value received from the function call.
Function Body¶
- The function body consists of a sequence of statements of any length, as long as those statements do not evaluate to a function/receiver opening or closing statement.
- This implies that function/receiver definitions are not allowed inside other function/receiver definitions.
- During execution of code inside the function body, variables defined as arguments will initially evaluate to the value of the argument. Once execution exits the function, those variables are destroyed and can no longer by referenced. This is what’s meant by “function scope”.
- During execution of code in the function body, if a
RETURN
statement is encountered at any time, execution will immediately complete and exit the function all with the returned value. - If all lines of code in the function body have been evaluated and no
RETURN
statements are encountered, theNULL
value is returned. In other words, it is the equivalent of writingRETURN NULL;
as the last line in the function body.
Function Closing Statement¶
- The single token
END_DEF
- This token must appear as its own line; e.g.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | # correct syntax
DEF test $n:
RETURN PLUS $n 1;
END_DEF
# syntax error
DEF test2 $n:
RETURN PLUS $n 1; END_DEF
# syntax error
DEF test $n:
RETURN PLUS $n 1;
END_DEF DEF next_function:
...
|
Receiver Definitions¶
Receivers are a subset of functions that work mostly the same, except:
- The opening/closing syntax uses the keyword
RECEIVE
andEND_RECEIVE
instead ofDEF
/END_DEF
- Instead of
RETURN
, the keywordsACCEPT
andREJECT
are used to return from the receiver.
ACCEPT
means the message received was valid andREJECT
means it was not.- Upon execution of
ACCEPT
, the game state will be updated and broadcast to all players (it will trigger a frontend UI refresh)- Upon execution of
REJECT
, the argument to theREJECT
primitive (a string) will be sent back to the frontend as an error message (only to the player originating the message being parsed by this receiver).- Upon execution of
REJECT
, the game state will not be updated, as it is assumed that the message received was an invalid game action. If you had changed the game state in the code prior toREJECT
, those changes will be discarded.
Special Functions¶
- The function (not receiver)
set_up
, if it exists, is executed at the start of the game. - During set up, a few special PROPs s are also automatically initiated:
current_player
is set to the first element of the special PROPplay_order
game_over
is set tofalse
winner
is set toNULL
Here is the entire sequence of events/executions for a Turnbase game:
- Creator of a game room starts the game (via clicking “start game”)
- The initial state JSON is loaded as the game state.
- If the function
set_up
exists, it is executed. If it does not exist, this step is skipped. - Special PROPs are set.
- Any time a message is received from the frontend, it is matched against the list of receivers defined in the ruleset. If a match is found, that receiver is executed.
Other Considerations¶
- No two functions or receivers should have the same name
- At least one receiver needs to be defined in the rules