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

GDScript function parameters statically typed as script-defined classes can't be given a default value #26556

Closed
MidZik opened this issue Mar 4, 2019 · 6 comments

Comments

@MidZik
Copy link
Contributor

MidZik commented Mar 4, 2019

Godot version: 3.1 beta 10

Issue description:

class A extends Reference:
	var a

func do_thing(parameter : A = null): # causes error
	pass

func do_different_thing(parameter : A = A.new()): # also causes error
	pass

The above script causes an error on line 4 (the function definition): Value type (null) doesn't match the type of argument 'parameter' (parameter).

Line 7 causes a similar error.

If parameter is statically typed as any built-in class instead, it works fine. It's only for script-defined classes that the error appears.

@ghost
Copy link

ghost commented Mar 4, 2019

Would be nice to allow for this in situations where you follow with if(parameter) to see if one was provided, but I think that it probably has to be a variant to include null.

@vnen Currently, is there a proper way of typing an argument to a specific class, and making it optional at the same time?

@MidZik
Copy link
Contributor Author

MidZik commented Mar 4, 2019

I don't know why I specified Reference specifically, this happens with all custom classes.

GDScript allows you to assign null to variables statically typed as custom classes, so it should do the same for function parameters.

@MidZik MidZik changed the title GDScript function parameters statically typed as custom reference classes can't be defaulted as null GDScript function parameters statically typed as script-defined classes can't be defaulted as null Mar 4, 2019
@akien-mga akien-mga added this to the 3.2 milestone Mar 4, 2019
@MidZik
Copy link
Contributor Author

MidZik commented Mar 4, 2019

I tested a bit more and realized you can't set any default value, it's not limited to just null.

Updating the issue now.

@MidZik MidZik changed the title GDScript function parameters statically typed as script-defined classes can't be defaulted as null GDScript function parameters statically typed as script-defined classes can't be given a default value Mar 4, 2019
@xDGameStudios
Copy link
Contributor

I tested a bit more and realized you can't set any default value, it's not limited to just null.

Updating the issue now.

Can also confirm this:

func do_different_thing(parameter : A = A.new()): # also causes error
	pass

@jabcross
Copy link
Contributor

Seconding this. As we don't have mandatory typing, and we'll probably never have the need for Option or something like it, I feel that this is feature is a must.

@Zylann
Copy link
Contributor

Zylann commented Dec 7, 2019

Still happens in Godot 3.2 beta2

@vnen vnen closed this as completed in 1aef8bf Jan 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants