Unreal Reference
The SpacetimeDB client for Unreal Engine contains all the tools you need to build native clients for SpacetimeDB modules using C++ and Blueprint.
Before diving into the reference, you may want to review:
- Generating Client Bindings - How to generate Unreal bindings from your module
- Connecting to SpacetimeDB - Establishing and managing connections
- SDK API Reference - Core concepts that apply across all SDKs
| Name | Description |
|---|---|
| Project setup | Configure an Unreal project to use the SpacetimeDB Unreal client SDK. |
| Generate module bindings | Use the SpacetimeDB CLI to generate module-specific types and interfaces. |
| DbConnection type | A connection to a remote database. |
| Context interfaces | Context objects for interacting with the remote database in callbacks. |
| Access the client cache | Access to your local view of the database. |
| Observe and invoke reducers | Send requests to the database to run reducers, and register callbacks for reducer results on the calling connection. |
| Subscriptions | Subscribe to queries and manage subscription lifecycle. |
| Query Builder API | Build typed subscription queries in Unreal C++ and Blueprint. |
| Identify a client | Types for identifying users and client connections. |
Project setup
Using the Unreal Engine Plugin
Add the SpacetimeDB Unreal SDK to your project as a plugin. The SDK provides both C++ and Blueprint APIs for connecting to SpacetimeDB modules.
Generate module bindings
Each SpacetimeDB client depends on some bindings specific to your module. Generate the Unreal interface files using the Spacetime CLI. From your project directory, run:
spacetime generate --lang unrealcpp --uproject-dir <uproject_directory> --module-path <module_path> --unreal-module-name <module_name>
Replace:
<uproject_directory>with the path to your Unreal project directory (containing the.uprojectfile)<module_path>with the path to your SpacetimeDB module<module_name>with the name of your Unreal module, typically the name of the project
Example:
spacetime generate --lang unrealcpp --uproject-dir /path/to/MyGame --module-path /path/to/quickstart-chat --unreal-module-name QuickstartChat
This generates module-specific bindings in your project's ModuleBindings directory.
Type DbConnection
A connection to a remote database is represented by the UDbConnection class. This class is generated per module and contains information about the types, tables, and reducers defined by your module.
| Name | Description |
|---|---|
| Connect to a database | Construct a UDbConnection instance. |
| Advance the connection | Process queued WebSocket messages and dispatch callbacks. |
| Access tables and reducers | Access the client cache, request reducer invocations, and register callbacks. |
Connect to a database
class UDbConnection
{
UFUNCTION(BlueprintCallable, Category = "SpacetimeDB")
static UDbConnectionBuilder* Builder();
};
Construct a UDbConnection by calling UDbConnection::Builder(), chaining configuration methods, and finally calling .Build(). At a minimum, you must specify WithUri to provide the URI of the SpacetimeDB instance, and WithDatabaseName to specify the database's name or identity.
| Name | Description |
|---|---|
| WithUri method | Set the URI of the SpacetimeDB instance hosting the remote database. |
| WithDatabaseName method | Set the name or identity of the remote database. |
| WithToken method | Supply a token to authenticate with the remote database. |
| WithCompression method | Set the compression method for WebSocket communication. |
| OnConnect callback | Register a callback to run when the connection is successfully established. |
| OnConnectError callback | Register a callback to run if the connection is rejected or the host is unreachable. |
| OnDisconnect callback | Register a callback to run when the connection ends. |
| Build method | Finalize configuration and open the connection. |
Method WithUri
class UDbConnectionBuilder
{
UFUNCTION(BlueprintCallable, Category = "SpacetimeDB")
UDbConnectionBuilder* WithUri(const FString& InUri);
};
Configure the URI of the SpacetimeDB instance or cluster which hosts the remote module and database.
Method WithDatabaseName
class UDbConnectionBuilder
{
UFUNCTION(BlueprintCallable, Category = "SpacetimeDB")
UDbConnectionBuilder* WithDatabaseName(const FString& InName);
};
Configure the SpacetimeDB domain name or Identity of the remote database which identifies it within the SpacetimeDB instance or cluster.
Method WithToken
class UDbConnectionBuilder
{
UFUNCTION(BlueprintCallable, Category = "SpacetimeDB")
UDbConnectionBuilder* WithToken(const FString& InToken);
};
Chain a call to .WithToken(token) to your builder to provide an OpenID Connect compliant JSON Web Token to authenticate with, or to explicitly select an anonymous connection.
Method WithCompression
class UDbConnectionBuilder
{
UFUNCTION(BlueprintCallable, Category = "SpacetimeDB")
UDbConnectionBuilder* WithCompression(const ESpacetimeDBCompression& InCompression);
};
Set the compression method for WebSocket communication. Available options:
ESpacetimeDBCompression::None- No compressionESpacetimeDBCompression::Gzip- Gzip compression (default)ESpacetimeDBCompression::Brotli- Brotli compression (not implemented, defaults to Gzip)
Callback OnConnect
class UDbConnectionBuilder
{
UFUNCTION(BlueprintCallable, Category = "SpacetimeDB")
UDbConnectionBuilder* OnConnect(FOnConnectDelegate Callback);
};
Chain a call to .OnConnect(callback) to your builder to register a callback to run when your new UDbConnection successfully initiates its connection to the remote database. The callback accepts three arguments: a reference to the UDbConnection, the FSpacetimeDBIdentity by which SpacetimeDB identifies this connection, and a private access token which can be saved and later passed to WithToken to authenticate the same user in future connections.
Callback OnConnectError
class UDbConnectionBuilder
{
UFUNCTION(BlueprintCallable, Category = "SpacetimeDB")
UDbConnectionBuilder* OnConnectError(FOnConnectErrorDelegate Callback);
};
Chain a call to .OnConnectError(callback) to your builder to register a callback to run when your connection fails.
Callback OnDisconnect
class UDbConnectionBuilder
{
UFUNCTION(BlueprintCallable, Category = "SpacetimeDB")
UDbConnectionBuilder* OnDisconnect(FOnDisconnectDelegate Callback);
};
Chain a call to .OnDisconnect(callback) to your builder to register a callback to run when your UDbConnection disconnects from the remote database, either as a result of a call to Disconnect or due to an error.
Method Build
class UDbConnectionBuilder
{
UFUNCTION(BlueprintCallable, Category = "SpacetimeDB")
UDbConnection* Build();
};
Finalize configuration and open the connection. This creates a WebSocket connection to ws://<uri>/v1/database/<database>/subscribe?compression=<compression> and begins processing messages using the Unreal SDK's binary v2 WebSocket subprotocol.
Advance the connection and process messages
The Unreal SDK receives WebSocket messages asynchronously, then queues them for processing on the game thread. You must either call FrameTick() regularly, typically from an Actor's Tick(), or enable automatic ticking once with SetAutoTicking(true). Without one of these, queued messages are not processed and table callbacks, reducer callbacks, and subscription callbacks will not fire.
You can either call FrameTick() yourself from an Actor or component tick, or enable the SDK's automatic ticker once after building the connection:
void AGameManager::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
if (Conn && Conn->IsActive())
{
Conn->FrameTick();
}
}
Conn = Builder->Build();
Conn->SetAutoTicking(true);
Method FrameTick
class UDbConnection
{
UFUNCTION(BlueprintCallable, Category = "SpacetimeDB")
void FrameTick();
};
Process any queued server messages and dispatch the corresponding callbacks. Call this regularly from game-thread code if you manage ticking yourself.
Method SetAutoTicking
class UDbConnection
{
UFUNCTION(BlueprintCallable, Category = "SpacetimeDB")
void SetAutoTicking(bool bAutoTick);
};
Enable or disable the SDK's automatic ticker. When enabled, the connection registers with Unreal's core ticker and calls FrameTick() for you.
Access tables and reducers
class UDbConnection
{
UPROPERTY(BlueprintReadOnly, Category="SpacetimeDB")
URemoteTables* Db;
UPROPERTY(BlueprintReadOnly, Category="SpacetimeDB")
URemoteReducers* Reducers;
};
The Db property provides access to the client cache, and the Reducers property allows invoking reducers and handling the results of reducers called by this connection.
Context interfaces
Context objects provide access to the database and reducers within callback functions. All context types inherit from FContextBase.
| Name | Description |
|---|---|
| FContextBase | Base context providing access to Db and Reducers. |
| FEventContext | Context for table row event callbacks. |
| FReducerEventContext | Context for reducer event callbacks. |
| FSubscriptionEventContext | Context for subscription lifecycle callbacks. |
| FErrorContext | Context for error callbacks. |
Type FContextBase
USTRUCT(BlueprintType)
struct FContextBase
{
GENERATED_BODY()
UPROPERTY(BlueprintReadOnly, Category = "SpacetimeDB")
URemoteTables* Db;
UPROPERTY(BlueprintReadOnly, Category = "SpacetimeDB")
URemoteReducers* Reducers;
UFUNCTION(BlueprintCallable, Category = "SpacetimeDB")
USubscriptionBuilder* SubscriptionBuilder();
UFUNCTION(BlueprintCallable, Category = "SpacetimeDB")
bool TryGetIdentity(FSpacetimeDBIdentity& OutIdentity) const;
UFUNCTION(BlueprintCallable, Category = "SpacetimeDB")
FSpacetimeDBConnectionId GetConnectionId() const;
};
Base context providing access to the client cache, reducers, subscription builder, and connection information.