2 Replies - 1054 Views - Last Post: 14 August 2013 - 02:33 PM Rate Topic: -----

#1 Mr_Fraggs  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 89
  • Joined: 17-June 12

Entity/Component System Slowdown

Posted 13 August 2013 - 02:05 PM

Hey everyone! So for the past few weeks i've been working on developing an entity/component engine to work on games with. I've got the design and implementation down after the past few weeks, but after trying it out for more than adding one or two entities, the program bogs down, with FPS/UPS dropping way too low.

So, here's how it's set up. I'll link the code that you might need to see to get an idea of how this engine works from start to finish. So first, everything in the game is an entity. An entity is just an ID number with a list of components. A component is literally just data, no logic. For instance, a player entity would have it's own unique ID number, a PositionComponent for giving it a position in the world, a graphic component used to draw it, etc. All of the logic is computed in Systems, which use the data on components to know what to do. So if we have a Movement System, it would take the player's PositionComponent, and update the PositionComponent based on, say, an InputComponent() that tells the Movement System if a key is pressed.

When I first got it running, I added one or two entities with a couple components in, and everything worked fine. Now, I tried adding in enough entities to cover the screen in tiles for a map (1280/32 tiles wide, 720 / 32 tiles high), minus one from each side just for me. Each entity for a tile on the map is just a Position component and a Graphic component, used for drawing the tile at a certain area. With that many entities, my program goes from 60FPS/ 60UPS, where it's locked to 60 UPS and if VSync is on, 60FPS, down to about 2 FPS and maybe 20 UPS.

First, I thought that it was rendering all of the graphics, but when I just add a Position Component, and throw in an InputComponent just to get a few different systems going, it is still as slow as when I was drawing graphics. After working on trying to fix this for a few days now, I come to you guys for some advice! I've put all of the code that should give you an idea of how the systems work in spoiler tags below so they don't take up pages of room. I've only included the code I thought would be what you guys needed. If I left something out that you might need to see, feel free to let me know and i'll toss it in as well.

I believe the problem lies somewhere in how i'm scanning the entire entity bank for every system to look for the components that the system needs. That's basically where I've been spending most of my time trying to fix this problem. If I tested it right, I believe that the overall Update() call from the main Game class is taking too long, since it's trying to be called a fixed amount of times per second. Any ideas would be awesome! And if you see anything else wrong with my code, feel free to toss it in here as well, but only if you happen to notice it while you're reading it! I know it's a lot of code, and a lot to ask for, but I'm pretty much out of ideas. Thanks guys :D

Spoiler

This post has been edited by Mr_Fraggs: 13 August 2013 - 02:09 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Entity/Component System Slowdown

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,437
  • Joined: 25-December 09

Re: Entity/Component System Slowdown

Posted 14 August 2013 - 06:46 AM

So much wrong it's hard to tell you where to start. But I guess I'll start by correcting one of your comments.

	for(unsigned int i = 0; i < activeEntityIDs.size(); i++)
	{
		PositionComponent *pc = EntityFactory::GetComponentFromEntity<PositionComponent*>(activeEntityIDs[i], Component::ComponentType::POSITION_COMPONENT);
		InputComponent *ic = EntityFactory::GetComponentFromEntity<InputComponent*>(activeEntityIDs[i], Component::ComponentType::INPUT_COMPONENT);
		//Now do some updating!


		//Set the temporary variables to null, then delete them.
		ic = nullptr;
		pc = nullptr;
		delete pc;
		delete ic;


That comment should read:

Quote

// Let's create a massive memory leak by setting the variables to null before deleting them.


Jim
Was This Post Helpful? 1
  • +
  • -

#3 Mr_Fraggs  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 89
  • Joined: 17-June 12

Re: Entity/Component System Slowdown

Posted 14 August 2013 - 02:33 PM

Thanks for pointing that out. Every time I would delete the pointer without that, my program crashed. It should be noted though, that the pointer is pointing to a smart_ptr<Type>, which is why I believe that it crashed each time. I'm just borrowing that object pointer from a Component Bank, then releasing the pointer, but keeping the object in the bank until the end of the program or it's deleted.

I also took that part out between last night and tonight. Instead, I use a vector of shared_ptr<Component> that is filled from inside of the function that gives out the references. Now, it looks like this:

Spoiler


Let me know if that improves/fixes that memory leak problem. Short little comments like that are what helps me learn. I learn by doing, (not saying I haven't been reading any C++ books, been doing that for years!) and while i'm doing, someone telling me where I screwed up is a great learning tool :)

Since I posted this, I have also moved all component objects themselves all into one bank, instead of them living in each entity. This seemed to speed up the program a lot, although still not as fast as it should be. I'm still working on it, but anything you guys might want to toss in here that could help or something I might be doing wrong is still appreciated, even if I changed that part of code already!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1