In WaveEngine the use of Services can be very useful when developing your applications / games. And, as you can create and register your own services you can add cross-scene and global elements that provide useful logic, like score managers, entry points to native libraries, and much more. However, until now, Visual Editor didn’t allow you to register your own services, so you had to do that from code. That could cause problems: Creating components that use custom services could be tricky to debug in our Visual Editor, or depending on your code, it may directly provoke malfunctions in your scene.
Luckily, WaveEngine 2.40 has introduced the possibility of registering and setting up your custom services, as well as some Wave Extension services.
Creating a new Service class.
Creating a new Service is almost exactly like in the previous version. The only difference is that you must make the class Serializable if you want to add it to the Visual Editor.
For example, we may want to create a new service called ScoreManager, so we may create this class in our project:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
using System.Runtime.Serialization; using WaveEngine.Common; using WaveEngine.Common.Attributes; namespace TestServices { [DataContract] public class ScoreManagerService : Service { [DataMember] public int PointsPerScore { get; set; } [DontRenderProperty] public int Score { get; private set; } protected override void DefaultValues() { base.DefaultValues(); this.PointsPerScore = 3; this.Score = 0; } public void Reset() { this.Score = 0; } public void NewScore() { this.Score += this.PointsPerScore; } } } |
This simple service keeps the score and has the property indicating the points per score. Notice the [DataContract] and [DataMember] attributes to make it serializable.
Registering services in Editor
Adding your service from Visual Editor is quite simple, just go to Project Settings… and click on the (new) Services area.
Once in the Services settings area, you can add and remove services by clicking on the and buttons.
By clicking on the button, a dialog will appear showing the service to be registered.
In this class list you can see the ScoreManagerService class that we added before in our project. Clicking on the OK button will add the service to your project.
Additionally, you can select the service and set its configuration properties, in this case the PointsPerScore property.
Using Services from C#
The services added in the Visual Studio are stored in a new file with the .wgame extension in the same folder and with the same name as the .weproj file. This file will store all the project properties. For now it only stores the registered services, but in the future version, we are planning to add more and more settings there.
This file is automatically loaded in the Initialize method from the Game class in the new project template:
1 2 3 4 5 6 7 8 9 |
public override void Initialize(IApplication application) { base.Initialize(application); this.Load(WaveContent.GameInfo); ScreenContext screenContext = new ScreenContext(new MyScene()); WaveServices.ScreenContextManager.To(screenContext); } |
In that method you can see the following line:
1 |
this.Load(WaveContent.GameInfo); |
Which loads the Game Info file (.wgame) and reads and registers the contained services.
Old Wave Versions
If you are migrating an old Wave Engine project to this version, you will need to manually add that Load method, as shown before.