cog icon indicating copy to clipboard operation
cog copied to clipboard

Uncaught exception - Invalid field access : components

Open christopherghenderson opened this issue 3 years ago • 3 comments

I'm trying to pull a component from an entity, however I'm receiving this error. Uncaught exception - Invalid field access : components

Iterating over entities in: @:nodes var nodes:Node<TiledEnvironment>;

I'm pulling the component using: node.entity.components.get(Position).body.x;

Called from ecs/systems/client/TiledEnvironmentSystem.hx line 43 Called from cog/System.hx line 44 Called from cog/Engine.hx line 16 Called from EcsManager.hx line 129 Called from Play.hx line 77 Called from Main.hx line 55 Called from openfl/events/EventDispatcher.hx line 402 Called from a C function Called from openfl/display/DisplayObject.hx line 1399 Called from openfl/display/Stage.hx line 1159 Invalid field access : components Called from ? line 1 Called from ApplicationMain.hx line 25 Called from ApplicationMain.hx line 130 Called from lime/app/Application.hx line 150 Called from lime/_internal/backend/native/NativeApplication.hx line 146 Called from a C function Called from lime/_internal/backend/native/NativeApplication.hx line 370 Called from lime/_internal/macros/EventMacro.hx line 91 Called from openfl/display/Stage.hx line 1950 Called from openfl/display/Stage.hx line 1163 Called from openfl/display/Stage.hx line 1423 Called from openfl/display/Stage.hx line 1159 Called from openfl/display/DisplayObject.hx line 1399 Called from a C function Called from openfl/events/EventDispatcher.hx line 402 Called from Main.hx line 55 Called from Play.hx line 77 Called from EcsManager.hx line 129 Called from cog/Engine.hx line 16 Called from cog/System.hx line 44 Called from ecs/systems/client/TiledEnvironmentSystem.hx line 43 Uncaught exception - Invalid field access : components

christopherghenderson avatar Apr 25 '21 05:04 christopherghenderson

You might be going about it wrong? Here's what I use: @:nodes var nodes:Node<Animation, Flag>; if (node.animation.index < etc) node.flag.set(etc)

MSGhero avatar Apr 26 '21 02:04 MSGhero

~~@MSGhero is correct, you need to specify at least 1 component when defining your nodes~~

~~So instead of this: @:nodes var nodes:Node;~~

~~You need to do something like this: @:nodes var nodes:Node<MyComponent>;~~

*Edit - actually just tested it, and specifying no components (like @:nodes var nodes:Node;) should actually work just fine, so the issue is lying elsewhere..

AustinEast avatar May 03 '21 15:05 AustinEast

Are you sure your "Entity" class (from node.entity.components.get(Position).body.x;) has it's components field assigned correctly? For example:


class Entity {
    public var components:Components;

    public function new() {
        // Make sure that the `components` field is assigned
        components = new Components();

        components.entity = this;
    }
}

As a side note, all Node instances have a reference to their relevant components object. So you could just do this:

node.components.get(Position).body.x;

instead of:

node.entity.components.get(Position).body.x;

AustinEast avatar May 03 '21 15:05 AustinEast