mirror of
https://github.com/clockworklabs/SpacetimeDB.git
synced 2026-05-06 07:26:43 -04:00
Fix and polish part 1 and part 2 of the Godot tutorial
This commit is contained in:
committed by
Lisandro Crespo
parent
2ca4b55d6d
commit
93ae99ce49
@@ -25,6 +25,7 @@ SpacetimeDB supports Godot version `4.6.2` or later. See [the overview](.) for m
|
||||
|
||||
Open Godot and create a new project by selecting "+ Create" from the Project Manager.
|
||||
|
||||
// TODO: THIS IMAGE MUST BE UPDATED
|
||||

|
||||
|
||||
For `Project Name` use `blackholio`. For `Project Location` select a directory that you can navigate to via the CLI because we will need to do so in part 2.
|
||||
@@ -52,10 +53,10 @@ dotnet add package SpacetimeDB.ClientSDK
|
||||
|
||||
The SpacetimeDB Godot SDK provides helpful tools for integrating SpacetimeDB into Godot, including a connection update manager which will synchronize your Godot client's state with your SpacetimeDB database in accordance with your subscription queries.
|
||||
|
||||
### Add the GameManager to the Scene
|
||||
### Create a new Scene
|
||||
|
||||
1. **Create a 2D Scene**:
|
||||
- In the **Scene** (usually on the top left), click on **Create Root Node: -> 2D Scene**.
|
||||
- In the **Scene** Dock (usually on the top left), click on **Create Root Node: -> 2D Scene**.
|
||||
- Alternatively, you can click the **+** button to add a child node and select a **Node 2D**.
|
||||
|
||||

