Instantiate Prefab in Entities 1.0.0

I’m studying Entities 1.0.0 and stopped at the spawn of an object on the stage, I just can’t understand even from examples, I get confused when I try to write it myself.
i created prefab and spawner components, i made prefabs out of them
Prefab

public struct Character_DataTransform : IComponentData{
    public float3 Position;
    public float3 Rotation;
    public float3 Scale;
    }

public class CharacterPrefab : MonoBehaviour{
    public float3 Position;
    public float3 Rotation;
    public float3 Scale;
    }

public class CharacterPrefab_Baker : Baker<CharacterPrefab>{
    public override void Bake(CharacterPrefab authoring){
        AddComponent<Character_DataTransform>();
        Debug.Log("CharacterPrefab_Baker is called");
        }
    }

Spawner

public struct CharacterSpawner_Component : IComponentData{
    public Entity SpawnPrefab;
    public float3 SpawnPosition;
    }
  
public class CharacterSpawner : MonoBehaviour{
    public GameObject SpawnPrefab;
    public float3 SpawnPosition;
    }

public class CharacterSpawner_Baker : Baker<CharacterSpawner>{
    public override void Bake(CharacterSpawner authoring){
        CharacterSpawner_Component sc = default;
        sc.SpawnPrefab = GetEntity(authoring.SpawnPrefab);
        sc.SpawnPosition = authoring.SpawnPosition;
        AddComponent(sc);
        }
    }

System?
How do I spawn an object at the point I need?
(confused with examples and can’t recreate…)

Have look at my tutorial.
And let me know if you still can’t make it.

I watched this video and tried the script itself from this video, the script works, but I got confused in it while studying it…
I need to understand the principle of the object spawn itself, I still don’t understand why it is created in the update, it is that each frame is created and deleted. can’t find a clear description about it…

You should look into helo cube samples of Unity ECS Samples at github.
There is tons of resources to study for someone who is just starting off.

Your have a spawner entity that holds a prefab entity reference in a component.

The system loops over every spawner and spawns the prefab entity.
The it destroys the spawner, or remove the component that contains the prefab reference, so that in the next frame that same spawner is no longer spawn a new prefab instance.

Does that make sens ?

I seem to do everything as in the example, but I get an error

using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
[BurstCompile]
public partial struct SystemSpawn : ISystem{

        EntityQuery SpawnQuery;
        uint counter;

        [BurstCompile]
        public void OnCreate(ref SystemState state){
            state.RequireForUpdate<Execute>();
            state.RequireForUpdate<CharacterSpawner>();
            var q = new EntityQueryBuilder(Allocator.Temp);
            q.WithAll<CharacterSpawner_Component>();
            SpawnQuery = state.GetEntityQuery(q);
            }

        [BurstCompile]
        public void OnDestroy(ref SystemState state){}

        [BurstCompile]
        public void OnUpdate(ref SystemState state){
            if(SpawnQuery.IsEmpty){
                var spawnPrefab = SystemAPI.GetSingleton<CharacterSpawner>().SpawnPrefab;
^^^^^^ here error
Assets\Scripts\SystemSpawn.cs(28,45): error CS8377: The type 'CharacterSpawner' must be a non-nullable value type, along with all fields at any level of nesting, in order to use it as parameter 'T' in the generic type or method 'SystemAPI.GetSingleton<T>()'

I don’t understand what’s wrong?

IsbspawnerPrefab an Entity?
Do you have any nested classes in the CharacterSpawner component?
Can you show us CharacterSpawner?

Also, did you study Unity samples?

Shouldn’t your singleton be CharacterSpawner_Component ?

The error message tells you that the type you pass in your getsingleton is not a struct.
Your CharacterSpawner is a class that you use to author the data in the editor.

But at runtime you should use you baked entity data so the CharacterSpawner_Component.

Component… I understand, I’m still confused in these concepts … thanks

I have a related problem to this - what do I need to do with the Entities I get as prefabs? They have the wrong numbers. (like, 20:1, 21:1, but at runtime, the actual prefab entity is 180:1, etc.)

How do I translate these “baking time” entity numbers to the runtime ones?

I was using a FixedList to store a short list of prefab entities on my spawner component.

EDIT: Solved, seems like you HAVE to put them into a DynamicBuffer or a naked Entity in an IComponentData.

Yes entity remapping is only supported by IComponentData and IBufferElementData

Sid enote: You can have Entities inside struct fields inside struct fields inside ICDs. The rule is that it has to be an Entity field directly within the struct. FixedLists are actually just a bunch of byte fields which get type-punned which is why FixedList doesn’t work.

I appreciate the amount of effort that goes into these, so please don’t think I’m criticising, but to offer a perspective… I’d pay real money for the same information in a simple html format somewhere, with screenshots and snippets where appropriate.

Scrubbing backwards and forward through a video (where it’s nigh impossible to “skim” audio) is time consuming. It’s a bad medium for random access, especially when all you’re looking for is a particular technique or code snippet and don’t particularly care about the preamble.

Thanks for the feedback.
Glad to hear you would be willing to financially support my efforts.
In the mean time, all the code is available in git hub. Each video has 2 links, one to the starting repo and one to the ending repo.
So you can have a look through the code or download the project for yourself.
Some of the code as also been update to reflect changes to the package or in some case include some minor fixes.

I have been considering working on a readme page for the main repo with description of each episode, link to the video and correspondent branches and few key words to help searching for future reference.

Video and text format both have benefits and drawback. Videos are a pain to keep up to date but they provide a much wider reach (lot of people start searching on YouTube before they search on git). On the other hand having the code in git allows for update.

I’m trying to provide the best of both worlds while keeping it sustainable for me.

Hope you’ll still enjoy the other episodes of the series and that the availability of the code in git will be enough to compensate the pain of watching the video several times or having to scrub through it. Let me know if you have other feedback.