#Trying to Instantiate what seems to be a valid entity returns Invalid entity Entity(-1:-17) etc.

1 messages · Page 1 of 1 (latest)

modest ermine
#

I am trying to learn ECS and making a test game and i wanted to add an inventory system. i Have a InventoryItem DynamicBuffer and ItemDatabase singleton with a blob asset and I am trying to instantiate an Item from the database with id and add it to the InventoryItem buffer. but it throws this error. I tried a couple different ways but similar issue arises.

I've attached the Exception, Debugger views, ItemDatabaseAuthoring and InventoryInitialize system which checks for InventoryInitialize tag and tries to fill the the buffer elements remaining data from the database.

Thanks in advance 🙏🏼

rocky burrow
#

What EntityCommandBuffer returns to you is a temporary entity, not the actual entity. That's why its value is negative.

#

The temp entity is also called "deferred". Because ECB only records commands to do something.

#

ECB.Instantiate is such command.

#

Only at the time you invoke ECB.Playback, commands stored inside the ECB will actually become effective.

#

The commands will be played back sequentially in the order you record them.

modest ermine
#

Thanks for the quick response. So what is the return value used for then? and how could i fill my inventory with newly instantiated entities?

#

do i also tag them maybe with owner and index, and write another system to fill the inventory with those tags?

rocky burrow
#

ECB is mostly used in multithreading context (in jobs) where you can't make structural changes.

#

So you have to record the commands that will cause structural changes to play them back at appropriate point in the future.

#

So what is the return value used for then?
To be used in subsequent ECB commands.

#

When the ECB is played back at some point in the future, those temporary entities will be remapped to the actual ones.

modest ermine
#

that makes sense 😅 , but the exception states the srcEntity is invalid. let me quickly try with state.EntityManager.Instantiate to see if that works

rocky burrow
#

But a temp entity is only meaningful in the context of the ECB that returns it. Outside of this specific ECB, that entity is meaningless to other things.

#

let me quickly try with state.EntityManager.Instantiate to see if that works
It will work, because EM.Instantiate will cause structural change right at the point you call it.

#

Because of that, you will have an actual entity.

modest ermine
#

it still throws exception 🤔

rocky burrow
modest ermine
rocky burrow
#

srcEntity is what you pass into the Instantiate method.

modest ermine
#

yup, and looking at the debugger it seems to be valid

rocky burrow
#

Yeah, right.

#

i've tried Adding DynamicBuffer to the singleton initially
You should reproduce this

#

The next time you capture screenshots please also capture line numbers.

modest ermine
#

thanks a lot, and lastly could i ask a few questions?

rocky burrow
#

Feel free to ask. This is the place for asking questions anw 😂

modest ermine
#

i am kind of a performance freak when i see documents saying it is not performant. so is it ok to use state.EntityManager calls like this, or when should i start to worry?

#

and could you think of a better design for an inventory system. i've seen people suggesting Owner componenets on the items instead of Inventory like i've done, but it seems counter intuitive coming from Object oriented.

hearty sundial
#

But you would have to deal with entities being deferred thus making it harder to store somewhere. ECB has AppendToBuffer and it will remap the entity after playback

rocky burrow
#

If you're going to use ECB to instantiate multiple entities in a row then it's better to use EM because you can do that in bulk.

#

However, I can only say that what is considered "best" really depends on each case.

#

it seems counter intuitive coming from Object oriented
But coding in ECS you have to discard most of OOP knowledge.

carmine jetty
hearty sundial
#

If you want absolutelly the best performance then you should consider using EM in 99% of cases and try to use the batch overload of instantiate as it can speed up the process up to 2x on huge amounts of entities. If the action is rare and you have a few entities or you can't have a structural change (while in SystemAPI.Query or in jobs) then use ECB

rocky burrow
#

I clearly remember I did instantiate them in bulk then separately scheduled jobs to process parents then children.

hearty sundial
#

Hm, I also use bulk instantiation with LEGs and everything works

#

Just checked: bulk works with LEG

modest ermine
#

thank you guys for all the information. i thought the ECB would automatically bulk the operations so it would be better than EM but i guess thats not the case, good to know for sure.