|
@@ -16,10 +16,10 @@ Example:
|
|
|
```cs
|
|
```cs
|
|
|
public class StartUpScriptExample : StartupScript
|
|
public class StartUpScriptExample : StartupScript
|
|
|
{
|
|
{
|
|
|
- public override void Start()
|
|
|
|
|
- {
|
|
|
|
|
- // Do some stuff during initialization
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ public override void Start()
|
|
|
|
|
+ {
|
|
|
|
|
+ // Do some stuff during initialization
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
```
|
|
```
|
|
|
|
|
|
|
@@ -36,10 +36,10 @@ The following script performs updates every frame, no matter what:
|
|
|
```cs
|
|
```cs
|
|
|
public class SampleSyncScript : SyncScript
|
|
public class SampleSyncScript : SyncScript
|
|
|
{
|
|
{
|
|
|
- public override void Update()
|
|
|
|
|
- {
|
|
|
|
|
- // Performs the update on the entity — this code is executed every frame
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ public override void Update()
|
|
|
|
|
+ {
|
|
|
|
|
+ // Performs the update on the entity — this code is executed every frame
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
```
|
|
```
|
|
|
|
|
|
|
@@ -48,7 +48,6 @@ public class SampleSyncScript : SyncScript
|
|
|
Asynchronous scripts are initialized only once, then canceled when removed from the scene.
|
|
Asynchronous scripts are initialized only once, then canceled when removed from the scene.
|
|
|
|
|
|
|
|
* Asynchronous code goes in the [Execute](xref:Stride.Engine.AsyncScript.Execute) function.
|
|
* Asynchronous code goes in the [Execute](xref:Stride.Engine.AsyncScript.Execute) function.
|
|
|
-
|
|
|
|
|
* Code performing the cancellation goes in the [Cancel](xref:Stride.Engine.ScriptComponent.Cancel) method.
|
|
* Code performing the cancellation goes in the [Cancel](xref:Stride.Engine.ScriptComponent.Cancel) method.
|
|
|
|
|
|
|
|
The following script performs actions that depend on events and triggers:
|
|
The following script performs actions that depend on events and triggers:
|
|
@@ -56,34 +55,38 @@ The following script performs actions that depend on events and triggers:
|
|
|
```cs
|
|
```cs
|
|
|
public class SampleAsyncScript : AsyncScript
|
|
public class SampleAsyncScript : AsyncScript
|
|
|
{
|
|
{
|
|
|
- public override async Task Execute()
|
|
|
|
|
- {
|
|
|
|
|
- // The initialization code should come here, if necessary
|
|
|
|
|
- // This method starts running on the main thread
|
|
|
|
|
-
|
|
|
|
|
- while (Game.IsRunning) // loop until the game ends (optional depending on the script)
|
|
|
|
|
- {
|
|
|
|
|
- // We're still on the main thread
|
|
|
|
|
-
|
|
|
|
|
- // Task.Run will pause the execution of this method until the task is completed,
|
|
|
|
|
- // while that's going on, the game will continue running, it will display new frames and process inputs appropriately
|
|
|
|
|
- var lobbies = await Task.Run(() => GetMultiplayerLobbies());
|
|
|
|
|
-
|
|
|
|
|
- // After awaiting a task, the thread the method runs on will have changed, this method now runs on a thread pool thread instead of the main thread
|
|
|
|
|
- // You can manipulate the data returned by the task here if needed
|
|
|
|
|
- // But if you want to interact with the engine safely, you have to make sure the method runs on the main thread
|
|
|
|
|
-
|
|
|
|
|
- // await Script.NextFrame() yields execution of this method to the main thread, meaning that this method is paused, and once the main thread processes the next frame,
|
|
|
|
|
- // it will pick that method up and run it
|
|
|
|
|
- await Script.NextFrame();
|
|
|
|
|
- // So after this call, this method is back on the main thread
|
|
|
|
|
-
|
|
|
|
|
- // You can now safely interact with the engine's systems by displaying the lobbies retrieved above in a UI for example
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ public override async Task Execute()
|
|
|
|
|
+ {
|
|
|
|
|
+ // The initialization code should come here, if necessary
|
|
|
|
|
+ // This method starts running on the main thread
|
|
|
|
|
+
|
|
|
|
|
+ while (Game.IsRunning) // loop until the game ends (optionalpendingon the script)
|
|
|
|
|
+ {
|
|
|
|
|
+ // We're still on the main thread
|
|
|
|
|
+
|
|
|
|
|
+ // Task.Run will pause the execution of this method until the task is completed,
|
|
|
|
|
+ // while that's going on, the game will continue running, it will display new frames and process inputs appropriately
|
|
|
|
|
+ var lobbies = await Task.Run(() => GetMultiplayerLobbies());
|
|
|
|
|
+
|
|
|
|
|
+ // After awaiting a task, the thread the method runs on will have changed,
|
|
|
|
|
+ // this method now runs on a thread pool thread instead of the main thread
|
|
|
|
|
+ // You can manipulate the data returned by the task here if needed
|
|
|
|
|
+ // But if you want to interact with the engine safely, you have to make sure the method runs on the main thread
|
|
|
|
|
+
|
|
|
|
|
+ // await Script.NextFrame() yields execution of this method to the main thread,
|
|
|
|
|
+ // meaning that this method is paused, and once the main thread processes the next frame,
|
|
|
|
|
+ // it will pick that method up and run it
|
|
|
|
|
+ await Script.NextFrame();
|
|
|
|
|
+ // So after this call, this method is back on the main thread
|
|
|
|
|
+
|
|
|
|
|
+ // You can now safely interact with the engine's systems by displaying the lobbies retrieved above in a UI for example
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
|
|
+Check out an example from our [Async scripts tutorial](../../tutorials/csharpintermediate/async-scripts.md).
|
|
|
|
|
+
|
|
|
## See also
|
|
## See also
|
|
|
|
|
|
|
|
* [Create a script](create-a-script.md)
|
|
* [Create a script](create-a-script.md)
|