Warn when creating a script with the same name as the parent class #47686
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds a warning when creating a script if the class name is the same as the parent class.
This is most useful for C# users, but it's also relevant for GDScript/VisualScript/GDNative users as well (for those, it's a best practice thing, not a technical limitation, or rather, a technical inconvenience).
For example, let's say that you created this Enemy class in C#, which is a KinematicBody3D:
Then you want to create a class for your player, but you forgot to rename the node to "Player" or similar, and you also forgot to change the name of the file to something other than
KinematicBody3D.cs
. So you get this:The C# script generator has code to handle this case, so it will mark it as extending
Godot.KinematicBody3D
. However, this breaks theEnemy
class, since now the behavior and variables from the player will show up inside Enemy, since Enemy is now extendingKinematicBody3D
fromKinematicBody3D.cs
instead ofGodot.KinematicBody3D
. To be clear, this is not a hypothetical problem, I've seen this happen to someone in the past.One solution would be to make all new scripts
: Godot.Something
instead of: Something
, however there would still be confusion if someone usednew KinematicBody3D
instead ofnew Godot.KinematicBody3D
.Also, even if this problem didn't exist (and it doesn't in GDScript etc), I would still recommend as a best practice that people don't name their scripts the same as a built-in type for the sake of clarity.
The solution I've come up with is this PR which will warn users if they try to create a script with the same name as the parent type. Really we would want to check if the class name is the same as any built-in type, but I'm not sure what's the best way to implement that, and checking if it's the same as the parent class will cover the most common cases for this warning.