Creating the player: an introduction to ECS systems, part 1
With the basics of components covered, let's turn our attention to systems. Systems are responsible for the logic of your game. In a way, you can think of them as entity processors because, most of the time, they take a set of entities as their input and produce a particular result as their output.
The first system we are writing is the one responsible for creating the player entity in our world, which we name, unsurprisingly, PlayerCreationSystem. It is defined as follows:
ECS systems derive from the ComponentSystem class, defined in the Unity.Entities namespace. There are actually several different types of systems available to us, most of which we will cover as we progress through the course. When defining a ComponentSystem, you need to override its OnUpdate method. In this case, with the card creation system being used for bootstrapping purposes only and with no other work to perform on a per-frame basis, the body of the update method is empty. In addition to OnUpdate, you may also override the OnCreate and OnDestroy methods, which are invoked when the system is created and destroyed respectively.
All entities live inside a world and you can create more than one world in your game; for our purposes, it will be enough to use the default world, which is created automatically. The entity manager, which all systems store a reference to, is your entry point to all the entity-related functionality provided by the ECS (creation and destruction of entities, addition and removal of components to/from entities, etc.). We create the entity by calling the entity manager's CreateEntity method and set its PlayerData component via the SetComponentData method. Note how, rather than hardcoding the player's health value in this file, we use an auxiliary utility class named GameplayConstants:
If you are wondering why the maximum player HP is not a constant, it is because it can be modified by an ability card; we will cover special cards later in the course.
Finally, with this system being used exclusively for bootstrapping purposes, we disable it right after creating the player entity by setting its Enabled property to false.