6 Replies - 349 Views - Last Post: 12 August 2013 - 12:40 PM Rate Topic: -----

#1 Mr_Fraggs  Icon User is offline

  • D.I.C Head

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

C++ Should Static be used?

Posted 10 August 2013 - 06:42 PM

Hey guys, I'm trying to find a better way to set up all of my managers/factories in the game i'm working on aside from passing references of them through the game where they are needed, which sometimes might be 4 or 5 passes.

Some of these systems are:
EntityFactory & EntityBank: Used to create, destroy and manage entities (anything in the game is an entity).
TextureFactory & TextureBank: Used to load and manage textures used in the game.

Take those two for example. Two systems needed mostly throughout the game by each system at some point, whether for initializing or destroying entities, getting a texture reference from the TextureBank, etc.

So, I know using the static keyword is frowned upon pretty much most of the time. But should I be initializing these Systems in the highest Game class, and use their functions statically throughout the program instead of passing references of them everywhere? Essentially making it a global variable. Or should I just design a better way to get access to systems that might be needed in a lot of other systems. Thanks guys!

This post has been edited by Mr_Fraggs: 10 August 2013 - 06:55 PM


Is This A Good Question/Topic? 1
  • +

Replies To: C++ Should Static be used?

#2 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: C++ Should Static be used?

Posted 10 August 2013 - 07:39 PM

No, static is not frowned upon at all. You will see that it is the cornerstone of design patterns like 'singletons' for instance. It looks like you are becoming aware of the dangers of object access and duplication which is an excellent observation.

Have you heard of design patterns?
Was This Post Helpful? 2
  • +
  • -

#3 Mr_Fraggs  Icon User is offline

  • D.I.C Head

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

Re: C++ Should Static be used?

Posted 11 August 2013 - 12:52 PM

I have, i'm not aware of many, but the singleton pattern is one I did scan over a bit. Also, the factory system, which i'm using in this project.

I'm working on a component/entity system for my game, and all was well with one or two entities on the screen, but as soon as I added a few hundred, with just a position component (just an x and y variable for now), updating those couple hundred caused my game to go from about 6000 fps to about 1 fps, and 60 UPS to 20-30 UPS. So i've been going through the entire system trying to figure out how to make the Update call more efficient, and this was one of my first stops. Next I think I might need to try getting entities from the entity bank much faster, which is what I think is causing the huge slowdown.
Was This Post Helpful? 0
  • +
  • -

#4 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: C++ Should Static be used?

Posted 11 August 2013 - 01:11 PM

This is pretty much why we have clipping planes, you're trying to render too much.

For you implementation of the factory pattern, are you using 'Factory Method' or 'Abstract Factory Pattern'?
Was This Post Helpful? 0
  • +
  • -

#5 Mr_Fraggs  Icon User is offline

  • D.I.C Head

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

Re: C++ Should Static be used?

Posted 11 August 2013 - 06:01 PM

I'm actually not sure about that last question, but I know that it's not because of the rendering, because I tried it without rendering anything, and just giving position components to a few hundred entities, and that slowed it down the same. I'm thinking it has to do with when I get a list of active entities the system needs. Right now i'm returning a pointer to the entity if it's needed, and searching through the entire entity list for the required ones. I'm working on simplifying that down now, hopefully it fixes some problems (All of them would be nice :P)!
Was This Post Helpful? 0
  • +
  • -

#6 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: C++ Should Static be used?

Posted 11 August 2013 - 06:50 PM

I just read your initial post again and you have the object type 'EntityFactory'. You need to work out:

1. How many objects can safely be on screen to not cause issues.
2. Clip excess objects from the scene.

Once you work that out you should be good to go.
Was This Post Helpful? 0
  • +
  • -

#7 Mr_Fraggs  Icon User is offline

  • D.I.C Head

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

Re: C++ Should Static be used?

Posted 12 August 2013 - 12:40 PM

That first thing I have been trying to work on. The thing is, with the amount of entities I have right now, basically I have a 1280x720 pixel screen, and the tiles used for the entities are 32x32 pixels. Just to use for testing, I create new entities based on how many tiles will fit on the screen, because if I can't even fill the screen with the amount of entities required to just place tiles on the ground (without the graphic, still), then there is clearly something wrong with my setup. I've taken one tile off of each side of the screen so that the edges still have some space around them for now, but I definitely feel like there should be way more entities on the screen without much problems, so that means I have to re-design my system and try to make it more efficient.

Basically my process is this:
EntityFactory object that creates, deletes, and manages entities. Entities are just a global ID, with components in them.
Systems that do logic on those components. The components are just data. The logic is all calculated in systems, like a PositionComponent would be needed by a MovementSystem.
Each System gets a list of entities with the required components by searching the entity bank. Then it does logic on all of those entities.

I think that getting each list of entities from the EntityBank is what is causing the slow down. Right now I have 6 systems, and each one has to research the entity factory and return a vector of entityIDs that have the required components. I think doing that multiple times per update is what is causing the updates per second to drop from 60 down to 20-30. I still have yet to fix the problem, although I did speed it up just a little by only returning IDs instead of entire entity objects until those entity objects are actually needed.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1