Hi all,
I am new to Unity and I only code as a hobby, so please be very descriptive about any help you provide: I am by no means a professional programmer.
I have been trying for weeks now to put together the first few bricks of a video game I am making on my own.
Since I am new to Unity I thought I would get straight into good habits by coding using Hybrid or Pure ECS (Hybrid seems to be a lot more flexible and accessible). After going through a significant amount of Unity documentation (this stuff can be seriously hard to read and not user-friendly unfortunately), forums and youtube tutorials and trying things out in Unity, I came to the realization that ECS coding has been constantly changing for at least 2-3 years and that a good 90% of the help material put online is either deprecated or on the way to be deprecated (e.g., JobSystem, ComponentSystem). Even more frustration came when I starting finding some incoherences in the official Unity ECS documentations regarding the number of components taken by Entities.ForEach (6? 8? who knows…). I don’t blame Unity, this is just factual but it is frustrating, especially far a new user such as myself
Ok, long story short, my adaptation of a (very simple in principle) Hybrid ECS code from a youtube tuto (
) does not work and I have tried so many things now that I do not know what to do anymore… Help would be very much appreciated!
All details are below:
The error I get:
error DC0023: Entities.ForEach uses managed IComponentData MovementSpeed&. This is only supported when using .WithoutBurst() and .Run().
My loaded packages (the key ones only):
- Entities
- Hybrid Renderer
- Jobs
- Burst
My Scene content:
- Main Camera (GameObject)
- Directional Light (GameObject)
- Plane/Ground (GameObject converted into an Entity at runtime using the built-in script set on Convert&Destroy)
- Cube/Player (GameObject converted into an Entity at runtime using the built-in script set on Convert&Destroy)
The following ECS Components have been added to the Cube (Hybrid ECS so these are MonoBehaviour, I will also try in Pure ECS tonight):
using UnityEngine;
namespace HybridECS.Components
{
public class MovementSpeed : MonoBehaviour
{
public float value;
}
}
And here is my ECS System (using SystemBase, since the other ways are to be deprecated soon):
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Jobs;
using Unity.Mathematics; // to use math
using Unity.Transforms; // to use Translation (= Transform in ECS???)
using UnityEngine;
namespace HybridECS.Systems
{
public class PlayerMovementSystem : SystemBase // ComponentSystem is being deprecated
{
protected override void OnUpdate()
{
// Local variables used in Entities.ForEach should be defined beforehand
float deltaTime = Time.DeltaTime;
// filters the entities based on theirs associated components, which will be included in/affected by this ECS System
// can take up to 8 types of components
// add the "ref" keyword to read&write the component; add the "in" keyword to read-only the component
Entities.ForEach((ref Translation translation, in Rotation rotation, in HybridECS.Components.MovementSpeed speed) => {
// write the system behaviour here, example of movement script:
translation.Value.x += speed.value * deltaTime;
}).Schedule();
}
}
}
If I cannot figure out how to do things in ECS I might just do everything using Monobehaviours, but it would be very disappointing for me who enjoys coding not just to make my own small video games but also to simply keep learning new things and ways of thinking.
Thanks very much in advance for your help!