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

Is it possible to call rust function from a new thread in Godot 4.2? #745

Closed
AllenDang opened this issue Jun 7, 2024 · 2 comments
Closed
Labels
question Not a problem with the library, but a question regarding usage.

Comments

@AllenDang
Copy link

AllenDang commented Jun 7, 2024

I define the rust class like this

#[derive(GodotClass)]
#[class(base=RefCounted)]
struct Test {
    base: Base<RefCounted>,
}

#[godot_api]
impl IRefCounted for Test {
    fn init(base: Base<RefCounted>) -> Self {
        Self {
            base,
        }
    }
}

#[godot_api]
impl Test {
    #[func]
    fn call_me_in_a_new_thread() {
        godot_print!("hello from another thread");
    }
}

In godot I tried to call the rust function like this

extends Node3D

var thread


func _ready() -> void:
	thread = Thread.new()
	thread.start(test)


# Thread must be disposed (or "joined"), for portability.
func _exit_tree():
	thread.wait_to_finish()


func test():
	var op = Test.new()
	op.call_me_in_a_new_thread()

It will cause an immediate crash. Is it possible to do that?

@lilizoey
Copy link
Member

lilizoey commented Jun 7, 2024

we currently havent figured out how to do this safely. see #18.

if you enable the experimental-threads feature then this shouldn't crash anymore. but you are then acknowledging that it's possible to break safety in subtle ways and it's up to you to enaure nothing bad happens.

@lilizoey lilizoey added the question Not a problem with the library, but a question regarding usage. label Jun 7, 2024
@Bromeon
Copy link
Member

Bromeon commented Jun 9, 2024

Closing in favor of #18.

@Bromeon Bromeon closed this as completed Jun 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Not a problem with the library, but a question regarding usage.
Projects
None yet
Development

No branches or pull requests

3 participants