|
||||
@@ -73,7 +74,7 @@ You will see a warning saying `This inspector might be out of date. Please build
|
||||
- Alternatively, go to `Scene -> Save Scene As`.
|
||||
|
||||
5. **Run Project and build C# Project**:
|
||||
- Press `F5` or (on Mac) to run the project, click on `Select Current` to select the current scene as the main one.
|
||||
- Press `F5` (or `Cmd + B` on Mac) to run the project, click on `Select Current` to select the current scene as the main one.
|
||||
- Alternatively, you can click on the play button in the top menu.
|
||||
|
||||
Our Godot project is all set up! If you press play, it will show a blank screen, but it should start the game without any errors. Now we're ready to get started on our SpacetimeDB server module, so we have something to connect to!
|
||||
|
||||
@@ -45,7 +45,9 @@ Run the following command to initialize the SpacetimeDB server module project wi
|
||||
spacetime init --lang csharp --server-only blackholio
|
||||
```
|
||||
|
||||
This command creates a new folder named `spacetimedb` inside of your Godot project `blackholio` directory and sets up the SpacetimeDB server project with C# as the programming language.
|
||||
Use `blackholio-server` for the project path and `blackholio` for the database name.
|
||||
|
||||
This command creates a new folder named `blackholio-server` inside of your Godot project `blackholio` directory. Inside, there's a `spacetimedb` folder that contains the SpacetimeDB server project with C# as the programming language.
|
||||
|
||||
</TabItem>
|
||||
<TabItem value="rust" label="Rust">
|
||||
@@ -55,7 +57,9 @@ Run the following command to initialize the SpacetimeDB server module project wi
|
||||
spacetime init --lang rust --server-only blackholio
|
||||
```
|
||||
|
||||
This command creates a new folder named `spacetimedb` inside of your Godot project `blackholio` directory and sets up the SpacetimeDB server project with Rust as the programming language.
|
||||
Use `blackholio-server` for the project path and `blackholio` for the database name.
|
||||
|
||||
This command creates a new folder named `blackholio-server` inside of your Godot project `blackholio` directory. Inside, there's a `spacetimedb` folder that contains the SpacetimeDB server project with Rust as the programming language.
|
||||
|
||||
</TabItem>
|
||||
<TabItem value="cpp" label="C++">
|
||||
@@ -67,7 +71,9 @@ Run the following command to initialize the SpacetimeDB server module project wi
|
||||
spacetime init --lang cpp --server-only blackholio
|
||||
```
|
||||
|
||||
This command creates a new folder named `spacetimedb` inside of your Godot project `blackholio` directory and sets up the SpacetimeDB server project with C++ as the programming language.
|
||||
Use `blackholio-server` for the project path and `blackholio` for the database name.
|
||||
|
||||
This command creates a new folder named `blackholio-server` inside of your Godot project `blackholio` directory. Inside, there's a `spacetimedb` folder that contains the SpacetimeDB server project with C++ as the programming language.
|
||||
</TabItem>
|
||||
</Tabs>
|
||||
|
||||
@@ -75,21 +81,21 @@ This command creates a new folder named `spacetimedb` inside of your Godot proje
|
||||
|
||||
<Tabs groupId="server-language" defaultValue="rust">
|
||||
<TabItem value="csharp" label="C#">
|
||||
In this section we'll be making some edits to the file `blackholio/spacetimedb/Lib.cs`. We recommend you open up this file in an IDE like VSCode or Rider.
|
||||
In this section we'll be making some edits to the file `blackholio-server/spacetimedb/Lib.cs`. We recommend you open up this file in an IDE like VSCode or Rider.
|
||||
|
||||
**Important: Open the `blackholio/spacetimedb/Lib.cs` file and delete its contents. We will be writing it from scratch here.**
|
||||
**Important: Open the `blackholio-server/spacetimedb/Lib.cs` file and delete its contents. We will be writing it from scratch here.**
|
||||
|
||||
</TabItem>
|
||||
<TabItem value="rust" label="Rust">
|
||||
In this section we'll be making some edits to the file `blackholio/spacetimedb/src/lib.rs`. We recommend you open up this file in an IDE like VSCode or RustRover.
|
||||
In this section we'll be making some edits to the file `blackholio-server/spacetimedb/src/lib.rs`. We recommend you open up this file in an IDE like VSCode or RustRover.
|
||||
|
||||
**Important: Open the `blackholio/spacetimedb/src/lib.rs` file and delete its contents. We will be writing it from scratch here.**
|
||||
**Important: Open the `blackholio-server/spacetimedb/src/lib.rs` file and delete its contents. We will be writing it from scratch here.**
|
||||
|
||||
</TabItem>
|
||||
<TabItem value="cpp" label="C++">
|
||||
In this section we'll be making some edits to the file `blackholio/spacetimedb/src/lib.cpp`. We recommend you open up this file in an IDE like VSCode or Rider.
|
||||
In this section we'll be making some edits to the file `blackholio-server/spacetimedb/src/lib.cpp`. We recommend you open up this file in an IDE like VSCode or Rider.
|
||||
|
||||
**Important: Open the `blackholio/spacetimedb/src/lib.cpp` file and delete its contents. We will be writing it from scratch here.**
|
||||
**Important: Open the `blackholio-server/spacetimedb/src/lib.cpp` file and delete its contents. We will be writing it from scratch here.**
|
||||
</TabItem>
|
||||
</Tabs>
|
||||
|
||||
@@ -221,7 +227,7 @@ Next, we're going to define a new `SpacetimeType` called `DbVector2` which we're
|
||||
|
||||
```csharp
|
||||
// This allows us to store 2D points in tables.
|
||||
[SpacetimeDB.Type]
|
||||
[Type]
|
||||
public partial struct DbVector2
|
||||
{
|
||||
public float x;
|
||||
@@ -256,7 +262,7 @@ public partial struct Circle
|
||||
public int player_id;
|
||||
public DbVector2 direction;
|
||||
public float speed;
|
||||
public SpacetimeDB.Timestamp last_split_time;
|
||||
public Timestamp last_split_time;
|
||||
}
|
||||
|
||||
[Table(Accessor = "food", Public = true)]
|
||||
@@ -371,7 +377,7 @@ The first table we defined is the `entity` table. An entity represents an object
|
||||
|
||||
We can create different types of entities with additional data by creating new tables with additional fields that have an `entity_id` which references a row in the `entity` table.
|
||||
|
||||
We've created two types of entities in our game world: `Food`s and `Circle`s. `Food` does not have any additional fields beyond the attributes in the `entity` table, so the `food` table simply represents the set of `entity_id`s that we want to recognize as food.
|
||||
We've created two types of entities in our game world: `Food` and `Circle`. `Food` does not have any additional fields beyond the attributes in the `entity` table, so the `food` table simply represents the set of `entity_id`s that we want to recognize as food.
|
||||
|
||||
The `Circle` table, however, represents an entity that is controlled by a player. We've added a few additional fields to a `Circle` like `player_id` so that we know which player that circle belongs to.
|
||||
|
||||
@@ -504,7 +510,7 @@ This following log output indicates that SpacetimeDB is successfully running on
|
||||
Starting SpacetimeDB listening on 127.0.0.1:3000
|
||||
```
|
||||
|
||||
Now that SpacetimeDB is running we can publish our module to the SpacetimeDB host. In a separate terminal window, navigate to the `blackholio/spacetimedb` directory.
|
||||
Now that SpacetimeDB is running we can publish our module to the SpacetimeDB host. In a separate terminal window, navigate to the `blackholio-server/spacetimedb` directory.
|
||||
|
||||
If you are not already logged in to the `spacetime` CLI, run the `spacetime login` command to log in to your SpacetimeDB website account. Once you are logged in, run `spacetime publish --server local blackholio`. This will publish our Blackholio server logic to SpacetimeDB.
|
||||
|
||||
@@ -526,23 +532,20 @@ Next, use the `spacetime` command to call our newly defined `Debug` reducer:
|
||||
```sh
|
||||
spacetime call --server local blackholio debug
|
||||
```
|
||||
|
||||
</TabItem>
|
||||
<TabItem value="rust" label="Rust">
|
||||
|
||||
Next, use the `spacetime` command to call our newly defined `debug` reducer:
|
||||
|
||||
```sh
|
||||
spacetime call --server local blackholio debug
|
||||
```
|
||||
|
||||
</TabItem>
|
||||
<TabItem value="cpp" label="C++">
|
||||
Next, use the `spacetime` command to call our newly defined `debug` reducer:
|
||||
|
||||
```sh
|
||||
spacetime call --server local blackholio debug
|
||||
```
|
||||
|
||||
</TabItem>
|
||||
</Tabs>
|
||||
|
||||
@@ -568,7 +571,7 @@ You should see something like the following output:
|
||||
|
||||
<Tabs groupId="server-language" defaultValue="rust">
|
||||
<TabItem value="csharp" label="C#">
|
||||
Next let's connect our client to our database. Let's start by modifying our `Debug` reducer. Rename the reducer to be called `Connect` and add `ReducerKind.ClientConnected` in parentheses after `SpacetimeDB.Reducer`. The end result should look like this:
|
||||
Next let's connect our client to our database. Let's start by modifying our `Debug` reducer. Rename the reducer to be called `Connect` and add `ReducerKind.ClientConnected` in parentheses after `Reducer`. The end result should look like this:
|
||||
|
||||
```csharp
|
||||
[Reducer(ReducerKind.ClientConnected)]
|
||||
@@ -638,25 +641,13 @@ spacetime publish --server local blackholio
|
||||
|
||||
The `spacetime` CLI has built in functionality to let us generate C# types that correspond to our tables, types, and reducers that we can use from our Godot client.
|
||||
|
||||
<Tabs groupId="server-language" defaultValue="rust">
|
||||
<TabItem value="csharp" label="C#">
|
||||
Let's generate our types for our module. In the `blackholio/spacetimedb`
|
||||
directory run the following command:
|
||||
</TabItem>
|
||||
<TabItem value="rust" label="Rust">
|
||||
Let's generate our types for our module. In the `blackholio/spacetimedb`
|
||||
directory run the following command:
|
||||
</TabItem>
|
||||
<TabItem value="cpp" label="C++">
|
||||
Let's generate our types for our module. In the `blackholio/spacetimedb` directory run the following command:
|
||||
</TabItem>
|
||||
</Tabs>
|
||||
Let's generate our types for our module. In the `blackholio-server/spacetimedb` directory run the following command:
|
||||
|
||||
```sh
|
||||
spacetime generate --lang csharp --out-dir ../module_bindings
|
||||
spacetime generate --lang csharp --out-dir ../../module_bindings
|
||||
```
|
||||
|
||||
This will generate a set of files in the `module_bindings` directory which contain the code generated types and reducer functions that are defined in your module, but usable on the client.
|
||||
This will generate a set of files in the `module_bindings` directory (inside your Godot `blackholio` project directory) which contain the code generated types and reducer functions that are defined in your module, but usable on the client.
|
||||
|
||||
```
|
||||
├── Reducers
|
||||
@@ -676,7 +667,7 @@ This will generate a set of files in the `module_bindings` directory which conta
|
||||
└── SpacetimeDBClient.g.cs
|
||||
```
|
||||
|
||||
This will also generate a file in the `module_bindings/SpacetimeDBClient.g.cs` directory with a type aware `DbConnection` class. We will use this class to connect to your database from Godot.
|
||||
This will also generate a file `module_bindings/SpacetimeDBClient.g.cs` with a type aware `DbConnection` class. We will use this class to connect to your database from Godot.
|
||||
|
||||
### Connecting to the Database
|
||||
|
||||
@@ -693,69 +684,76 @@ using Godot;
|
||||
Replace the implementation of the `GameManager` class with the following.
|
||||
|
||||
```cs
|
||||
public class GameManager : MonoBehaviour
|
||||
public partial class GameManager : Node
|
||||
{
|
||||
public static event Action OnConnected;
|
||||
public static event Action OnSubscriptionApplied;
|
||||
|
||||
[Export]
|
||||
private string ServerUrl { get; set; } = "http://127.0.0.1:3000";
|
||||
|
||||
[Export]
|
||||
private string DatabaseName { get; set; } = "blackholio";
|
||||
public static event Action OnConnected;
|
||||
public static event Action OnSubscriptionApplied;
|
||||
|
||||
[Export]
|
||||
private string ServerUrl { get; set; } = "http://127.0.0.1:3000";
|
||||
|
||||
[Export]
|
||||
private string DatabaseName { get; set; } = "blackholio";
|
||||
|
||||
[Export]
|
||||
private Color BackgroundColor { get; set; } = Colors.MidnightBlue;
|
||||
[Export]
|
||||
private Color BackgroundColor { get; set; } = Colors.MidnightBlue;
|
||||
|
||||
[Export]
|
||||
private float BorderThickness { get; set; } = 5.0f;
|
||||
[Export]
|
||||
private float BorderThickness { get; set; } = 5.0f;
|
||||
|
||||
[Export]
|
||||
private Color BorderColor { get; set; } = Colors.Goldenrod;
|
||||
[Export]
|
||||
private Color BorderColor { get; set; } = Colors.Goldenrod;
|
||||
|
||||
[Export]
|
||||
private string DefaultPlayerName { get; set; } = "3Blave";
|
||||
[Export]
|
||||
private string DefaultPlayerName { get; set; } = "3Blave";
|
||||
|
||||
private static GameManager Instance { get; private set; }
|
||||
private static GameManager Instance { get; set; }
|
||||
public static Identity LocalIdentity { get; private set; }
|
||||
public static DbConnection Conn { get; private set; }
|
||||
|
||||
public GameManager()
|
||||
{
|
||||
var builder = DbConnection.Builder()
|
||||
.OnConnect(HandleConnect)
|
||||
.OnConnectError(HandleConnectError)
|
||||
.OnDisconnect(HandleDisconnect)
|
||||
.WithUri(ServerUrl)
|
||||
.WithDatabaseName(DatabaseName);
|
||||
.OnConnect(HandleConnect)
|
||||
.OnConnectError(HandleConnectError)
|
||||
.OnDisconnect(HandleDisconnect)
|
||||
.WithUri(ServerUrl)
|
||||
.WithDatabaseName(DatabaseName);
|
||||
|
||||
AuthToken.Init();
|
||||
if (AuthToken.Token != string.Empty)
|
||||
{
|
||||
builder = builder.WithToken(AuthToken.Token);
|
||||
}
|
||||
if (AuthToken.TryGetToken(out var authToken))
|
||||
{
|
||||
builder = builder.WithToken(authToken);
|
||||
}
|
||||
|
||||
Conn = builder.Build();
|
||||
STDBUpdateManager.Add(Conn);
|
||||
Conn = builder.Build();
|
||||
STDBUpdateManager.Add(Conn);
|
||||
}
|
||||
|
||||
public override void _EnterTree()
|
||||
{
|
||||
Instance = this;
|
||||
}
|
||||
|
||||
public override void _ExitTree()
|
||||
{
|
||||
Disconnect();
|
||||
public override void _EnterTree()
|
||||
{
|
||||
Instance = this;
|
||||
}
|
||||
|
||||
if (Instance == this)
|
||||
{
|
||||
Instance = null;
|
||||
}
|
||||
}
|
||||
public override void _ExitTree()
|
||||
{
|
||||
Disconnect();
|
||||
|
||||
if (Instance == this)
|
||||
{
|
||||
Instance = null;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool IsConnected() => Conn != null && Conn.IsActive;
|
||||
|
||||
private void Disconnect()
|
||||
{
|
||||
STDBUpdateManager.Remove(Conn, true);
|
||||
Conn = null;
|
||||
}
|
||||
|
||||
// Called when we connect to SpacetimeDB and receive our client identity
|
||||
private void HandleConnect(DbConnection _conn, Identity identity, string token)
|
||||
private void HandleConnect(DbConnection conn, Identity identity, string token)
|
||||
{
|
||||
GD.Print("Connected.");
|
||||
AuthToken.SaveToken(token);
|
||||
@@ -779,7 +777,7 @@ public class GameManager : MonoBehaviour
|
||||
GD.Print("Disconnected.");
|
||||
if (ex != null)
|
||||
{
|
||||
GD.PrintException(ex);
|
||||
GD.PrintErr(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -788,19 +786,12 @@ public class GameManager : MonoBehaviour
|
||||
GD.Print("Subscription applied!");
|
||||
OnSubscriptionApplied?.Invoke();
|
||||
}
|
||||
|
||||
public static bool IsConnected() => Conn != null && Conn.IsActive;
|
||||
|
||||
|
||||
public void Disconnect()
|
||||
{
|
||||
STDBUpdateManager.Remove(Conn, true);
|
||||
Conn = null;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Here we configure the connection to the database, by passing it some callbacks in addition to providing the `ServerUrl` and `DatabaseName` to the connection. We pass the connection to `STDBUpdateManager`, a simple script that hooks into Godot's update loop in order to drive the sending and processing of messages between your client and SpacetimeDB. When the client connects, the SpacetimeDB SDK will call the `HandleConnect` method, allowing us to start up the game.
|
||||
Here we configure the connection to the database, by passing it some callbacks in addition to providing the `ServerUrl` and `DatabaseName` to the connection. We add the connection to `STDBUpdateManager`, a simple script that hooks into Godot's update loop in order to drive the sending and processing of messages between your client and SpacetimeDB.
|
||||
|
||||
When the client connects, the SpacetimeDB SDK will call the `HandleConnect` method, allowing us to start up the game.
|
||||
|
||||
In our `HandleConnect` callback we build a subscription and call `Subscribe`, subscribing to all data in the database. This causes SpacetimeDB to synchronize the state of all your tables with your Godot client's SDK client cache.
|
||||
|
||||
@@ -812,7 +803,29 @@ The "SDK client cache" is a client-side view of the database defined by the supp
|
||||
|
||||
---
|
||||
|
||||
Now we're ready to connect the client and server. Press the play button in Godot.
|
||||
There's only one thing left to do before being able to connect the client and server. Because our SpacetimeDB C# project lives nested in our Godot's project directory, we need to force exclude `blackholio-server` from Godot's C# `.csproj`. Open `Blackholio.csproj` with any text editor and add the following line at the end of the `<PropertyGroup>`:
|
||||
|
||||
```
|
||||
<DefaultItemExcludes>$(DefaultItemExcludes);blackholio-server/**</DefaultItemExcludes>
|
||||
```
|
||||
|
||||
Your `Blackholio.csproj` should look similar to this one:
|
||||
|
||||
```
|
||||
<Project Sdk="Godot.NET.Sdk/4.6.2">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<TargetFramework Condition=" '$(GodotTargetPlatform)' == 'android' ">net9.0</TargetFramework>
|
||||
<EnableDynamicLoading>true</EnableDynamicLoading>
|
||||
<DefaultItemExcludes>$(DefaultItemExcludes);blackholio-server/**</DefaultItemExcludes>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="SpacetimeDB.ClientSDK" Version="2.1.0" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
```
|
||||
|
||||
Now we're ready. Press the play button in Godot.
|
||||
|
||||
If all went well you should see the below output in your Godot logs.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user