Update() function in game engine, how to call it?

closed account (jLzA7k9E)
Hi guys, I'm trying to build a game engine but I'm stuck at this point: I don't see a way to call my "update" method. I've been thinking about a parent class with a virtual function which is then overridden in derived classes but that simply does not work for me since I'm not actually creating the objects.
An example to explain better:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//Base.h
class Base {

        static std::vector<Base*> vec;

    public:

        virtual void Update() = 0;
        static void Func();
};


//Base.cpp
void Base::Func()
{
    for (int i = 0; i != vec.size(), i++)
    {
        vec[i]->Update();
    }
}


As you can see here, Base::Func() would be called every frame.

At this point the "user" is going to create its own implementation and header files in order to use the engine, like this:

1
2
3
4
5
6
7
8
9
10
11
12
//Derived.h
class Derived : public Base {
public:
    void Update() override;
};

//Derived.cpp

void Derived::Update() 
{
    //Insert code here
}


Sadly, I don't see how I am going to do that. I'd love to learn how to do something like Unity3D or Cocos2d do with their update method. If somebody could help me I'd be very grateful, thanks in advance.
I don't see a way to call my "update" method. I've been thinking about a parent class with a virtual function which is then overridden in derived classes but that simply does not work for me since I'm not actually creating the objects.
You're over thinking it. Why exactly are you using polymorphism in the first place?
closed account (jLzA7k9E)
Thanks for answering. Simply because I thought that would work but it failed. I don't understand how I can call all the update methods of all the subclasses without creating an instance of those classes. Any suggestions?
Last edited on
I understand what you want to do, but what is the problem you're trying to solve? What is this being used for? I can most likely suggest a better alternative.
Last edited on
closed account (jLzA7k9E)
The problem is that I don't know how to call the update function in the subclass. Just like in the example, I am gonna build a statue of you if you'll find a solution :D

What I would need is: a way to call the update function of the subclass from the parent class, without having any instance of the subclass
Last edited on
I don't think you're understanding what I'm asking. I'm asking where this system will be used. I'm trying to provide a better/more maintainable alternative for what you're doing. This design isn't really the best.

What context will this be used in? (Engine subsystem? Game Objects? etc)
Last edited on
closed account (jLzA7k9E)
Oh sorry I misunderstood, it's to call the update function, so mostly to move sprites, animations and all transform-related functions.
I don't think this is the best system. It's not really maintainable, and I doubt you'll be able to get reasonable framerates once you have a relatively small amount of instances of child classes going around. (which may or may not be important depending on quite a few factors).

One of your main problems here is that you're trying to be too generic. Think about how your system would handle this. These systems have pretty much nothing in common from that standpoint. Not only will this pretty much kill any chance of cache coherency (which seems like it won't be an issue, but multiple tiny costs really do add up), but it's not maintainable. You'll end up either having not enough features in the base class, or too many in it. Making something like this is much more trouble than it's worth. Your system sounds good in theory, but it will not work out in practice. I tried doing something like this when I was new, and it really sucks when you have to delete thousands of lines of code just because you over-engineered a system without thinking it through.

I went more into design in this thread (second to last post on the first page)http://www.cplusplus.com/forum/beginner/152947/
it's worth a look.

Even things that might be similar are completely different when it comes to how the hardware processes it. And once you think about it, it doesn't make any sense from the programmer standpoint either.

Pretty much abandon everything that you've been taught about OOP, and think about how your hardware would like it. It seems like a large task, but it's worth it in the end.

While this doesn't directly answer your question, it's a (better) alternative.
Last edited on
closed account (jLzA7k9E)
Thank you for taking your time to respond to me. To be honest , I didn't quite catch the alternative. What are you exactly telling me to do?
Well to summarize my post, I'm telling you not do this. Rethink your solution. There is no reason to have a root base class that provides a common interface for so many tasks whatsoever (they're overly generic, and just not useful at all). Generally universal systems like this never work out. You're misusing OOP...

But then again, OOP isn't even the best thing to use when writing an engine, so stay clear of it altogether.

What I'm suggesting is to abandon OOP completely for your engine. It doesn't make sense at all (from the hardware standpoint), and IMO it doesn't belong in low-leveler game development. Leave that to high-level scripters. Think about how the computer handles data, after all the point of your program is to handle data. Design your code around your data. In my post that I've linked I show an example of what I mean. This way is the most logical IMO (and the fastest).

So instead of doing something like this, maybe you can make specific structures that serve one purpose, with no relation to one another (no base class common to each system!!!!). Design each system with a specific goal in mind.

I acknowledge what I'm saying is ambiguous, but I can't really go more into detail than that without having a specific example of what you want to do.
Last edited on
closed account (jLzA7k9E)
But to "update" all that happen I do need an update function, don't I?
Like let's assume the engine is finished. Now the user writes an implementation file.
In that file he has got to be able to have a function that gets called every time? Otherwise I don't see how that would work.
I post some code from cocos2d ( not my code ):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//HelloWorld.h
#include "cocos2d.h"

class HelloWorld : public cocos2d::Layer
{
public:
    static cocos2d::Scene* createScene();
    virtual bool init() override;
    CREATE_FUNC(HelloWorld);

    void update(float) override;

private:
   cocos2d::Sprite* sprite;
};

//HelloWorld.cpp
#include "HelloWorld.h"

USING_NS_CC;

Scene* HelloWorld::createScene()
{
    auto scene = Scene::create();
    auto layer = HelloWorld::create();
    scene->addChild(layer);
    return scene;
}

bool HelloWorld::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
    
    sprite = Sprite::create("Veyron.png");
    sprite->setPosition(this->getBoundingBox().getMidX(), this->getBoundingBox().getMidY());
    this->addChild(sprite, 0);
    
    this->scheduleUpdate();
    return true;
}

void HelloWorld::update(float delta){
   auto position = sprite->getPosition();
   position.x -= 250 * delta;
   if (position.x  < 0 - (sprite->getBoundingBox().size.width / 2))
      position.x = this->getBoundingBox().getMaxX() + sprite->getBoundingBox().size.width/2;
   sprite->setPosition(position);
}


As you can see there's an overridden "init" function that gets called only once, at the beginning. Then there's an overridden "update" function that gets called at every frame ( or at every given time ).
Yes...

The update function needs to be called. The reason why your method isn't working is because you don't have a class instance.

Broken down, a class is essentially just a structure, and methods are just normal functions that take the structure as a parameter.

This:
1
2
3
4
5
6
7
8
9
10
11
12
class MyClass
{
    public:
        void SetX(int x);
    private:
        int x;
};

void MyClass::SetX(int x)
{
    this->x = x;
}


Is equivalent to:
1
2
3
4
5
6
7
8
9
struct MyClass
{
    int x;
};

void SetX(MyClass* _this, int x)
{
    _this->x = x;
}


But I believe you're trying to call the function without making an instance (which is illegal if the function isn't a static member), so you're not passing the first parameter of the second example, which is illegal (it doesn't know what structure to modify).

So pretty much just make an instance of the class and then call it to solve your problem (but I still believe this is a terrible way to go about designing your engine).
Last edited on
Topic archived. No new replies allowed.