If I instantiate an object in a game loop, for example:
1 2 3 4
while(true) {
UI ui; // Just an example
ui.DomeSomething();
}
...and UI has its respective constructor and destructor, would I be creating unnecessary overhead? When I cout the constructor and destructor functions, they're obviously constantly called.
Should I declare UI ui outside of the game loop, or it doesn't matter in C++? Hope I make sense. Thanks!
The answer to the question is whether your UI object needs to be scoped outside of your loop.
If UI is your user interface, then yes, instantiating it with every game loop is not a good design. Instantiate it when you do your game startup tasks, before you pass control to your game loop.
Deciding proper scope for your objects is an ongoing task with an active project. IMO. Something best started before the first line of code is written and completed when the program "ships."
Thanks for the quick answer. I should have used another example. In terms of design it makes more sense starting the UI somewhere else, but performance-wise, does it make a difference instantiating in the loop?
I come from a Javascript background, a very paranoid world where garbage collection is always behind you. I know C++ doesn't suffer that, but again...I'm paranoid (and learning).
Performance-wise it could make a major difference where your UI is instantiated.
A UI should only be instantiated only ONCE. Doing it inside a game loop could have it constructed and destructed with every loop! for most games, video games, that could be ~20 times a second (or more)!
The actual workings of the UI creation/destruction would depend on your compiler, but IMO is still not a good design.
Your game loop should get user input, change the status of the game objects based on the user input and create the next frame of the game's display.
If you were driving a car, would you want to disassemble it after the wheels rotate a single revolution, and then reassemble it again?
Excellent answer FurryGuy. Thanks! The wheel example helped out a lot too. Although I would have thought constructing and destructing would hit performance based on the size of the object.
For example a very simple Vector class with just xy properties and a few helper functions.
Again, thanks, I'll keep what you said in mind and continue practicing. Solved.
"Performance bottlenecks are almost never where most programmers think they are. :) "
lol You're right, I'm beginning to learn that.
So if Obj A's construction function gets the sqrt of a vector N amount of times, and ObjB's constructor just changes a private integer variable. The performance bottleneck MIGHT be occurring within ObjA's constructor due to the amount of calculations done. Considering both are in the game loop example scenario.
Is that what you mean with the size of the object being less of a consideration than what happens in the ctr/dtr functions? Sorry if I'm extending this question too much.
Thanks for your honesty, I've found lots of value in your answers regardless. You're right about C++, so many ways to improve code, it never ends. A strong reason why I try to avoid working on anything related to JS nowadays. C++ is very addicting.
I hear you. Hopefully we'll both get there (and beyond) soon. It's going to take a lot of practice :) I think game programming helps, it's helping me. Especially if you decide on doing everything from scratch.
I actually ended up turning my UI class into a singleton by the way.