#Working with manual entity prefab

1 messages ยท Page 1 of 1 (latest)

dire surge
#

This is the specification of my character archetype:

Character Archetype:
    Prefab   // Prefab component from Unity.Entities
    EnityTag // A shared component with value is either "Hero" or "Enemy"
    CharacterIdComponent
    LocalTransform
    MovementSpeedComponent
    DirectionComponent
    HpComponent
    AttackPowerComponent
    AttackSpeedComponent
#

And this is the query:

[BurstCompile]
public void OnUpdate(ref SystemState state)
{
    var options
        = EntityQueryOptions.IncludePrefab
        | EntityQueryOptions.IncludeDisabledEntities
        | EntityQueryOptions.IgnoreComponentEnabledState
        ;

    _query = QueryBuilder().WithOptions(options).WithAll<Prefab, EntityTag>().Build();
    _query.SetSharedComponentFilter(EntityKind.Hero.AsTag()); // Set the shared EntityTag component

    if (_query.IsEmpty)
    {
        UnityEngine.Debug.Log("Empty Query");
        return;
    }

    var prefab = _query.GetSingletonEntity();
    state.EntityManager.Instantiate(prefab);
}
#

The result is always Empty Query. I don't understand why is that. What should I do to get the prefab I want?

desert pine
dire surge
#

I just follow the recently updated Unity ECS samples

desert pine
#

ah, like this

#

well

#

do your entities exist in the first place?

dire surge
desert pine
#

check relationships

#

tab

#

and see whether your system is here

dire surge
#

there is no relationship

#

what does that mean?

desert pine
#

your entity doesn't match the query

dire surge
#

well, that's what I'm asking here: why it doesn't match? ๐Ÿ˜‚

desert pine
#

QueryBuilder().WithOptions(options).WithAll<Prefab, EntityTag>().Build();
Btw, I think you should use SystemAPI instead

#

or it is already?

dire surge
#

I'm using SystemAPI

using static Unity.Entities.SystemAPI;
desert pine
#

ah

#

I couldn't figure why method is here out of nowhere ๐Ÿ˜…

#

hmm

#

try to explicitly specify SystemAPI

#

also, try removing disabled component

#

and just leave it as Prefab

dire surge
desert pine
dire surge
desert pine
#

is it properly generated?

desert pine
dire surge
desert pine
#

but I never specified disabled

#

to query prefabs

#

๐Ÿค”

#

let me check my own code

dire surge
#

I pass these options into the query builder, I expect they should be respected

var options
    = EntityQueryOptions.IncludePrefab
    | EntityQueryOptions.IncludeDisabledEntities
    | EntityQueryOptions.IgnoreComponentEnabledState
    ;
desert pine
#

did you check query without shared filter?

dire surge
#

didn't work too

#

๐Ÿ˜‚

desert pine
#

hmm

#

Mind showing generated code?

#

I feel like codegen gets borked if you use variables to specify options

dire surge
#

hmm how to see that?

desert pine
#

in rider you just ctrl + click on type declaration

#

it'll suggest other documents that declare this partial type

dire surge
#

too bad I don't have rider

desert pine
#

you can find it in explorer too

#

lemme remember the folder

#

C:\Users\username\AppData\Local\Temp\SourceGeneratedDocuments\D97F33D625EC816606B84DCC\

#

but I feel like VS should be able to inspect other declarations somehow

#

maybe you can just try doing this._somegeneratedquery and use VS somehow to get where it was declared?

dire surge
#

can ILSpy work?

desert pine
#

no, it's code, not assembly

#

oh, if you mean building it

#

and then inspecting, then yeah, but I wouldn't suggest it ๐Ÿ˜…

#

surely VS should support inspecting generated code

dire surge
#

well the assembly is in the Project/Library/ScriptAssemblies folder in case you don't know

#

what should I look for then?

#

suppose I have the generated code

desert pine
#

just how generated code looks

#

because generated code is what will run

#

not what you actually wrote

dire surge
#

I do know about source generator ๐Ÿ˜‚

#

what I mean is what should I look for in the generated code?

desert pine
#

whether it's correct

#

options are passed

#

and etc

dire surge
dire surge
desert pine
#

just inline them

#

that should fix it I think

dire surge
dire surge
#

just found this bug ๐Ÿ˜‚

desert pine
#

yeah, that seemed strange

dire surge
#

now I have my Hero spawned successfully

#

thanks again!

desert pine
#

runtime prefab creation is not really good idea in general, any specific idea you avoid baking?

dire surge
#

I just want to do most things by code

#

to test the limit

desert pine
#

but that's simply slower

#

more build size, development time spent and etc

dire surge
#

well I'm free to do whatever I want atm

thorny mirage
dire surge
thorny mirage
#

do you see the query in the system?

#

yes codegen is fragile like that...

dire surge
#

If I define a variable, it won't be passed into the query builder

thorny mirage
#

next time inspect the query in the System debugger. that should have shown you whats wrong

dire surge
#

do you mean this?

thorny mirage
#

yes

#

when you select a system you can see the queries it has

desert pine
thorny mirage
#

if codegen failed for your query you wouldnt see one in here

dire surge
#

Oh, so that is

#

Thanks

dire surge
#

btw I'm be more mindful about the generated code