API Reference
NewWorld
NewWorld
creates a new World
object.
Parameters
Parameter | Type | Description |
---|---|---|
opts | …WorldOption | Options for configuring the world. |
Return Values
Type | Description |
---|---|
*World | A pointer to the newly created World instance. |
error | An error indicating any issues during creation. |
Options
WithCustomMockRedis
The WithCustomMockRedis
option uses the given miniredis instance as the storage layer for the game state. Game state saved to these instances of miniredis are not persistent across world restarts, so this should only be used for local development and testing.
Parameters
Parameter | Type | Description |
---|---|---|
miniRedis | *miniredis.Miniredis | A miniredis instance. |
WithDisableSignatureVerification
The WithDisableSignatureVerification
option disables signature verification on the World’s server. This should only be used for testing.
Parameters
This method has no parameters.
WithPort
The WithPort
option allows for a custom port to be set for the World’s server. If this option is unset it uses a default port of “4040”.
Parameters
Parameter | Type | Description |
---|---|---|
port | string | The port number for the world’s server. |
WithPrettyLog
The WithPrettyLog
option enables JSON parsing and colorized, human-friendly formatted logs using zerolog.
Parameters
This method has no parameters.
WithReceiptHistorySize
The WithReceiptHistorySize option specifies the number of ticks for which the World object retains receipts. For instance, at tick 40 with a receipt history size of 5, the World stores receipts from ticks 35 to 39. Upon reaching tick 41, it will hold receipts for ticks 36 to 40. If this option remains unset, it defaults to a history size of 10. Game clients can get receipts via the /query/receipts/list endpoint. Nakama also uses this endpoint to transmit receipts to listening clients.
Parameters
Parameter | Type | Description |
---|---|---|
size | int | The size of the receipt history to be set for World. |
Setting a very large receipt history size may impact memory usage and performance. Choose a size that balances your game’s needs with resource constraints.
WithStoreManager
The WithStoreManager
option overrides the default gamestate manager. The gamestate manager is responsible for storing entity and component information, and recovering those values after a world restart. A default manager will be created if this option is unset.
Parameters
Parameter | Type | Description |
---|---|---|
s | gamestate.Manager | The replacement game-state manager. |
WithTickChannel
The WithTickChannel
option sets a channel that will be used to start each tick. A game tick will be started each time a message appears on the given channel. A custom tick rate can be set using time.Tick. This is also useful in tests to manually start ticks. If unset, a default tick rate of 1 per second is used.
Parameters
Parameter | Type | Description |
---|---|---|
ch | <-chan time.Time | The channel that will start each tick. |
Example
Choose your tick rate carefully based on your game’s requirements. Higher tick rates provide smoother updates but require more processing power and network bandwidth.
WithTickDoneChannel
The WithTickDoneChannel
option sets a channel that will receive the just-completed tick number each time a tick completes execution. All systems are guaranteed to have been called when a message appears on this channel. This is particularly useful in tests to ensure your systems have fully executed before checking expectations.
Parameters
Parameter | Type | Description |
---|---|---|
ch | chan<- uint64 | The channel that will be notified at the end of each tick. |
Example
The WithTickDoneChannel is essential for writing deterministic tests. Always wait for the done signal before making assertions about game state changes.
RegisterSystems
registers one or more systems to the World
. Systems are executed in the order of which they were added to the world.
Example
RegisterInitSystems
RegisterInitSystems
registers one or more init systems to the World
. Init systems are executed exactly one time on tick 0. Init systems will not be run when loading a pre-existing world from permanent storage (e.g. on a server restart).
Example
Parameters
Parameter | Type | Description |
---|---|---|
world | *World | A pointer to a World instance. |
s | …System | Variadic parameter for init systems to be added to the World. |
RegisterComponents
RegisterComponents
registers one or more components to the World
. Upon registration, components are assigned an ID. IDs are assigned incrementally, starting from 0, in the order in which they were passed to the method.
Example
Parameters
Parameter | Type | Description |
---|---|---|
T | type parameter | A component struct that implements the Name method. |
world | *World | A pointer to a World instance. |
Return Value
Type | Description |
---|---|
error | An error indicating any issues that occurred during the component registration. |
RegisterQuery
RegisterQuery
registers the queries in the World
. This allows the Query
endpoints to be automatically generated.
Example
Parameters
Parameter | Type | Description |
---|---|---|
Request | type parameter | The input type of this query |
Reply | type parameter | The output type of this query |
world | *World | A pointer to a World instance. |
name | string | The name of the server endpoint to use this query |
handler | func(engine.Context, *Request) (Reply, error) | The handler to execute the logic of this query |
opts | …QueryOption[Request, Reply] | Variadic options to augment the behavior of this query |
Return Value
Type | Description |
---|---|
error | An error indicating any issues that occurred during the query registration. |
RegisterMessages
RegisterMessages
registers messages in the World
. This allows message endpoints to be automatically generated.
Example
Parameters
Parameter | Type | Description |
---|---|---|
world | *World | A pointer to a World instance. |
msgs | …AnyMessage | Variadic parameter for Message instances to be registered. |
Return Value
Type | Description |
---|---|
error | An error indicating any issues that occurred during the message registration. |
StartGame
StartGame
starts the game by loading any previously saved game state, spinning up the message/query handler, and starting the game ticks. This method blocks the main Go routine. If for whatever reason execution needs to continue after calling this method, it should be called in a separate go routine.
Return Value
Type | Description |
---|---|
error | An error indicating any issues when starting the game. |