Jump to content
  • Advertisement
Sign in to follow this  
Daivuk

Do excessive OOP in game can cause lag?

This topic is 4841 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi. My space combat will probably run about 500 to 2000 entities in real time. But do excessive OOP can cause lag for all these? I mean there is my hierarchical : CEntite ----CEntiteBehavior --------CStatic ------------CDebris ------------CAsteroid --------CVaisseau ------------CCapital ----------------CPorteur ----------------CDestroyer ------------CFrigate ----------------CIon ----------------CFlak ------------CLeger ----------------CFighter ----------------CBomber --------CProp ------------CCanon ------------CTurret --------CProjectile ------------CMissile ------------CObus So, when using a fighter, its also a CLeger->CVaisseau->CEntiteBehavior->CEntite So the question is more, do OOP is fast, or just usable? Because I can just put all this in one class (CEntite) and make a variable int type; and use switch case to know what to do lol. If its very faster this way. But I dont know. That's a stupid question, but I have asking myself for long time. Because I made a similar space simulation all in the main.cpp with just 3 functions and 2 or 3 structs and I got a so much better FPS.... Thanks to reply.

Share this post


Link to post
Share on other sites
Advertisement
The short answer is NO

The long answer is yes and no. Using OOP in games will slow things down a _LITTLE_ bit, due to extra function overhead(Virtual functions, etc.), but the performance hit doesn't weigh up against the better scalability of your project.

I'd keep it this way, because it's much easier to expand your code. If you want more ships, you just get a new class and done.

Toolmaker

Share this post


Link to post
Share on other sites
This is an interesting topic if always wondered about. For instance is a class slower than a struct and some functions? Not really, the C++ compiler does optimizations and all, and it's pretty good about keeping the speed up.

Share this post


Link to post
Share on other sites
It really depend on what features you use. For instance using virtual functions gives a small overhead, but a basic class is the same as a struct (with public access as default). Using a struct with some functions is just the same as using a class (though I find using a class cleaner :)


class C
{
public:
int val;
void Add(int v) { val += v; }
};

//is the same as the following

struct S
{
int val;
};

void Add(S* ptr, int v) { ptr->val += v; }

Share this post


Link to post
Share on other sites
In C++ a struct is a class with default access set to public instead of private.

As far as the switch statement goes, when using polymorphism the compiler is going to put some code in that selects the appropriate version of a method to call based on the class of the object. That logic is probably implemented as a switch statement too. So in the end it probably doesn't matter much, so I would do what makes things easier to code.

Share this post


Link to post
Share on other sites
Ok thanks. But I already know the diff between struct and class ;)
The point is its fastest using one class that do all the thing or a huge hierarchy....

Ok so whatever, thanks for the help. I will stay with this and get less ships in the secene :P.
I tested it and 1k ship is far TOO much. You just dont see anything lol

Share this post


Link to post
Share on other sites
The answer is no. OOP is simply an organizational tool. Any noted overheads can be atoned to simply being organized by OOP where they're easily visible instead of spread out in hiding, or inefficient use of the language, problems which can affect any code, OOP or not.

Share this post


Link to post
Share on other sites
Quote:
Original post by smr
As far as the switch statement goes, when using polymorphism the compiler is going to put some code in that selects the appropriate version of a method to call based on the class of the object. That logic is probably implemented as a switch statement too.

Eh, no.

A virtual call is implemented as an indirection through a vtable. In C it would look like this: <code>obj->vtable->VirtualMethod();</code>.

No switch.

Share this post


Link to post
Share on other sites
Quote:
Original post by Daivuk
Ok so whatever, thanks for the help. I will stay with this and get less ships in the secene :P.
I tested it and 1k ship is far TOO much. You just dont see anything lol

OOP isn't your problem.

Share this post


Link to post
Share on other sites
Quote:
Original post by Arild Fines
Quote:
Original post by smr
As far as the switch statement goes, when using polymorphism the compiler is going to put some code in that selects the appropriate version of a method to call based on the class of the object. That logic is probably implemented as a switch statement too.

Eh, no.

A virtual call is implemented as an indirection through a vtable. In C it would look like this: <code>obj->vtable->VirtualMethod();</code>.

No switch.


Well a switch statement is generally implemented as a jump table by the compiler, which is how the addresses of virtual functions are stored in the vtable... so it generally will be the same.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!