Defining the basic player data: an introduction to ECS entities and components, part 1

The player in our game is an ECS entity, with the PlayerData component attached to it:

Please note that, throughout the course, we will assume any script we create has the same name as the class contained within. For example, in this case, the script will be named PlayerData.cs.

As you can see in the previous fragment of code, ECS components are plain structs implementing the IComponentData interface. This interface lives in the Unity.Entities namespace. Note how we are putting all of our code inside the CCG namespace. This is a good practice to prevent name clashes when using third-party code.

The PlayerData struct stores two integers:

  • Hp: the player's current health.
  • Coins: the player's current number of coins.

As we will see during the course, the ECS is all about structs and value types rather than classes and reference types (so common in traditional object-oriented code). This is one of the main reasons behind its "performance by default" motto.

Complete and Continue