Query
How to define and register a query
Queries are predefined API endpoints that provide access to the game state data and other information that a game client might need.
In Cardinal, queries are defined as a pair of Go structs representing a Request
and Response
along with a handler function with the following signature:
Example:
- A
WorldVars
static query used to provide the client with game constants. - A
PlayerLocation
query used to provide the client with the location of the player.
Defining Queries
By convention, queries are defined in the query
directory with each query definition in its own separate file.
You can easily create a new query and register it to the world by following these steps:
Define the request/response struct and the handler function
A query is defined using a pair of Go structs and a handler function. You can write any arbitrary logic in the handler function and access the game state using cardinal.WorldContext
.
Register the query in the world
Queries must be registered in the world before they can be used. This is done by calling the RegisterQuery
function and passing in the name of the endpoint and the function handler.
Query Options
EVM Support
Queries can be sent by EVM smart contracts by using the WithQueryEVMSupport
option when you register your queries. This will generate the ABI types necessary for interactions with smart contracts.
Not all Go types are supported for the fields in your query structs when using this option. See EVM+ Message and Query to learn more.