Skip to content

Commit ab6b6b4

Browse files
authored
Dylan/onboarding-upgrades (#28)
* doc: Onboarding impr, fixes, consistency, cleanup refactor: Whats next cleanup, +unity, -bloat Removed redundant text while there refactor: Unity quickstart fixes, impr, prettify refactor: Unity pt1 fixes, impr, prettify fix(README): Rm "see test edits below" ref * !exists refactor(minor): General onboarding cleanup * Shorter, prettier, consistent fix(sdks/c#): Broken unitypackage url feat(sdks/c#): Add OneTimeQuery api ref * doc: Onboarding impr, fixes, consistency, cleanup * fix: Rm redundant 'module_bindings' mention * fix: Floating period, "arbitrary", "important": - PR review change requests - Additionally: hasUpdatedRecently fix and reformatting * fix: Mentioned FilterBy, used FindBy - Used FindBy since that was what the tutorial used, and also looking for a single Identity. - Note: There may be a similar rust discrepancy in the Unity pt1 tutorial. It'll work with Filter, but just simply less consistent. Holding off on that since my Rust syntax knowledge !exists. * fix(Unity-pt1): Rm copy+paste redundant comments * Duplicate comments found both above and within funcs * fix(unity): Rm unused using statement +merged info * Removed `System.Runtime.CompilerServices` * SpacetimeDB.Module seems to already include this (merged the info) * refactor(minor): Code spacing for grouping/clarity * feat: 'Standalone mode runs in foreground' memo * At general quickstart for `spacetime start` * refactor(unity-pt1): Standalone mode foreground memo * Also, removed the "speed" loss mention of C# * fix(syntaxErr): Fix err, keep FilterBy, handle null - After a verbose discussion, we will eventually swap to FindBy for single-result queries, but not in this PR. - For now, the syntax err is fixed by making the var nullable and suffixing a LINQ FirstOrDefault(). Approved by Tyler in Discord. - We never *actually* created a player in the tutorial. This creates the player. Approved by Tyler in Discord. * fix: Remote player `is null` check removal
1 parent 4a50e1c commit ab6b6b4

File tree

8 files changed

+145
-76
lines changed

8 files changed

+145
-76
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ To make changes to our docs, you can open a pull request in this repository. You
1313
git clone ssh://[email protected]/<username>/spacetime-docs
1414
```
1515

16-
3. Make your edits to the docs that you want to make + test them locally (see Testing Your Edits below)
16+
3. Make your edits to the docs that you want to make + test them locally
1717
4. Commit your changes:
1818

1919
```bash

docs/getting-started.md

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,33 @@
22

33
To develop SpacetimeDB applications locally, you will need to run the Standalone version of the server.
44

5-
1. [Install](/install) the SpacetimeDB CLI (Command Line Interface).
6-
2. Run the start command
5+
1. [Install](/install) the SpacetimeDB CLI (Command Line Interface)
6+
2. Run the start command:
77

88
```bash
99
spacetime start
1010
```
1111

12-
The server listens on port `3000` by default. You can change this by using the `--listen-addr` option described below.
12+
The server listens on port `3000` by default, customized via `--listen-addr`.
1313

14-
SSL is not supported in standalone mode.
14+
💡 Standalone mode will run in the foreground.
15+
⚠️ SSL is not supported in standalone mode.
1516

1617
## What's Next?
1718

18-
You are ready to start developing SpacetimeDB modules. We have a quickstart guide for each supported server-side language:
19+
You are ready to start developing SpacetimeDB modules. See below for a quickstart guide for both client and server (module) languages/frameworks.
20+
21+
### Server (Module)
1922

2023
- [Rust](/docs/modules/rust/quickstart)
2124
- [C#](/docs/modules/c-sharp/quickstart)
2225

23-
Then you can write your client application. We have a quickstart guide for each supported client-side language:
26+
**Note:** Rust is [roughly 2x faster](https://faun.dev/c/links/faun/c-vs-rust-vs-go-a-performance-benchmarking-in-kubernetes/) than C#
27+
28+
### Client
2429

2530
- [Rust](/docs/sdks/rust/quickstart)
26-
- [C#](/docs/sdks/c-sharp/quickstart)
31+
- [C# (Standalone)](/docs/sdks/c-sharp/quickstart)
32+
- [C# (Unity)](/docs/unity/part-1)
2733
- [Typescript](/docs/sdks/typescript/quickstart)
28-
- [Python](/docs/sdks/python/quickstart)
29-
30-
We also have a [step-by-step tutorial](/docs/unity/part-1) for building a multiplayer game in Unity3d.
34+
- [Python](/docs/sdks/python/quickstart)

docs/modules/c-sharp/index.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ static partial class Module
4242
{
4343
// We can skip (or explicitly set to zero) auto-incremented fields when creating new rows.
4444
var person = new Person { Name = name, Age = age };
45+
4546
// `Insert()` method is auto-generated and will insert the given row into the table.
4647
person.Insert();
4748
// After insertion, the auto-incremented fields will be populated with their actual values.
@@ -211,8 +212,10 @@ public partial struct Person
211212
212213
// Finds a row in the table with the given value in the `Id` column and returns it, or `null` if no such row exists.
213214
public static Person? FindById(int id);
215+
214216
// Deletes a row in the table with the given value in the `Id` column and returns `true` if the row was found and deleted, or `false` if no such row exists.
215217
public static bool DeleteById(int id);
218+
216219
// Updates a row in the table with the given value in the `Id` column and returns `true` if the row was found and updated, or `false` if no such row exists.
217220
public static bool UpdateById(int oldId, Person newValue);
218221
}

docs/modules/c-sharp/quickstart.md

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,18 @@ If you haven't already, start by [installing SpacetimeDB](/install). This will i
1616

1717
## Install .NET 8
1818

19-
Next we need to [install .NET 8 SDK](https://dotnet.microsoft.com/en-us/download/dotnet/8.0) so that we can build and publish our module. .NET 8.0 is the earliest to have the `wasi-experimental` workload that we rely on.
19+
Next we need to [install .NET 8 SDK](https://dotnet.microsoft.com/en-us/download/dotnet/8.0) so that we can build and publish our module.
20+
21+
You may already have .NET 8 and can be checked:
22+
```bash
23+
dotnet --list-sdks
24+
```
25+
26+
.NET 8.0 is the earliest to have the `wasi-experimental` workload that we rely on, but requires manual activation:
27+
28+
```bash
29+
dotnet workload install wasi-experimental
30+
```
2031

2132
## Project structure
2233

@@ -35,7 +46,11 @@ spacetime init --lang csharp server
3546

3647
## Declare imports
3748

38-
`spacetime init` should have pre-populated `server/Lib.cs` with a trivial module. Clear it out, so we can write a module that's still pretty simple: a bare-bones chat server.
49+
`spacetime init` generated a few files:
50+
51+
1. Open `server/StdbModule.csproj` to generate a .sln file for intellisense/validation support.
52+
2. Open `server/Lib.cs`, a trivial module.
53+
3. Clear it out, so we can write a new module that's still pretty simple: a bare-bones chat server.
3954

4055
To the top of `server/Lib.cs`, add some imports we'll be using:
4156

@@ -45,8 +60,10 @@ using SpacetimeDB.Module;
4560
using static SpacetimeDB.Runtime;
4661
```
4762

48-
- `System.Runtime.CompilerServices` allows us to use the `ModuleInitializer` attribute, which we'll use to register our `OnConnect` and `OnDisconnect` callbacks.
49-
- `SpacetimeDB.Module` contains the special attributes we'll use to define our module.
63+
- `System.Runtime.CompilerServices`
64+
- `SpacetimeDB.Module`
65+
- Contains the special attributes we'll use to define our module.
66+
- Allows us to use the `ModuleInitializer` attribute, which we'll use to register our `OnConnect` and `OnDisconnect` callbacks.
5067
- `SpacetimeDB.Runtime` contains the raw API bindings SpacetimeDB uses to communicate with the database.
5168

5269
We also need to create our static module class which all of the module code will live in. In `server/Lib.cs`, add:
@@ -184,7 +201,7 @@ You could extend the validation in `ValidateMessage` in similar ways to `Validat
184201

185202
In C# modules, you can register for `Connect` and `Disconnect` events by using a special `ReducerKind`. We'll use the `Connect` event to create a `User` record for the client if it doesn't yet exist, and to set its online status.
186203

187-
We'll use `User.FilterByIdentity` to look up a `User` row for `dbEvent.Sender`, if one exists. If we find one, we'll use `User.UpdateByIdentity` to overwrite it with a row that has `Online: true`. If not, we'll use `User.Insert` to insert a new row for our new user. All three of these methods are generated by the `[SpacetimeDB.Table]` attribute, with rows and behavior based on the row attributes. `FilterByIdentity` returns a nullable `User`, because the unique constraint from the `[SpacetimeDB.Column(ColumnAttrs.PrimaryKey)]` attribute means there will be either zero or one matching rows. `Insert` will throw an exception if the insert violates this constraint; if we want to overwrite a `User` row, we need to do so explicitly using `UpdateByIdentity`.
204+
We'll use `User.FindByIdentity` to look up a `User` row for `dbEvent.Sender`, if one exists. If we find one, we'll use `User.UpdateByIdentity` to overwrite it with a row that has `Online: true`. If not, we'll use `User.Insert` to insert a new row for our new user. All three of these methods are generated by the `[SpacetimeDB.Table]` attribute, with rows and behavior based on the row attributes. `FindByIdentity` returns a nullable `User`, because the unique constraint from the `[SpacetimeDB.Column(ColumnAttrs.PrimaryKey)]` attribute means there will be either zero or one matching rows. `Insert` will throw an exception if the insert violates this constraint; if we want to overwrite a `User` row, we need to do so explicitly using `UpdateByIdentity`.
188205

189206
In `server/Lib.cs`, add the definition of the connect reducer to the `Module` class:
190207

@@ -235,7 +252,7 @@ public static void OnDisconnect(DbEventArgs dbEventArgs)
235252
else
236253
{
237254
// User does not exist, log warning
238-
Log($"Warning: No user found for disconnected client.");
255+
Log("Warning: No user found for disconnected client.");
239256
}
240257
}
241258
```
@@ -250,12 +267,16 @@ From the `quickstart-chat` directory, run:
250267
spacetime publish --project-path server <module-name>
251268
```
252269

270+
```bash
271+
npm i wasm-opt -g
272+
```
273+
253274
## Call Reducers
254275

255276
You can use the CLI (command line interface) to run reducers. The arguments to the reducer are passed in JSON format.
256277

257278
```bash
258-
spacetime call <module-name> send_message '["Hello, World!"]'
279+
spacetime call <module-name> send_message "Hello, World!"
259280
```
260281

261282
Once we've called our `send_message` reducer, we can check to make sure it ran by running the `logs` command.
@@ -288,4 +309,4 @@ spacetime sql <module-name> "SELECT * FROM Message"
288309

289310
You've just set up your first database in SpacetimeDB! The next step would be to create a client module that interacts with this module. You can use any of SpacetimDB's supported client languages to do this. Take a look at the quick start guide for your client language of choice: [Rust](/docs/languages/rust/rust-sdk-quickstart-guide), [C#](/docs/languages/csharp/csharp-sdk-quickstart-guide), [TypeScript](/docs/languages/typescript/typescript-sdk-quickstart-guide) or [Python](/docs/languages/python/python-sdk-quickstart-guide).
290311

291-
If you are planning to use SpacetimeDB with the Unity3d game engine, you can skip right to the [Unity Comprehensive Tutorial](/docs/unity/part-1) or check out our example game, [BitcraftMini](/docs/unity/part-3).
312+
If you are planning to use SpacetimeDB with the Unity game engine, you can skip right to the [Unity Comprehensive Tutorial](/docs/unity/part-1) or check out our example game, [BitcraftMini](/docs/unity/part-3).

docs/modules/rust/quickstart.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,4 +269,4 @@ You can find the full code for this module [in the SpacetimeDB module examples](
269269

270270
You've just set up your first database in SpacetimeDB! The next step would be to create a client module that interacts with this module. You can use any of SpacetimDB's supported client languages to do this. Take a look at the quickstart guide for your client language of choice: [Rust](/docs/sdks/rust/quickstart), [C#](/docs/sdks/c-sharp/quickstart), [TypeScript](/docs/sdks/typescript/quickstart) or [Python](/docs/sdks/python/quickstart).
271271

272-
If you are planning to use SpacetimeDB with the Unity3d game engine, you can skip right to the [Unity Comprehensive Tutorial](/docs/unity/part-1) or check out our example game, [BitcraftMini](/docs/unity/part-3).
272+
If you are planning to use SpacetimeDB with the Unity game engine, you can skip right to the [Unity Comprehensive Tutorial](/docs/unity/part-1) or check out our example game, [BitcraftMini](/docs/unity/part-3).

docs/sdks/c-sharp/index.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ The SpacetimeDB client C# for Rust contains all the tools you need to build nati
1717
- [Method `SpacetimeDBClient.Connect`](#method-spacetimedbclientconnect)
1818
- [Event `SpacetimeDBClient.onIdentityReceived`](#event-spacetimedbclientonidentityreceived)
1919
- [Event `SpacetimeDBClient.onConnect`](#event-spacetimedbclientonconnect)
20-
- [Subscribe to queries](#subscribe-to-queries)
20+
- [Query subscriptions & one-time actions](#subscribe-to-queries)
2121
- [Method `SpacetimeDBClient.Subscribe`](#method-spacetimedbclientsubscribe)
2222
- [Event `SpacetimeDBClient.onSubscriptionApplied`](#event-spacetimedbclientonsubscriptionapplied)
23+
- [Method `SpacetimeDBClient.OneOffQuery`](#event-spacetimedbclientoneoffquery)
2324
- [View rows of subscribed tables](#view-rows-of-subscribed-tables)
2425
- [Class `{TABLE}`](#class-table)
2526
- [Static Method `{TABLE}.Iter`](#static-method-tableiter)
@@ -64,13 +65,11 @@ dotnet add package spacetimedbsdk
6465

6566
### Using Unity
6667

67-
To install the SpacetimeDB SDK into a Unity project, download the SpacetimeDB SDK from the following link.
68+
To install the SpacetimeDB SDK into a Unity project, [download the SpacetimeDB SDK](https://github.com/clockworklabs/com.clockworklabs.spacetimedbsdk/releases/latest), packaged as a `.unitypackage`.
6869

69-
https://sdk.spacetimedb.com/SpacetimeDBUnitySDK.unitypackage
70+
In Unity navigate to the `Assets > Import Package > Custom Package` menu in the menu bar. Select your `SpacetimeDB.Unity.Comprehensive.Tutorial.unitypackage` file and leave all folders checked.
7071

71-
In Unity navigate to the `Assets > Import Package > Custom Package...` menu in the menu bar. Select your `SpacetimeDBUnitySDK.unitypackage` file and leave all folders checked.
72-
73-
(See also the [Unity Tutorial](/docs/unity/part-1).)
72+
(See also the [Unity Tutorial](/docs/unity/part-1))
7473

7574
## Generate module bindings
7675

@@ -319,6 +318,15 @@ void Main()
319318
}
320319
```
321320

321+
### Method [`OneTimeQuery`](#method-spacetimedbclientsubscribe)
322+
323+
You may not want to subscribe to a query, but instead want to run a query once and receive the results immediately via a `Task` result:
324+
325+
```csharp
326+
// Query all Messages from the sender "bob"
327+
SpacetimeDBClient.instance.OneOffQuery<Message>("WHERE sender = \"bob\"");
328+
```
329+
322330
## View rows of subscribed tables
323331

324332
The SDK maintains a local view of the database called the "client cache". This cache contains whatever rows are selected via a call to [`SpacetimeDBClient.Subscribe`](#method-spacetimedbclientsubscribe). These rows are represented in the SpacetimeDB .Net SDK as instances of [`SpacetimeDB.Types.{TABLE}`](#class-table).

0 commit comments

Comments
 (0)