gecko0307 / dagon

3D game engine for D
https://gecko0307.github.io/dagon
Other
321 stars 30 forks source link

Calling EntityController inherited functions #56

Closed infinityplusb closed 4 years ago

infinityplusb commented 5 years ago

I'm trying to call a function I've defined in a class that inherits from a EntityController, but am having issues with the syntax.

I've instantiated a new Entity Controller as an "NPC" type character, for an ant-simulation

class NewAnt : EntityController
{
    Vector3f = home;
    this(Entity e){
        super(e);
    }
    override void update(double dt)
    {...}

    void setHome(in Vector3f location)
    {
        entity.position = location;
        home = location ;
    }
}

Within a

class NewScene : Scene
{
    NewAnt eAnt; 
    Entity ants;    // a parent entity to store all the ants
...
        auto ant = createEntity3D(ants);
        ant.drawable = aOBJFood.mesh;
        ant.material = antWithNoFoodMaterial;
        ant.rotation = rotationQuaternion(Axis.y, degtorad(180.0f));
        ant.scaling = Vector3f(0.5, 0.5, 0.5);

        ant.updateTransformation(0.0);

        auto antCtrl = New!NewAnt(ant);
        ant.controller = antCtrl;

        antCtrl.setHome(Vector3f(myRndXPos, 0.5f, myRndYPos));
}

This all works fine, and the function setHome gets called correctly when I am instantiating the EntityController. This program runs and does everything correctly.

However if I try to call the setHome function during the onUpdate I clearly am not doing it correctly.

ants.children[1].controller.setHome(Vector3f(0.0f, 0.0f, 0.0f));

Gives me

Error: no property setHome for type dagon.logics.controller.EntityController

The code at here should compile and run (not release mode yet :S). Adding in line 142 in Simulator.d causes the error.

gecko0307 commented 5 years ago

You should cast the controller to NewAnt to access child class methods, like this:

NewAnt antController = cast(NewAnt)ants.children[1].controller;
antController.setHome(Vector3f(0.0f, 0.0f, 0.0f));
infinityplusb commented 5 years ago

Thanks. That works.

I already created a controller here

        auto antCtrl = New!NewAnt(ant);
        ant.controller = antCtrl;

Should I have set that somewhere else, or in a different way, so I don't have to keep creating controllers? Or so that I can create it once and access it whenever I want?

gecko0307 commented 5 years ago

You can store antCtrl in your scene. I think it's better to store controllers in an array, at the same indices with corresponding indices in ants.children.