You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* expanded on taggedenums and added examples for each special ReducerKind
Fixed a few typos/mistakes here and there also.
* fixed part2 hyperlinks
* fixed config version type
from Identity to uint
* update Throw => throw
* update log typo
* fix type on connect reducerkind from init=>connect
* private=>public for UpdatePlayerLoginState reducer
* remove double "publish" condenses it into one publish at the end after chat
* fixed name of GameManager file, tweaks to instructions
kept application.runInBackground (it wasn't included)
renamed many instances of "TutorialGameManager.cs" to "BitcraftMiniGameManager.cs" to represent accurate filename
* fixed onConnectError
* more TutorialGameManager renames to BitcraftMiniGameManager.cs and also a FilterByX fix
* added clarity to UIUsernameChooser.cs and LocalPlayer.cs -- Also fixed RemotePlayer.cs errors
* some small tweaks again to GameManager name
* updated tagged enums to reflect record usage and pattern matching
* filter -> find fixes
* expanded on taggedenums and added examples for each special ReducerKind
Fixed a few typos/mistakes here and there also.
* fixed config version type
from Identity to uint
* update Throw => throw
* update log typo
* fix type on connect reducerkind from init=>connect
* private=>public for UpdatePlayerLoginState reducer
* remove double "publish" condenses it into one publish at the end after chat
* fixed name of GameManager file, tweaks to instructions
kept application.runInBackground (it wasn't included)
renamed many instances of "TutorialGameManager.cs" to "BitcraftMiniGameManager.cs" to represent accurate filename
* fixed onConnectError
* more TutorialGameManager renames to BitcraftMiniGameManager.cs and also a FilterByX fix
* added clarity to UIUsernameChooser.cs and LocalPlayer.cs -- Also fixed RemotePlayer.cs errors
* some small tweaks again to GameManager name
* updated tagged enums to reflect record usage and pattern matching
* filter -> find fixes
* updated based on feedback
Copy file name to clipboardExpand all lines: docs/docs/modules/c-sharp/index.md
+19-2Lines changed: 19 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -321,7 +321,7 @@ public static void AddIn5Minutes(ReducerContext e, string name, int age)
321
321
322
322
#### Special reducers
323
323
324
-
These are two special kinds of reducers that can be used to respond to module lifecycle events. They're stored in the `SpacetimeDB.Module.ReducerKind` class and can be used as an argument to the `[SpacetimeDB.Reducer]` attribute:
324
+
These are four special kinds of reducers that can be used to respond to module lifecycle events. They're stored in the `SpacetimeDB.Module.ReducerKind` class and can be used as an argument to the `[SpacetimeDB.Reducer]` attribute:
325
325
326
326
-`ReducerKind.Init` - this reducer will be invoked when the module is first published.
327
327
-`ReducerKind.Update` - this reducer will be invoked when the module is updated.
@@ -337,4 +337,21 @@ public static void Init()
337
337
{
338
338
Log("...and we're live!");
339
339
}
340
-
```
340
+
341
+
[SpacetimeDB.Reducer(ReducerKind.Update)]
342
+
publicstaticvoidUpdate()
343
+
{
344
+
Log("Update get!");
345
+
}
346
+
347
+
[SpacetimeDB.Reducer(ReducerKind.Connect)]
348
+
publicstaticvoidOnConnect(DbEventArgsctx)
349
+
{
350
+
Log($"{ctx.Sender} has connected from {ctx.Address}!");
Log("Error: Failed to create a unique PlayerComponent", LogLevel.Error);
137
-
Throw;
136
+
Log("Error: Failed to create a unique EntityComponent", LogLevel.Error);
137
+
throw;
138
138
}
139
139
140
140
// The PlayerComponent uses the same entity_id and stores the identity of
@@ -275,15 +275,6 @@ In a fully developed game, the server would typically perform server-side valida
275
275
276
276
---
277
277
278
-
### Publishing a Module to SpacetimeDB
279
-
280
-
Now that we've written the code for our server module and reached a clean checkpoint, we need to publish it to SpacetimeDB. This will create the database and call the init reducer. In your terminal or command window, run the following commands.
281
-
282
-
```bash
283
-
cd server
284
-
spacetime publish -c unity-tutorial
285
-
```
286
-
287
278
### Finally, Add Chat Support
288
279
289
280
The client project has a chat window, but so far, all it's used for is the message of the day. We are going to add the ability for players to send chat messages to each other.
💡View the [entire lib.cs file](https://gist.github.com/dylanh724/68067b4e843ea6e99fbd297fe1a87c49)
339
331
340
-
Now that we added chat support, let's publish the latest module version to SpacetimeDB, assuming we're still in the `server` dir:
332
+
Now that we've written the code for our server module and reached a clean checkpoint, we need to publish it to SpacetimeDB. This will create the database and call the init reducer. In your terminal or command window, run the following commands.
Next we are going to connect to our SpacetimeDB module. Open `TutorialGameManager.cs` in your editor of choice and add the following code at the top of the file:
37
+
Next we are going to connect to our SpacetimeDB module. Open `Assets/_Project/Game/BitcraftMiniGameManager.cs` in your editor of choice and add the following code at the top of the file:
38
38
39
-
**Append to the top of TutorialGameManager.cs**
39
+
**Append to the top of BitcraftMiniGameManager.cs**
40
40
41
41
```csharp
42
42
usingSpacetimeDB;
@@ -46,7 +46,7 @@ using System.Linq;
46
46
47
47
At the top of the class definition add the following members:
48
48
49
-
**Append to the top of TutorialGameManager class inside of TutorialGameManager.cs**
49
+
**Append to the top of BitcraftMiniGameManager class inside of BitcraftMiniGameManager.cs**
50
50
51
51
```csharp
52
52
// These are connection variables that are exposed on the GameManager
@@ -64,14 +64,16 @@ The first three fields will appear in your Inspector so you can update your conn
64
64
65
65
Now add the following code to the `Start()` function. For clarity, replace your entire `Start()` function with the function below.
66
66
67
-
**REPLACE the Start() function in TutorialGameManager.cs**
67
+
**REPLACE the Start() function in BitcraftMiniGameManager.cs**
68
68
69
69
```csharp
70
70
// Start is called before the first frame update
71
71
voidStart()
72
72
{
73
73
instance=this;
74
74
75
+
Application.runInBackground=true;
76
+
75
77
SpacetimeDBClient.instance.onConnect+= () =>
76
78
{
77
79
Debug.Log("Connected.");
@@ -86,7 +88,7 @@ void Start()
86
88
// Called when we have an error connecting to SpacetimeDB
// Called when we are disconnected from SpacetimeDB
@@ -123,7 +125,7 @@ The "local client cache" is a client-side view of the database defined by the su
123
125
124
126
Next we write the `OnSubscriptionApplied` callback. When this event occurs for the first time, it signifies that our local client cache is fully populated. At this point, we can verify if a player entity already exists for the corresponding user. If we do not have a player entity, we need to show the `UserNameChooser` dialog so the user can enter a username. We also put the message of the day into the chat window. Finally we unsubscribe from the callback since we only need to do this once.
125
127
126
-
**Append after the Start() function in TutorialGameManager.cs**
128
+
**Append after the Start() function in BitcraftMiniGameManager.cs**
127
129
128
130
```csharp
129
131
voidOnSubscriptionApplied()
@@ -148,7 +150,7 @@ void OnSubscriptionApplied()
148
150
149
151
### Adding the Multiplayer Functionality
150
152
151
-
Now we have to change what happens when you press the "Continue" button in the name dialog window. Instead of calling start game like we did in the single player version, we call the `create_player` reducer on the SpacetimeDB module using the auto-generated code. Open `UIUsernameChooser.cs`.
153
+
Now we have to change what happens when you press the "Continue" button in the name dialog window. Instead of calling start game like we did in the single player version, we call the `create_player` reducer on the SpacetimeDB module using the auto-generated code. Open `Assets/_Project/Username/UIUsernameChooser.cs`.
152
154
153
155
**Append to the top of UIUsernameChooser.cs**
154
156
@@ -171,7 +173,7 @@ public void ButtonPressed()
171
173
}
172
174
```
173
175
174
-
We need to create a `RemotePlayer` script that we attach to remote player objects. In the same folder as `LocalPlayer.cs`, create a new C# script called `RemotePlayer`. In the start function, we will register an OnUpdate callback for the `EntityComponent` and query the local cache to get the player’s initial position. **Make sure you include a `using SpacetimeDB.Types;`** at the top of the file.
176
+
We need to create a `RemotePlayer` script that we attach to remote player objects. In the same folder as `Assets/_Project/Player/LocalPlayer.cs`, create a new C# script called `RemotePlayer`. In the start function, we will register an OnUpdate callback for the `EntityComponent` and query the local cache to get the player’s initial position. **Make sure you include a `using SpacetimeDB.Types;`** at the top of the file.
175
177
176
178
First append this using to the top of `RemotePlayer.cs`
177
179
@@ -203,7 +205,7 @@ public class RemotePlayer : MonoBehaviour
Debug.Log($"PlayerComponent not found - Creating a new player ({inputUsername})");
208
210
Reducer.CreatePlayer(inputUsername);
209
211
@@ -246,21 +248,21 @@ private void EntityComponent_OnUpdate(EntityComponent oldObj, EntityComponent ob
246
248
247
249
Next we need to handle what happens when a `PlayerComponent` is added to our local cache. We will handle it differently based on if it’s our local player entity or a remote player. We are going to register for the `OnInsert` event for our `PlayerComponent` table. Add the following code to the `Start` function in `TutorialGameManager`.
248
250
249
-
**Append to bottom of Start() function in TutorialGameManager.cs:**
251
+
**Append to bottom of Start() function in BitcraftMiniGameManager.cs:**
Create the `PlayerComponent_OnInsert` function which does something different depending on if it's the component for the local player or a remote player. If it's the local player, we set the local player object's initial position and call `StartGame`. If it's a remote player, we instantiate a `PlayerPrefab` with the `RemotePlayer` component. The start function of `RemotePlayer` handles initializing the player position.
256
258
257
-
**Append to bottom of TutorialGameManager class in TutorialGameManager.cs:**
259
+
**Append to bottom of TutorialGameManager class in BitcraftMiniGameManager.cs:**
0 commit comments