Merge pull request #43240 from HaSa1002/docs-mainloop

Docs: Fix Mainloop example
This commit is contained in:
Max Hilbrunner 2021-09-16 02:22:37 +02:00 committed by GitHub
commit 47f7b4b7ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 35 additions and 19 deletions

View File

@ -5,15 +5,14 @@
</brief_description>
<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.
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. [code]godot -s my_loop.gd[/code], which should then be a [MainLoop] implementation.
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. [code]godot -s my_loop.gd[/code] or the "Main Loop Type" project setting is overwritten.
Here is an example script implementing a simple [MainLoop]:
[b]FIXME:[/b] No longer valid after DisplayServer split and Input refactoring.
[codeblock]
[codeblocks]
[gdscript]
class_name CustomMainLoop
extends MainLoop
var time_elapsed = 0
var keys_typed = []
var quit = false
func _initialize():
print("Initialized:")
@ -22,24 +21,41 @@
func _process(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_keycode_string(event.keycode))
# Quit on Escape press.
if event.keycode == KEY_ESCAPE:
quit = true
# Quit on any mouse click.
if event is InputEventMouseButton:
quit = true
return Input.get_mouse_button_mask() != 0 || Input.is_key_pressed(KEY_ESCAPE)
func _finalize():
print("Finalized:")
print(" End time: %s" % str(time_elapsed))
print(" Keys typed: %s" % var2str(keys_typed))
[/codeblock]
[/gdscript]
[csharp]
using Godot;
using System;
public class CustomMainLoop : MainLoop
{
public float TimeElapsed = 0;
public override void _Initialize()
{
GD.Print("Initialized:");
GD.Print($" Starting Time: {TimeElapsed}");
}
public override bool _Process(float delta)
{
TimeElapsed += delta;
// Return true to end the main loop.
return Input.GetMouseButtonMask() != 0 || Input.IsKeyPressed((int)KeyList.Escape);
}
private void _Finalize()
{
GD.Print("Finalized:");
GD.Print($" End Time: {TimeElapsed}");
}
}
[/csharp]
[/codeblocks]
</description>
<tutorials>
</tutorials>