Can't start thread from instanced class

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By katuiche

Hello!

I’m having some problems with thread execution.
After some debug i isolated the problem but i’m not sure why it’s happening.

In my tests, this code works:

extends Node2D
var thread

func _ready():
    thread = Thread.new()
    thread.start(self, 'test1')

func test1(data):
    print('hey')
    return 0

This prints “hey” as expected.

However, this code doesn’t prints “hey”:

Main node:

extends Node2D
var Class = load('res://class.gd')

 func _ready():
    var c = Class.new()
    c.test1()

Class node:

var thread

func test1():
    thread = Thread.new()
    thread.start(self, 'test2')

func test2(data):
    print('hey')
    return 0

Instead, it gives me the error “ERROR: Could not call function ‘test2’’ starting thread ID: 4956 Reason: Method Not Found”
It seems it can’t find the method in the instanced class. I tried others arguments instead of ‘self’ like the calling instance or sending the instance as argument from the calling instance.

Regards.

:bust_in_silhouette: Reply From: katuiche

I figured out what was the problem:
Only nodes can start processes.

So adding Extends Node in the class file works