AustinEast / cog

Macro powered ECS Framework written in Haxe
30 stars 1 forks source link

Uncaught exception - Invalid field access : components #2

Open christopherghenderson opened 3 years ago

christopherghenderson commented 3 years ago

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;

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

MSGhero commented 3 years ago

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)

AustinEast commented 3 years ago

@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 commented 3 years ago

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;