godotengine / godot-proposals

Godot Improvement Proposals (GIPs)
MIT License
1.12k stars 69 forks source link

Allow us to strongly type packed scenes #782

Open Shadowblitz16 opened 4 years ago

Shadowblitz16 commented 4 years ago

Describe the project you are working on: 3d Tower defense

Describe the problem or limitation you are having in your project: there is no way to strongly type packed scenes

Describe the feature / enhancement and how it helps to overcome the problem or limitation: can we have a some sort of generics for packed scenes that allow for strongly typed instancing

Describe how your proposal will work, with code, pseudocode, mockups, and/or diagrams: something like..

var scene : PackedScene(Spatial)

exporting generic packed scenes would also insure type safety in the inspector

If this enhancement will not be used often, can it be worked around with a few lines of script?: it would be used alot and no not safely

Is there a reason why this should be core and not an add-on in the asset library?: supports better code

Jummit commented 4 years ago

I actually get autocompletion for packed scenes, so I think this isn't needed. If you are programmatically loading PackedScenes, you can always define the type after instancing it.

Shadowblitz16 commented 4 years ago

autocompletion has nothing to do with this problem it has to do with type safety

Jummit commented 4 years ago

autocompletion has nothing to do with this problem it has to do with type safety

Ah, gotcha. That is something I didn't think about.

Shadowblitz16 commented 4 years ago

using this suggestion https://github.com/godotengine/godot-proposals/issues/1207 it would be something like this..

var scene : PackedScene<Spatial>
Shadowblitz16 commented 3 years ago

closing in favor of.. https://github.com/godotengine/godot-proposals/issues/2699

Shadowblitz16 commented 3 years ago

reopening due to people might needing to export packed scenes only instead of both packed scenes and nodes

Shadowblitz16 commented 1 year ago

This might be better as...

@export var scene_or_node_path : Control

This we can drag in a scene or a node as long as it's that type. Unity does this and it's really nice.

salmelo commented 11 months ago

I'd love to see this feature, coming from a unity background, but I'd really prefer if scenes and nodes inside scenes didn't both fit in the same variable. I can't think of any situation where you'd actually want a variable that could be either one, you'll have references to other nodes in the scene you're in and you'll have references to scenes you want to instantiate at runtime, but not both in the same box.

DaloLorn commented 11 months ago

If #1935 was implemented, then you actually could treat a node class as equivalent to its bound scene (if any) at runtime, since instantiating the node would automatically instantiate the scene. This would then permit us to extend that equivalence to the editor, offering the corresponding type safety and autocompletion improvements.

You could, admittedly, not expect that a PackedScene[Spatial] was a Spatial (since it only needs to have a Spatial as its root node), but you could expect that a Spatial is a PackedScene[Spatial] (because it can't not have a fully-functioning Spatial as its root node). I'm not sure if that explanation makes any sense?