summaryrefslogtreecommitdiff
path: root/doc/classes
diff options
context:
space:
mode:
authorMax Hilbrunner <mhilbrunner@users.noreply.github.com>2021-09-16 02:22:37 +0200
committerGitHub <noreply@github.com>2021-09-16 02:22:37 +0200
commit47f7b4b7ca0975d7274f1004d616f383cfc6a450 (patch)
tree0e0c8e6efd98dd3755fbba57bb7658726ac3b013 /doc/classes
parent4317d3a4a26bb9ebaca7b788452f06f6688d8625 (diff)
parentfc30909e0594d3fc30e89d0d6bcb6f72c03b987f (diff)
Merge pull request #43240 from HaSa1002/docs-mainloop
Docs: Fix Mainloop example
Diffstat (limited to 'doc/classes')
-rw-r--r--doc/classes/MainLoop.xml54
1 files changed, 35 insertions, 19 deletions
diff --git a/doc/classes/MainLoop.xml b/doc/classes/MainLoop.xml
index 11124a1436..1df00f556a 100644
--- a/doc/classes/MainLoop.xml
+++ b/doc/classes/MainLoop.xml
@@ -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>