Another newbie question:

For a typical action game, you want the game event loop to run constantly, even if the user doesn't do anything that frame. But for a turn-based game (or a just a non-game app), you want the game event-loop to wait for input as long as there is nothing else to do, otherwise you needlessly consume the user's CPU (and battery for laptops and mobiles).

Is there a way to tell Godot that an app wants the main event loop to wait for an input event?

I read those. I see how to make a node not respond to events, but I don't see anything about making the event-loop itself block until there is a new event (and no background threads to run). What did I miss?

I seem to recall reading there was a way to do this, but now that I need it, I can't find where I read that.

    I don't see how that would do what I'm asking. That doesn't change the nature of the MainLoop. As far as I can tell, the MainLoop is still not going to block waiting for an event. So my node(s) will not get any CPU, but the main loop will still be consuming CPU.

    Haystack making the event-loop itself block

    There are methods that control the event-loop, such as set_process_input(), but I think the overhead is negligible if there's no actual processing to be done.
    https://docs.godotengine.org/en/stable/classes/class_node.html#methods

    If I have a node in the scene tree that contains a _process() method, and that node is not currently displayed, such as a popup dialog, I use set_process(false) to prevent its _process() method from being called.

    The game loop goes on in a turn based game, even if there is no input from the user and all the other player's entities have moved. There may be sound playing, animations animating, the user may look around while pondering their next move.
    So, you don't stop this thing, though you may allow the player to press pause for pressing business. Don't fear about battery use, the other things those contraptions do, like wifi, bluetooth, display, cost more than a mobile processor processing.

    No, you wouldn't block because Godot (by default) is single-threaded, so that would freeze and or crash the game. I think you are thinking about it wrong. If you are doing no work, then no work is being done. There are functions being called in the engine all the time (even with a blank project with no code) so it's sort of a fixed overhead. And a function call (with nothing in it) is so cheap, you could probably call it thousands or maybe a million times before you even notice 1 fps being lost. So this seems to be a premature optimization.

      If you want to avoid the SceneTree and make a custom MainLoop, you can. That likely also means you won't really be using any of the nodes and scene composition. You'll have to directly interact with the servers for everything afaik. But if you want, you can. It'll be a lot of work tho.

      https://docs.godotengine.org/en/stable/tutorials/scripting/scene_tree.html?highlight=MainLoop#mainloop
      https://docs.godotengine.org/en/stable/classes/class_mainloop.html

      cybereality That wouldn't freeze or crash the game. The OS would suspend the game's process until the OS detects another input event. The app is single-threaded, but the OS isn't. This is how all standard single-threaded non-game apps work. Inside every GUI app is main loop something like this:

      while (not_done)
      {
         if (input_event_pending())
        {
           process_input_event();
           continue;
        }
        else
        {
            yield_thread_until_next_event();
        }
      }

        Haystack
        I suspect the problem there is that godot has to support a half-dozen or more operating systems, so any interface with the operating system has to be the least common denominator of all systems (including html). The developers have tried to make it as independent as possible, to avoid problems with new exports or changes to APIs. The exceptions are usually minor things like accelerometers.

        Edit: There is a low processor mode intended for non-game applications that reduces the background activity.

          I don't want to be rude, but please actually read the documentation I gave you. It explains it clearly.

          It may be easier if you watch this video. Godot is based on event driven programming, so you use event handlers and signals to deal with events or asynchronous events. Very rarely, if ever, would you use a while loop, because it will most likely freeze your game, like I said.

            Previously I wrote "I seem to recall reading there was a way to do this, but now that I need it, I can't find where I read that." This is what I was thinking of: "Project >> Project Settings >> Application >> Run >> Low Processor Mode".

            https://docs.godotengine.org/en/stable/classes/class_os.html

            "If true, the engine optimizes for low processor usage by only refreshing the screen if needed. Can improve battery consumption on mobile."

            duane Yes, "low processor mode" was what I was thinking of. Thanks!

            cybereality

            Very rarely, if ever, would you use a while loop, because it will most likely freeze your game, like I said.

            I think you are misunderstanding what I meant. I think that you think I want to put that "while" loop inside one of my node's callbacks. I do not want that. I agree with you that that would be bad.

            My point was that something like the "while" event loop exists somewhere inside the Godot core, and there exists some way to configure it (e.g. one or more Godot project settings) so that it will sleep until the next event instead of just looping as fast as it can.

            The Godot Editor (written in Godot) does not consume extra CPU cycles when you aren't typing or mousing (or debugging). But some of the very trivial demo projects I've downloaded do consume CPU even when you don't interact with them.

            I'm trying to learn how best to minimize the amount of CPU/GPU that my app consumes when it doesn't have anything useful to do. (When there are no animations playing, no music, no UI events, etc.)

              Haystack My point was that something like the "while" event loop exists somewhere inside the Godot core, and there exists some way to configure it (e.g. one or more Godot project settings) so that it will sleep until the next event instead of just looping as fast as it can.

              If you look in the documentation, the main loop is an abstract class that inherits from SceneTree (Edit: correction below !). The main loop is started when the program starts, and when the main loop ends, the program ends. The main loop in my renderer does the same (though it is ludicrously much simpler), and I believe any such thing on a Turing machine that starts and controls a time depending system like a game, or simulation that calculates stuff periodically does it (ready for correction).

              When you say "as fast at it can" I think there are several misconceptions in there.

              First, assuming not a headless system (a server that does not display but just calculate) there is rendering. The "speed" (frame rate) depends either on the refresh rate of the monitor (aka vsync, switch it on !), as fast as it can (but then the drivers or program may start to optimize things away when it becomes clear that a frame in flight will never see the light of day, "my pc makes 500 fps !" is a nonsensical statement), or things in between with modern monitors (in Vulkan they are named presentation modes). So, if you switch on vsync, the rendering part of the main loop will only be called after the monitor signalled "give me the next frame". This is done every 60th of a second or every 144th in fixed modes. With many games this is only a small fraction of what a processor can do, and it is usually only one thread, out of 16 or more on modern PC processors. You're pretty close to idle power consumption most of the time.

              Second, the main loop may call other functionality, and start concurrent functions, physics processing for instance that needs a different, fixed(!) tact or else numeric catastrophe :-). The main loop may and must, if values are needed, synchronize these things, for that it must run. You will need a controlled way to pause and resume it, or else gamers walk out on you with the pitchforks in their hands shouting "much wrong, many anger, such error !" :-)

              Suggestion is, let the game engine do its thing and make a game without pondering the intestines of Godot, or, if you wish to try, write your own framework, which is a very nice finger exercise :-), and probably makes you come back to the engine after a year or 2, more clever than before :-)

              Edit: On a PC, do NOT overclock if you don't really need it. That's the energy hogs. Mine rises from around 70W to >400W. On a mobile, switch off network and bluetooth or even to flight mode because driving an antenna is energy intensive, more than what the rest of the electronics need.

                Haystack The Godot Editor (written in Godot) does not consume extra CPU cycles when you aren't typing or mousing (or debugging). But some of the very trivial demo projects I've downloaded do consume CPU even when you don't interact with them.

                Demo projects don't necessarily use optimal code. They often keep it simple for teaching purposes.