Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The custom Mainloop inherited from SceneTree does not work properly #70764

Closed
Hapenia-Lans opened this issue Dec 31, 2022 · 6 comments · Fixed by #70771
Closed

The custom Mainloop inherited from SceneTree does not work properly #70764

Hapenia-Lans opened this issue Dec 31, 2022 · 6 comments · Fixed by #70771
Milestone

Comments

@Hapenia-Lans
Copy link

Hapenia-Lans commented Dec 31, 2022

Godot version

4.0 beta 10, also 3.5.1

System information

Windows 11

Issue description

Creating a Mainloop inherited from SceneTree makes sense when you want to add a few features to SceneTree instead of starting from scratch. Unfortunately, this doesn't work as well, as if its type is erased.

But inheriting from Mainloop directly (in the example) can work properly.

Steps to reproduce

  1. Create a mainloop extends SceneTree. In this case, I call it MyGDScriptMainLoop.
class_name MyGDScriptMainLoop extends SceneTree

func say_hello():
    print("hello")
  1. Go to project settings, set application/run/main_loop_type to MyGDScriptMainLoop.
  2. Create a test scene and try to call Engine.get_main_loop().say_hello().
extends Node2D


# Called when the node enters the scene tree for the first time.
func _ready() -> void:
    Engine.get_main_loop().say_hello()
    pass # Replace with function body.

Then you will see:

image

And also I found that Engine.get_main_loop() is MyGDScriptMainLoop is false.

Using GDExtension to do this also failed. But since I'm using godot-rust, I'm not sure if this problem exists with godot-cpp.

Minimal reproduction project

bug.zip

@KoBeWi
Copy link
Member

KoBeWi commented Dec 31, 2022

Seems like custom main loop does not take script classes into account. It instantiates the base type, but does not attach the script.
Not sure why it would fail with GDExtension though.

@Hapenia-Lans
Copy link
Author

Seems like custom main loop does not take script classes into account. It instantiates the base type, but does not attach the script. Not sure why it would fail with GDExtension though.

I thought so at first, but I found that the examples in the document worked as well.

image

@KoBeWi
Copy link
Member

KoBeWi commented Dec 31, 2022

Ah right, I forgot about the part where MainLoop works...
No idea then.

@kleonc
Copy link
Member

kleonc commented Dec 31, 2022

The main loop is being set correctly, just too late (both the main loop and its custom script). I've opened PR with a fix.

Before that as a workaround you could e.g. await for a single frame like:

extends Node2D

func _ready() -> void:
	#Engine.get_main_loop().say_hello() # Fails.
	await get_tree().process_frame
	Engine.get_main_loop().say_hello() # Works.

@Hapenia-Lans
Copy link
Author

I also found this issue in 3.5.1. @kleonc

@kleonc
Copy link
Member

kleonc commented Jan 22, 2023

I also found this issue in 3.5.1. @kleonc

@Hapenia-Lans I've already marked #70771 for cherry-picking to 3.x/3.5 (meaning when it will be merged it should be backported) so I guess I've checked that as well back then (now I don't remember:smile:).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants