Attention: Here be dragons

This is the latest (unstable) version of this documentation, which may document features not available in or compatible with released stable versions of Godot.

MainLoop

繼承: Object

被繼承: SceneTree

遊戲主迴圈的抽象基底類別。

說明

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 MainLoop Script is provided from the command line (with e.g. godot -s my_loop.gd) or the ProjectSettings.application/run/main_loop_type project setting is overwritten.

Here is an example script implementing a simple MainLoop:

class_name CustomMainLoop
extends MainLoop

var time_elapsed = 0

func _initialize():
    print("Initialized:")
    print("  Starting time: %s" % str(time_elapsed))

func _process(delta):
    time_elapsed += delta
    # Return true to end the main loop.
    return Input.get_mouse_button_mask() != 0 || Input.is_key_pressed(KEY_ESCAPE)

func _finalize():
    print("Finalized:")
    print("  End time: %s" % str(time_elapsed))

方法

void

_finalize() virtual

void

_initialize() virtual

bool

_physics_process(delta: float) virtual

bool

_process(delta: float) virtual


訊號

on_request_permissions_result(permission: String, granted: bool) 🔗

當使用者對許可權請求作出反應時發出。


常數

NOTIFICATION_OS_MEMORY_WARNING = 2009 🔗

當套用程式超過其分配的記憶體時,從作業系統收到的通知。

僅限 iOS 平臺。

NOTIFICATION_TRANSLATION_CHANGED = 2010 🔗

當翻譯可能發生變化時收到的通知。會在使用者改變區域設定時觸發。可以用來回應語言的變化,例如即時改變 UI 字串。可配合內建的翻譯支援使用,比如 Object.tr()

NOTIFICATION_WM_ABOUT = 2011 🔗

當發出“關於”資訊請求時,從作業系統收到的通知。

僅限 macOS 平臺。

NOTIFICATION_CRASH = 2012 🔗

當引擎即將當機時,從Godot的當機處理常式收到的通知。

如果當機處理常式被啟用,這只會在桌面平臺上實作。

NOTIFICATION_OS_IME_UPDATE = 2013 🔗

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

Implemented on desktop and web platforms.

NOTIFICATION_APPLICATION_RESUMED = 2014 🔗

當應用程式從暫停恢復時,從作業系統收到的通知。

僅適用於 Android 與 iOS 平台。

NOTIFICATION_APPLICATION_PAUSED = 2015 🔗

當應用程式被暫停時,從作業系統收到的通知。

僅適用於 Android 與 iOS 平台。

注意:在 iOS 上,這個訊號觸發後,你大約只有 5 秒可以完成相關任務。超過這個限制,iOS 將會直接終止應用程式,而非單純暫停。

NOTIFICATION_APPLICATION_FOCUS_IN = 2016 🔗

當應用程式獲得焦點時,從作業系統收到的通知,即當焦點從作業系統桌面或第三方應用程式切換到 Godot 任一開啟視窗時。

在桌面與行動平台皆有實作。

NOTIFICATION_APPLICATION_FOCUS_OUT = 2017 🔗

當應用程式失去焦點時,從作業系統收到的通知,即當焦點從 Godot 任一開啟視窗切換到作業系統桌面或第三方應用程式時。

在桌面與行動平台皆有實作。

NOTIFICATION_TEXT_SERVER_CHANGED = 2018 🔗

文字伺服器被更改時,收到的通知。

NOTIFICATION_APPLICATION_PIP_MODE_ENTERED = 2019 🔗

Notification received when the application enters picture-in-picture mode.

NOTIFICATION_APPLICATION_PIP_MODE_EXITED = 2020 🔗

Notification received when the application exits picture-in-picture mode.


方法說明

void _finalize() virtual 🔗

在程式退出前呼叫。


void _initialize() virtual 🔗

在初始化時呼叫一次。


bool _physics_process(delta: float) virtual 🔗

Called each physics tick. delta is the logical time between physics ticks in seconds and is equal to Engine.time_scale / Engine.physics_ticks_per_second. Equivalent to Node._physics_process().

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

Note: _physics_process() may be called up to Engine.max_physics_steps_per_frame times per (idle) frame. This step limit may be reached when the engine is suffering performance issues.

Note: Accumulated delta may diverge from real world seconds.


bool _process(delta: float) virtual 🔗

Called on each idle frame, prior to rendering, and after physics ticks have been processed. delta is the time between frames in seconds. Equivalent to Node._process().

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

Note: When the engine is struggling and the frame rate is lowered, delta will increase. When delta is increased, it's capped at a maximum of Engine.time_scale * Engine.max_physics_steps_per_frame / Engine.physics_ticks_per_second. As a result, accumulated delta may not represent real world time.

Note: When --fixed-fps is enabled or the engine is running in Movie Maker mode (see MovieWriter), process delta will always be the same for every frame, regardless of how much time the frame took to render.

Note: Frame delta may be post-processed by OS.delta_smoothing if this is enabled for the project.