Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How do I restart a scene/destroy all entities properly? (without any error)

Discussion in 'Entity Component System' started by Conspiracy, Feb 2, 2020.

  1. Conspiracy

    Conspiracy

    Joined:
    Oct 22, 2016
    Posts:
    40
    quick FYI.
    I know I'm asking a lot but this is more of a beginner/general question so please try to answer as clearly as possible, try not to gloss over any detail assuming I already know what you're talking about, because I don't (probably).

    Anyway...
    So restarting a scene requires me to also destroy all the entities. However, there are job systems running on those entities and so if I destroy those entities it will create an error (at least in the editor, idk why it's running fine in the actual build for android).

    I used postUpdateCommands in a componentSystem ForEach(entity ent) loop to destroy those entities. After the entities are destroyed this errors will pop up :

    InvalidOperationException: The previously scheduled job Broadphase:AdjustBodyIndicesJob reads from the NativeArray AdjustBodyIndicesJob.HaveStaticBodiesChanged. You must call JobHandle.Complete() on the job Broadphase:AdjustBodyIndicesJob, before you can write to the NativeArray safely.

    InvalidOperationException: The previously scheduled job Scheduler:CreateDispatchPairPhasesJob reads from the NativeArray CreateDispatchPairPhasesJob.PhaseLookupTableDynamicDynamicPairs. You must call JobHandle.Complete() on the job Scheduler:CreateDispatchPairPhasesJob, before you can deallocate the NativeArray safely.


    Now I know it says that I should call JobHandle.Complete() but I'm not destroying the entities in the jobSystem so I don't know how I'm supposed to do that. There's also a problem with entity query (that the job got on void OnCreate()) missing now that the entity is destroyed (even tho it respawned).

    so yeah, that was my question. thank you for your help
     
  2. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,554
    Destroy command is a structural change so even if you use command buffer or just use EntityManager then all jobs would already be completed first on playback.. The problem is probably not that you could destroy an entity that jobs are using. Because API make sure you couldn't.

    So what usually happen is that when you flip the table like this a jobs that you forgot to chain JobHandle properly will show its problem. (It worked before because the entity combination didn't allow the chain-disconnected job from running and you avoided problem by coincidentally) You have to trace the returned handle of that job in error did it connect to inputDeps, or the another job that got reported together.
     
  3. Conspiracy

    Conspiracy

    Joined:
    Oct 22, 2016
    Posts:
    40
    I'm pretty sure that I put inputDeps in every single job schedule also what do you mean by "another job got reported together"?

    I think I realized that a lot the problem is with the job having its entity query reference lost because the entity was destroyed and the new entity is not being referenced because the OnCreate() void is not running anymore. I'm still thinking of ways to handle this tho.