Which method would be best? What are the pros and cons? For example, if I have a main menu screen, I can easily start my game loop right as the user presses 'Start'. Which would be better, moving the 'Start' button as well as, 'settings, leaderboard, etc' buttons out of view? or to erase them and then recreate them when needed?
2 Answers
Erase them and recreate them as-needed. It is logically simpler and cleaner, and it avoids any potential problems that might arise from focus issues leaving the buttons and whatnot still accessible via the keyboard or other input device.
The only real reason to consider keeping them around but hiding them is if they are costly to re-create or show; generally this will not be a performance concern, and if it is it's worth first looking at whether or not you're doing something incorrect during the creation (such as reloading the textures from disk or whatever).
If for whatever reason you do have to go the route of simply moving the UI offscreen, make sure to take whatever precautions are needed to ensure they are totally disabled.
-
\$\begingroup\$ thanks, the main reason for my question is because my character shows up on the main menu screen in an idle animation, but the character is not part of the same class as the UI, so I was thinking about how to remove and redraw the character if the user clicked to enter the settings for example, which led me to think about the buttons in general, basically im recreating the buttons as suggested but i'm moving the character \$\endgroup\$ Commented Feb 9, 2017 at 22:37
It is simpler to debug 2 distinct simple code paths than it is to debug 1 code path where a boolean flag is used a lot to enable or disable features.
In other words for stuff like the main menu, pause screen you would have a different function you call from the main game loop:
switch (mode){
case MAIN_SCREEN: do_main_screen_stuff(); break;
case PAUSED: do_pauze_screen_stuff(); break;
case GAMEPLAY: game_update_and_render(); break;
case LOADING: loading_screen(); break;
}
But just because you aren't in GAMEPLAY
doesn't mean that you can't render the player character doing some animations.
For example Assassin's Creed loading screen let you run around in a empty world. The only code needed for that is the animation blending systems and the inputs, there is no need to check collisions with the world geometry or trigger areas, no need to run the render culling operations because there is nothing to render except the player character.
There is reason to move stuff offscreen when not used however. For example if you want to avoid lag in late game because more stuff becomes available in the hub world, instead you can move the trigger areas that activate later out of bounds so the extra checks are always happening and framerate stays constant.