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

Threads dont work with optional arguments #38042

Closed
2shady4u opened this issue Apr 20, 2020 · 4 comments · Fixed by #38078
Closed

Threads dont work with optional arguments #38042

2shady4u opened this issue Apr 20, 2020 · 4 comments · Fixed by #38078
Milestone

Comments

@2shady4u
Copy link
Contributor

2shady4u commented Apr 20, 2020

Godot version:
v3.2.1

OS/device including version:
Windows 10

Issue description:
Starting a new thread on a function with optional arguments doesn't work as expected.
Both calling the function normally and calling the function by emitting a signal works perfectly as expected.

Minimal reproduction project:

extends Node2D

var thread : Thread

signal test_signal

func _ready():
	thread = Thread.new()
	thread.start(self, "do_stuff")

	connect("test_signal", self, "do_stuff")
	emit_signal("test_signal")
	
	do_stuff()

func do_stuff(optional_var : int = 0):
	print("HURRAY!")

func _exit_tree():
	thread.wait_to_finish()

Gives following error when running:

E 0:00:00.812   _start_func: Could not call function 'do_stuff' to start thread 13480: Invalid Argument #0.
  <C++ Error>   Method failed.
  <C++ Source>  core/bind/core_bind.cpp:2694 @ _start_func()

Evidently running the start thread command with an actual integer as argument does work.

@Calinou
Copy link
Member

Calinou commented Apr 20, 2020

I think this is because the userdata passed to Thread.start() must not be null. This issue is probably a duplicate of another one.

@Faymoon
Copy link

Faymoon commented Apr 20, 2020

I think it may be because on this line we can see that the arg number is hard coded :
https://github.com/godotengine/godot/blob/master/core/bind/core_bind.cpp#L2104

@Faymoon
Copy link

Faymoon commented Apr 21, 2020

I tried to replace the 1 with a zero on the line i linked before and it worked with this code :

extends Node2D

var thread : Thread

func _ready():
	thread = Thread.new()
	thread.start(self, "do_stuff")
	
	do_stuff()

func do_stuff(optional_var : int = 0):
	print("HURRAY!")

func _exit_tree():
	thread.wait_to_finish()

@Faymoon
Copy link

Faymoon commented Apr 21, 2020

Threads don't really works as signals, signals can accept multiples parameters but thread just let you give one userdata, i think it's the reason why it doesn't work here. I'm working on a real fix. (The fix just before prevent all threads to receive userdata.

@YeldhamDev YeldhamDev added this to the 4.0 milestone Apr 21, 2020
akien-mga pushed a commit that referenced this issue Jul 13, 2021
akien-mga pushed a commit to akien-mga/godot that referenced this issue Jul 13, 2021
toasterofbread pushed a commit to toasterofbread/godot that referenced this issue Mar 10, 2022
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