IssueRaiser

Add a generic parameter to PackedScene (like Array) to allow for easier type safety

### Describe the project you are working on A game in which the player can shoot bullets. The class `Bullet` has an `init` method that sets it up and returns `self`. `bullet_scene` contains a scene in which the root has the `Bullet` script. ### Describe the problem or limitation you are having in your project I want to instance and set up the bullet like so: ```gdscript var bullet = bullet_scene.instantiate() \ .init(Vector2.UP * 200) ``` which works fine but it isn't type safe. I can't have godot infer the type of bullet with `:=` and its because `instantiate` just returns a node of unknown type. I can't autocomplete `Bullet`'s methods and mistakes aren't caught until they come up in runtime ### Describe the feature / enhancement and how it helps to overcome the problem or limitation A typed `PackedScene` that, when `instantiate` is called, returns a value statically typed as whatever type it only supposed to be able to contain. ### Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams Godot 4 brings typed arrays where getting an item from them automatically types that value as the type they are marked to contain. ```gdscript var array : Array[Bullet] array.append(Bullet.new()) array[0].init(Vector2.UP) # green line numbers, autocomplete! ``` Seeing that this is possible, I imagine it wouldn't be too much more difficult to type a PackedScene in a similar way, say ```gdscript @export var bullet_scene : PackedScene[Bullet] ``` In

Posted by jaymehta-g on Apr 14, 2023, 1:33 AM

Tags

Related issues

Raise real-world problems, find people facing the same issue, and discuss solutions.2,500 issues0 comments

ux (Software)

Add a generic parameter to PackedScene (like Array) to allow for easier type safety

jaymehta-gApr 14, 2023, 1:33 AM

### Describe the project you are working on A game in which the player can shoot bullets. The class `Bullet` has an `init` method that sets it up and returns `self`. `bullet_scene` contains a scene in which the root has the `Bullet` script. ### Describe the problem or limitation you are having in your project I want to instance and set up the bullet like so: ```gdscript var bullet = bullet_scene.instantiate() \ .init(Vector2.UP * 200) ``` which works fine but it isn't type safe. I can't have godot infer the type of bullet with `:=` and its because `instantiate` just returns a node of unknown type. I can't autocomplete `Bullet`'s methods and mistakes aren't caught until they come up in runtime ### Describe the feature / enhancement and how it helps to overcome the problem or limitation A typed `PackedScene` that, when `instantiate` is called, returns a value statically typed as whatever type it only supposed to be able to contain. ### Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams Godot 4 brings typed arrays where getting an item from them automatically types that value as the type they are marked to contain. ```gdscript var array : Array[Bullet] array.append(Bullet.new()) array[0].init(Vector2.UP) # green line numbers, autocomplete! ``` Seeing that this is possible, I imagine it wouldn't be too much more difficult to type a PackedScene in a similar way, say ```gdscript @export var bullet_scene : PackedScene[Bullet] ``` In

Comments (0)

No comments yet. Start the discussion.

Comment on this issue