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

Allow built in scripts to use class_name #2307

Open
Shadowblitz16 opened this issue Feb 18, 2021 · 11 comments
Open

Allow built in scripts to use class_name #2307

Shadowblitz16 opened this issue Feb 18, 2021 · 11 comments

Comments

@Shadowblitz16
Copy link

Shadowblitz16 commented Feb 18, 2021

Describe the project you are working on

Spaceship game.

Describe the problem or limitation you are having in your project

I am trying to make a custom sprite layer node without needing a extra dangling script.

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

Allow build in scripts to be able to use tool mode and class_name.

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

same way it would for normal scripts

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

it would be used for custom nodes when people don't want it to have a dependency on a script

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

it was already in but was removed for unknown reasons
godotengine/godot#27876

@YuriSizov
Copy link
Contributor

it was already in but was removed for unknown reasons

If you read the issue, it never worked. The PR you link only blocks you from using something that is broken.

@KoBeWi
Copy link
Member

KoBeWi commented Feb 18, 2021

Tool mode already works in built-in scripts.

@Shadowblitz16
Copy link
Author

Shadowblitz16 commented Feb 19, 2021

@pycbouh
oh my bad, can it be made to work?

@KoBeWi but does class_name?
I don't know how to actually register my scene as a node due to the fact that class_name is not supported with built in scripts.

@KoBeWi
Copy link
Member

KoBeWi commented Feb 19, 2021

class_name doesn't work with built-in scripts and it would be difficult to make it work. Registered classes point to scripts paths, but built-in scripts don't have a path. You'd need to point to the sub-resource of the scene, which means that to load that class you need to load whole scene and look for the specific resource. Theoretically we could extract single sub-resource from inside a scene, but Godot doesn't support that right now and it might require significant changes to how scene loading work.

EDIT:

I don't know how to actually register my scene as a node

This is impossible right now. There was an old issue about it: godotengine/godot#21187
but looks like no one made a proposal yet (or it wasn't linked).

As a workaround, you can instance the scene inside your class.

@Shadowblitz16
Copy link
Author

Shadowblitz16 commented Mar 21, 2021

@KoBeWi then consider this my proposal (which is what I made it for).

I think loading things from string paths should be obsoleted anyways (unless its a resource).
we should use type names like every other programming language and game engine

@YuriSizov
Copy link
Contributor

Script is a resource. And what you say doesn't make sense: there is always some path that needs to be loaded, even if you don't see it, even if it's always loaded in a global scope. You can't load something into memory without having a file path to read from.

@Shadowblitz16
Copy link
Author

Shadowblitz16 commented Mar 22, 2021

@pycbouh a script is a resource yes but the type is not.

The idea would be that it would be compiled and linked by type.
how else do you think other languages work?
the only thing you need is the script (resource) for is modifying the code

@YuriSizov
Copy link
Contributor

YuriSizov commented Mar 22, 2021

how else do you think other languages work?

I don't know which languages do you mean, but you need to explicitly load scripts in all of them. Some may provide syntactic sugar where namespaces and class names are translated into file system paths, but that's irrelevant to the underlying implementation that always associates a script/class with a file. It's literally impossible to get rid of a file path, because that's where the script is defined at.

"Registered classes point to scripts paths" is true for every language in existence. Because your runtime needs to know where to get a class and then load it. Unless you propose we parse and load all scripts and scenes all the time, assign them a class name and then just reference that in memory. Which would be highly inefficient.

The problem that KoBeWi is describing has nothing to do with class to path association. It has to do with the fact that built-in scripts are not individual files by definition, but rather are data stored in a scene. So to load a script you need to load the whole scene, at least partially by parsing its contents to find where the script is and what it is. This is by design, of course, because that's the whole point of bundling and coupling data like that. If you want independent and fully capable scripts you should store them separately from scenes.

@Shadowblitz16
Copy link
Author

Shadowblitz16 commented Mar 24, 2021

@pycbouh most languages use the class name not load(...) to load the type
thats what I am trying to say

then we can just reference nodes by class_name in the scene files and there is no need for files
if there is a type missing that a missing dependency

EDIT: if thats the case and what KoBeWi said has nothing to do with class_names then something like that should be worked on.

Its a major issue that we are forced to have to have separate scripts and scenes when there really is no point due to the fact we can only have 1 script per node.

it just clutters the project tree

@Shadowblitz16
Copy link
Author

closing in favor of this #2612

@Shadowblitz16
Copy link
Author

Shadowblitz16 commented May 16, 2023

I am reopening this.
I ran into this issue again and it would be nice to have

@Shadowblitz16 Shadowblitz16 reopened this May 16, 2023
@Shadowblitz16 Shadowblitz16 changed the title Allow built in scripts to use class_name and tool Allow built in scripts to use class_name May 16, 2023
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

4 participants