MainLoop
继承: Object
继承者: SceneTree
Abstract base class for the game’s main loop.
游戏主循环的抽象基类。
Description
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.
MainLoop是Godot项目的游戏循环的抽象基类。它被SceneTree继承,后者是Godot项目中使用的默认游戏循环实现,不过也可以编写和使用自己的MainLoop子类而不是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 main Script is provided from the command line (with e.g. godot -s my_loop.gd, which should then be a MainLoop implementation.
在应用程序启动时,必须向OS提供一个MainLoop实现;否则,应用程序将退出。除非从命令行提供一个主脚本(例如 godot -s my_loop.gd,它应该是一个 MainLoop 实现),否则这个过程会自动发出(和创建一个 SceneTree)。
Here is an example script implementing a simple MainLoop:
这是实现简单MainLoop的示例脚本:
extends MainLoop
var time_elapsed = 0
var keys_typed = []
var quit = false
func _initialize():
print("Initialized:")
print(" Starting time: %s" % str(time_elapsed))
func _idle(delta):
time_elapsed += delta
# Return true to end the main loop.
return quit
func _input_event(event):
# Record keys.
if event is InputEventKey and event.pressed and !event.echo:
keys_typed.append(OS.get_scancode_string(event.scancode))
# Quit on Escape press.
if event.scancode == KEY_ESCAPE:
quit = true
# Quit on any mouse click.
if event is InputEventMouseButton:
quit = true
func _finalize():
print("Finalized:")
print(" End time: %s" % str(time_elapsed))
print(" Keys typed: %s" % var2str(keys_typed))