Table of Contents

Class MainLoop

Namespace
Godot
Assembly
GodotSharp.dll

MainLoop is the abstract base class for a Godot project's game loop. It is inherited by SceneTree, which is the default game loop implementation used in Godot projects, though it is also possible to write and use one's own MainLoop subclass instead of the scene tree.

Upon the application start, a MainLoop implementation must be provided to the OS; otherwise, the application will exit. This happens automatically (and a SceneTree is created) unless a MainLoopScript is provided from the command line (with e.g. godot -s my_loop.gd) or the "Main Loop Type" project setting is overwritten.

Here is an example script implementing a simple MainLoop:

using Godot;

[GlobalClass] public partial class CustomMainLoop : MainLoop { private double _timeElapsed = 0;

  public override void _Initialize()
  {
      GD.Print("Initialized:");
      GD.Print($"  Starting Time: {_timeElapsed}");
  }

  public override bool _Process(double delta)
  {
      _timeElapsed += delta;
      // Return true to end the main loop.
      return Input.GetMouseButtonMask() != 0 || Input.IsKeyPressed(Key.Escape);
  }

  private void _Finalize()
  {
      GD.Print("Finalized:");
      GD.Print($"  End Time: {_timeElapsed}");
  }

}

public class MainLoop : GodotObject, IDisposable
Inheritance
MainLoop
Implements
Derived
Inherited Members

Constructors

MainLoop()

public MainLoop()

Fields

NotificationApplicationFocusIn

Notification received from the OS when the application is focused, i.e. when changing the focus from the OS desktop or a thirdparty application to any open window of the Godot instance.

Implemented on desktop platforms.

public const long NotificationApplicationFocusIn = 2016

Field Value

long

NotificationApplicationFocusOut

Notification received from the OS when the application is defocused, i.e. when changing the focus from any open window of the Godot instance to the OS desktop or a thirdparty application.

Implemented on desktop platforms.

public const long NotificationApplicationFocusOut = 2017

Field Value

long

NotificationApplicationPaused

Notification received from the OS when the application is paused.

Specific to the Android platform.

public const long NotificationApplicationPaused = 2015

Field Value

long

NotificationApplicationResumed

Notification received from the OS when the application is resumed.

Specific to the Android platform.

public const long NotificationApplicationResumed = 2014

Field Value

long

NotificationCrash

Notification received from Godot's crash handler when the engine is about to crash.

Implemented on desktop platforms if the crash handler is enabled.

public const long NotificationCrash = 2012

Field Value

long

NotificationOsImeUpdate

Notification received from the OS when an update of the Input Method Engine occurs (e.g. change of IME cursor position or composition string).

Specific to the macOS platform.

public const long NotificationOsImeUpdate = 2013

Field Value

long

NotificationOsMemoryWarning

Notification received from the OS when the application is exceeding its allocated memory.

Specific to the iOS platform.

public const long NotificationOsMemoryWarning = 2009

Field Value

long

NotificationTextServerChanged

Notification received when text server is changed.

public const long NotificationTextServerChanged = 2018

Field Value

long

NotificationTranslationChanged

Notification received when translations may have changed. Can be triggered by the user changing the locale. Can be used to respond to language changes, for example to change the UI strings on the fly. Useful when working with the built-in translation support, like Tr(StringName, StringName).

public const long NotificationTranslationChanged = 2010

Field Value

long

NotificationWMAbout

Notification received from the OS when a request for "About" information is sent.

Specific to the macOS platform.

public const long NotificationWMAbout = 2011

Field Value

long

Methods

HasGodotClassMethod(in godot_string_name)

Check if the type contains a method with the given name. This method is used by Godot to check if a method exists before invoking it. Do not call or override this method.

protected override bool HasGodotClassMethod(in godot_string_name method)

Parameters

method godot_string_name

Name of the method to check for.

Returns

bool

HasGodotClassSignal(in godot_string_name)

Check if the type contains a signal with the given name. This method is used by Godot to check if a signal exists before raising it. Do not call or override this method.

protected override bool HasGodotClassSignal(in godot_string_name signal)

Parameters

signal godot_string_name

Name of the signal to check for.

Returns

bool

InvokeGodotClassMethod(in godot_string_name, NativeVariantPtrArgs, out godot_variant)

Invokes the method with the given name, using the given arguments. This method is used by Godot to invoke methods from the engine side. Do not call or override this method.

protected override bool InvokeGodotClassMethod(in godot_string_name method, NativeVariantPtrArgs args, out godot_variant ret)

Parameters

method godot_string_name

Name of the method to invoke.

args NativeVariantPtrArgs

Arguments to use with the invoked method.

ret godot_variant

Value returned by the invoked method.

Returns

bool

_Finalize()

Called before the program exits.

public virtual void _Finalize()

_Initialize()

Called once during initialization.

public virtual void _Initialize()

_PhysicsProcess(double)

Called each physics frame with the time since the last physics frame as argument (delta, in seconds). Equivalent to _PhysicsProcess(double).

If implemented, the method must return a boolean value. true ends the main loop, while false lets it proceed to the next frame.

public virtual bool _PhysicsProcess(double delta)

Parameters

delta double

Returns

bool

_Process(double)

Called each process (idle) frame with the time since the last process frame as argument (in seconds). Equivalent to _Process(double).

If implemented, the method must return a boolean value. true ends the main loop, while false lets it proceed to the next frame.

public virtual bool _Process(double delta)

Parameters

delta double

Returns

bool

Events

OnRequestPermissionsResult

Emitted when a user responds to a permission request.

public event MainLoop.OnRequestPermissionsResultEventHandler OnRequestPermissionsResult

Event Type

MainLoop.OnRequestPermissionsResultEventHandler