0 votes

I am trying to write a custom copy-data function. I would like to give it a stronger typing so I can get the auto-complete. In other words, I have this:

# res://MyClass.gd
func copyFrom( other ):
    value = other.value
    # ...

but would like this:

# res://MyClass.gd
func copyFrom( other : MyClass ):
    value = other.value
    # ...

I have tried a few things with these frustrating findings:
It looks like you can't preload a class in it's own file.
It looks like load results can't be made const, so they can't be used as a type, (I think?)
I would add class_name but that seems to give me all kinds of circular errors if I try to use the file anywhere else.

There may not be a way to do it right now (3.1), and that's fine, too. I just would like to know the answer if it is possible.

in Engine by (32 points)

1 Answer

0 votes

Looks like it may be possible in v4 to use a class_name in it's own file, but for now it isn't doable. See
https://github.com/godotengine/godot/issues/21461#issuecomment-751329701

That issue has a few workaround suggestions. It seems that in 3.1.2+ you can sometimes use as or is with the class_name. (Like assert( other is MyClass )) That seems finicky and other files that uses that class can show errors and have trouble getting dependencies right. I think I'll just leave it all dynamic typed until v4... insert the usual joke here.

by (18 points)

Seems like this is the expected behavior.

# res://MyClass.gd
func copyFrom( other ):
    if other is MyClass:
        value = other.value
    elif other is SomeOtherClass:
        pass
# ...

All other kinds of class compare or use cases returns circular errors like the OP said

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.

Categories