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
- This is the biggest problem with contracts, there's simply no way to actually say "please always execute this check".
- I don't disagree with you on this, I worked my whole life in corporate and haven't worked as a OSS maintainer before, though I will _and ...
- the new VSCode UI, looks cute, i don't like it.
- I've been working on my own private harness for the past 8 months, and I've been collecting ideas from such repos I've stumbled upon.
- The way AI is able to interact with outside resources is pretty impressive, but the quality of code it produces to me is still questionab...
- Add PR-style review comments in Git Diff UI that feed into the active agent thread
Raise real-world problems, find people facing the same issue, and discuss solutions.2,500 issues0 comments
ux (Software)
This is the biggest problem with contracts, there's simply no way to actually say "please always execute this check".
I don't disagree with you on this, I worked my whole life in corporate and haven't worked as a OSS maintainer before, though I will _and ...
the new VSCode UI, looks cute, i don't like it.
I've been working on my own private harness for the past 8 months, and I've been collecting ideas from such repos I've stumbled upon.
The way AI is able to interact with outside resources is pretty impressive, but the quality of code it produces to me is still questionab...
Add PR-style review comments in Git Diff UI that feed into the active agent thread
Thanks for indulging me so far, by the way, I really appreciate this discussion, it's very stimulating.
Migrate GDScript to the GDExtension mechanism and overhaul the implementation
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
Comments (0)
No comments yet. Start the discussion.