Skip to content

Commit bb86b73

Browse files
authored
Make async tutorial more complete, and add an example of failure (#98)
1 parent 14671ae commit bb86b73

File tree

2 files changed

+54
-27
lines changed

2 files changed

+54
-27
lines changed

src/app/start/tutorials/unity/moving-block/moving-block.component.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -562,8 +562,7 @@ <h2 id="conclusion">Conclusion</h2>
562562
to try things out for yourself.
563563
</p>
564564
<p>
565-
If you're interested in making multiplayer games, check out the last and
566-
much shorter
565+
If you're interested in making multiplayer games, check out the much shorter
567566
<a href="start/tutorials/unity/using-async">using-async</a> tutorial.
568567
</p>
569568
</article>

src/app/start/tutorials/unity/using-async/using-async.component.html

Lines changed: 53 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -45,46 +45,74 @@ <h2 id="Async Menu">Async Menu</h2>
4545
</p>
4646
<code-block-variation>
4747
<pre codeBlockVariationOption><code prism language="csharp">
48-
using System.Collections.Generic;
49-
using UnityEngine;
50-
using UnityEngine.UI;
51-
using UnityEngine.SceneManagement;
52-
using TMPro;
48+
using System.Collections.Generic;
49+
using UnityEngine;
50+
using UnityEngine.UI;
51+
using UnityEngine.SceneManagement;
52+
using TMPro;
5353

54-
public class AsyncMenu : MonoBehaviour {{'{'}}
54+
public class AsyncMenu : MonoBehaviour {{'{'}}
5555

56-
public Button? connectBtn;
57-
public TMP_Dropdown? dropdown;
56+
public Button connectBtn;
57+
public TMP_Dropdown dropdown;
58+
public TMP_InputField connectionString;
5859

59-
private List<{{'string'}}> options = new();
60+
private List<{{'string'}}> options = new();
6061

61-
void OnEnable() {{'{'}}
62-
options.Add("BasicExample");
63-
options.Add("MovingBlock");
62+
void OnEnable() {{'{'}}
63+
options.Add("BasicExample");
64+
options.Add("MovingBlock");
6465

65-
dropdown.AddOptions(options);
66+
dropdown.AddOptions(options);
6667

67-
connectBtn.onClick.AddListener(onButtonClick);
68-
{{'}'}}
68+
// This connection string will start a connection with 20ms delta time
69+
// At 20ms, systems will execute 50 times/sec (1000ms / 20ms)
70+
connectionString.text = "good?delta_time=20";
6971

70-
void onButtonClick() {{'{'}}
71-
var sceneName = options[dropdown.value];
72-
Debug.Log("Loading scene " + sceneName);
72+
connectBtn.onClick.AddListener(onButtonClick);
7373

74-
SceneManager.LoadScene(sceneName);
74+
// Callback invoked when the Defaults class is initialized
75+
Ecsact.Defaults.WhenReady(() => {{'{'}}
76+
Ecsact.Defaults.Runtime.async.OnAsyncError((err, requestIds) => {{'{'}}
77+
Debug.Log("Error: " + err);
78+
{{'}'}});
79+
{{'}'}});
7580

76-
var millisecondTime = Time.fixedDeltaTime * 1000;
81+
// Keeps the OnAsyncError listener alive when loading new scene
82+
Object.DontDestroyOnLoad(this.gameObject);
83+
{{'}'}}
7784

78-
Debug.Log("Loading with time(ms): " + millisecondTime);
85+
void onButtonClick() {{'{'}}
86+
var sceneName = options[dropdown.value];
87+
Debug.Log("Loading scene " + sceneName);
88+
SceneManager.LoadScene(sceneName);
89+
90+
Debug.Log("connecting with: " + connectionString.text);
91+
92+
Ecsact.Defaults.Runtime.async.Connect(connectionString.text);
93+
94+
this.gameObject.SetActive(false);
95+
{{'}'}}
96+
{{'}'}}
7997

80-
Ecsact.Defaults.Runtime.async.Connect("good?delta_speed="+millisecondTime);
81-
{{'}'}}
82-
{{'}'}}
8398
</code></pre>
8499
</code-block-variation>
85100
<p>
86101
That's it! Load your scenes and try different delta speeds to try out how
87-
async works.
102+
async works. See what happens if you give it an invalid connection string
103+
too.
88104
</p>
89105
</section>
106+
<h2 id="conclusion">Conclusion</h2>
107+
<p>
108+
As long as you handle connecting and disconnecting, the default and async
109+
runners function the same way and can be interchanged. Just remember that
110+
the <a href="/docs/unity/defaults">Defaults Registry</a> is
111+
<code>NULL</code>
112+
</p>
113+
<p>
114+
If you haven't already, check out both the
115+
<a href="start/tutorials/unity/basic-example">basic-example</a> and
116+
<a href="start/tutorials/unity/moving-block">moving-block</a> tutorials.
117+
</p>
90118
</article>

0 commit comments

Comments
 (0)