CodeBlanks Logo Icon CodeBlanks

Challenge 2: ECS component

Write a component struct derived from IComponentData.

What is IComponentData?

In the ECS architecture components hold only data; game logic lives in systems. A component is defined with a struct that implements the IComponentData interface; a value type is used instead of a class because data is stored more efficiently this way. Inside a component only fields are found, never methods.

public struct HareketBileseni : IComponentData
{
    public float hiz;
}

The component above carries only the hiz data.

Practice Movement component

Define a data component named HareketBileseni with a hiz field; the component should be a structure that implements the IComponentData interface. In the Start method of the class BilesenTest, create a variable of that type, assign the value 5f to its hiz field and print the text "Hiz: " joined with the hiz field using Debug.Log.

Starter code

Player.cs
using Unity.Entities;
using UnityEngine;

public class BilesenTest : MonoBehaviour
{
    void Start()
    {
        // HareketBileseni tipinde bir degisken olustur
        // hiz alanina 5f degerini ata
        // "Hiz: " metnini hiz alaniyla birlestirip yazdir
    }
}

// HareketBileseni adli veri bilesenini tanimla: IComponentData'yi uygulayan bir yapi olsun
// icinde hiz adli ondalik alan bulunsun

Sample solution

Player.cs
using Unity.Entities;
using UnityEngine;

public struct HareketBileseni : IComponentData
{
    public float hiz;
}

public class BilesenTest : MonoBehaviour
{
    void Start()
    {
        HareketBileseni h = new HareketBileseni();
        h.hiz = 5f;
        Debug.Log("Hiz: " + h.hiz);
    }
}

Expected output

Hiz: 5

Practice Health component

Define a data component named YasamBileseni with a can field; the component should be a structure that implements the IComponentData interface. In the Start method of the class YasamTest, create a variable of that type, assign the value 50 to its can field and print the text "Can: " joined with the can field using Debug.Log.

Starter code

Player.cs
using Unity.Entities;
using UnityEngine;

public class YasamTest : MonoBehaviour
{
    void Start()
    {
        // YasamBileseni tipinde bir degisken olustur
        // can alanina 50 degerini ata
        // "Can: " metnini can alaniyla birlestirip yazdir
    }
}

// YasamBileseni adli veri bilesenini tanimla: IComponentData'yi uygulayan bir yapi olsun
// icinde can adli tam sayi alan bulunsun

Sample solution

Player.cs
using Unity.Entities;
using UnityEngine;

public struct YasamBileseni : IComponentData
{
    public int can;
}

public class YasamTest : MonoBehaviour
{
    void Start()
    {
        YasamBileseni y = new YasamBileseni();
        y.can = 50;
        Debug.Log("Can: " + y.can);
    }
}

Expected output

Can: 50

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 With which keyword is an IComponentData derived data component defined?

  • With enum
  • With struct Correct answer
  • With class
  • With interface

Explanation: ECS components are defined with the value type struct; struct is used instead of class.

2 Define the component

Question
public ____ HareketBileseni : IComponentData
Answer
public struct HareketBileseni : IComponentData

Explanation: Because ECS components are value types they are defined with the struct keyword.

3 ECS component terms

Term Meaning
IComponentData The interface that defines a component
Bileşen A structure that holds only data
Entity The identity that components attach to
Alan The data record inside a component

All lessons in Expert