Merge pull request #43240 from HaSa1002/docs-mainloop
Docs: Fix Mainloop example
This commit is contained in:
commit
47f7b4b7ca
1 changed files with 35 additions and 19 deletions
|
@ -5,15 +5,14 @@
|
||||||
</brief_description>
|
</brief_description>
|
||||||
<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.
|
[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]:
|
Here is an example script implementing a simple [MainLoop]:
|
||||||
[b]FIXME:[/b] No longer valid after DisplayServer split and Input refactoring.
|
[codeblocks]
|
||||||
[codeblock]
|
[gdscript]
|
||||||
|
class_name CustomMainLoop
|
||||||
extends MainLoop
|
extends MainLoop
|
||||||
|
|
||||||
var time_elapsed = 0
|
var time_elapsed = 0
|
||||||
var keys_typed = []
|
|
||||||
var quit = false
|
|
||||||
|
|
||||||
func _initialize():
|
func _initialize():
|
||||||
print("Initialized:")
|
print("Initialized:")
|
||||||
|
@ -22,24 +21,41 @@
|
||||||
func _process(delta):
|
func _process(delta):
|
||||||
time_elapsed += delta
|
time_elapsed += delta
|
||||||
# Return true to end the main loop.
|
# Return true to end the main loop.
|
||||||
return quit
|
return Input.get_mouse_button_mask() != 0 || Input.is_key_pressed(KEY_ESCAPE)
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
func _finalize():
|
func _finalize():
|
||||||
print("Finalized:")
|
print("Finalized:")
|
||||||
print(" End time: %s" % str(time_elapsed))
|
print(" End time: %s" % str(time_elapsed))
|
||||||
print(" Keys typed: %s" % var2str(keys_typed))
|
[/gdscript]
|
||||||
[/codeblock]
|
[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>
|
</description>
|
||||||
<tutorials>
|
<tutorials>
|
||||||
</tutorials>
|
</tutorials>
|
||||||
|
|
Loading…
Reference in a new issue