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

Export scene by attached script Type #10388

Open
codevogel opened this issue Aug 7, 2024 · 2 comments
Open

Export scene by attached script Type #10388

codevogel opened this issue Aug 7, 2024 · 2 comments

Comments

@codevogel
Copy link

Describe the project you are working on

A little 2d top-down strategy game.

Describe the problem or limitation you are having in your project

Coming from a Unity background, I am used to being able to assign a Prefab of a certain Type

e.g. I could export a Player on my PlayerSpawner and it would only accept prefabs that have the Player component attached.

As Godot's 'replacement' for Prefabs is Scenes, I would like to see something similar possible with scenes.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

Right now, if I have a PlayerSpawner, and expose a Player with export, I can only select already existing instances of Player in the inspector.

I would like to also be able to select scenes that have the class_name Player script attached to their root node.

Right now, I can either:

  • refer to the player 'prefab' scene by path: This is pretty bad given now it relies on a hardcoded path.
  • @export var player_scene: PackedScene: With this, we can select ANY scene, even things that are not a Player, like a Door. So, we end up having to do a cast on instantiating the node, to verify that we are indeed spawning a Player.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

Since Node only allows one gdscript to be attached, and a Scene always has a Root node, I would imagine it would be possible to restrict the Inspector to only select scenes that have the Player script attached to their root node.

e.g. return a list of scenes for the inspector that are filtered by something like this (pseudocode):

func is_scene_selectable(scene: PackedScene):
    return true if scene.attached_script? is player else false

If this enhancement will not be used often, can it be worked around with a few lines of script?

Yes, for example, each time you instantiate a scene, you check it's type, but since this is something that happens often, it results into more and more lines of code.

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

Since it would also assume that you don't need to cast the player scene node to a Player.

@codevogel
Copy link
Author

Related to:

* [Allow us to strongly type packed scenes #782](https://github.com/godotengine/godot-proposals/issues/782)

* [Allow type specification of root node for exported PackedScenes #5255](https://github.com/godotengine/godot-proposals/issues/5255)

* [Add a generic parameter to PackedScene (like Array) to allow for easier type safety #6694](https://github.com/godotengine/godot-proposals/issues/6694)

Thanks, I did some digging but I couldn't find these issues at the time of posting

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