Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ScriptSystem.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Threading.Tasks;
5 
6 using SiliconStudio.Core;
7 using SiliconStudio.Core.Serialization.Assets;
8 using SiliconStudio.Paradox.Games;
9 using SiliconStudio.Core.MicroThreading;
10 
11 namespace SiliconStudio.Paradox
12 {
13  /// <summary>
14  /// The script system handles scripts scheduling in a game.
15  /// </summary>
16  public sealed class ScriptSystem : GameSystemBase
17  {
18  /// <summary>
19  /// Gets the scheduler.
20  /// </summary>
21  /// <value>The scheduler.</value>
22  public Scheduler Scheduler { get; private set; }
23 
24  /// <summary>
25  /// Initializes a new instance of the <see cref="GameSystemBase" /> class.
26  /// </summary>
27  /// <param name="registry">The registry.</param>
28  /// <remarks>The GameSystem is expecting the following services to be registered: <see cref="IGame" /> and <see cref="AssetManager" />.</remarks>
29  public ScriptSystem(IServiceRegistry registry)
30  : base(registry)
31  {
32  Enabled = true;
33  Scheduler = new Scheduler();
34  Services.AddService(typeof(ScriptSystem), this);
35  }
36 
37  public override void Update(GameTime gameTime)
38  {
39  Scheduler.Run();
40  }
41 
42  /// <summary>
43  /// Allows to wait for next frame.
44  /// </summary>
45  /// <returns>ChannelMicroThreadAwaiter&lt;System.Int32&gt;.</returns>
46  public ChannelMicroThreadAwaiter<int> NextFrame()
47  {
48  return Scheduler.NextFrame();
49  }
50 
51  /// <summary>
52  /// Adds the specified micro thread function.
53  /// </summary>
54  /// <param name="microThreadFunction">The micro thread function.</param>
55  /// <returns>MicroThread.</returns>
56  public MicroThread Add(Func<Task> microThreadFunction)
57  {
58  return Scheduler.Add(microThreadFunction);
59  }
60 
61  /// <summary>
62  /// Adds the specified script.
63  /// </summary>
64  /// <param name="script">The script.</param>
65  /// <returns>MicroThread.</returns>
66  public MicroThread Add(IScript script)
67  {
68  return Scheduler.Add(script.Execute);
69  }
70 
71  /// <summary>
72  /// Waits all micro thread finished their task completion.
73  /// </summary>
74  /// <param name="microThreads">The micro threads.</param>
75  /// <returns>Task.</returns>
76  public async Task WhenAll(params MicroThread[] microThreads)
77  {
78  await Scheduler.WhenAll(microThreads);
79  }
80  }
81 }
Represents an execution context managed by a Scheduler, that can cooperatively yield execution to ano...
Definition: MicroThread.cs:16
A service registry is a IServiceProvider that provides methods to register and unregister services...
The script system handles scripts scheduling in a game.
Definition: ScriptSystem.cs:16
Base class for a GameSystemBase component.
async Task WhenAll(params MicroThread[] microThreads)
Waits all micro thread finished their task completion.
Definition: ScriptSystem.cs:76
Current timing used for variable-step (real time) or fixed-step (game time) games.
Definition: GameTime.cs:31
MicroThread Add(IScript script)
Adds the specified script.
Definition: ScriptSystem.cs:66
MicroThread Add(Func< Task > microThreadFunction)
Adds the specified micro thread function.
Definition: ScriptSystem.cs:56
ChannelMicroThreadAwaiter< int > NextFrame()
Allows to wait for next frame.
Definition: ScriptSystem.cs:46
override void Update(GameTime gameTime)
This method is called when this game component is updated.
Definition: ScriptSystem.cs:37
Scheduler that manage a group of cooperating MicroThread.
Definition: Scheduler.cs:20
ScriptSystem(IServiceRegistry registry)
Initializes a new instance of the GameSystemBase class.
Definition: ScriptSystem.cs:29