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

Polymorphism doesn't work on a typed arrays #93084

Closed
GitHubPiotr opened this issue Jun 12, 2024 · 2 comments
Closed

Polymorphism doesn't work on a typed arrays #93084

GitHubPiotr opened this issue Jun 12, 2024 · 2 comments

Comments

@GitHubPiotr
Copy link

Tested versions

Tested on Godot 4.3beta1

System information

Windows 10, Forward+

Issue description

I noticed that polymorphism doesn't work on arrays of a specific type. Example below

Steps to reproduce

  1. Create new scene and attach a script
  2. In the script create two new empty classes. Extend the second class by the first class
    t1
  3. In _ready() function create variable of Array[Class that extends another class]
    t3
  4. Create another variable of Array[Your primary class] and assign it with variable from step 3
    t4
  5. Done. You getting a compiler error
    t2

Minimal reproduction project (MRP)

test.zip

@AThousandShips
Copy link
Member

This is a current limitation, please see:

@dalexeev
Copy link
Member

Duplicate of:

See:

This is not a bug. Arrays are passed by reference; they do not copy the entire content. Otherwise you would get:

var a: Array[Node2D]
var b: Array[Node]
b = a # Now `b` refers to the same array as `a`.
b.append(Label.new()) # Valid, `Label` is a subtype of `Node`.
print(a[0]) # Invalid, `Label` is **not** a subtype of `Node2D`.

Probably the only way this would work is if the GDScript supported read/write qualifiers. But it's too difficult.

Thanks for the contribution nonetheless!

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

3 participants