-
-
Notifications
You must be signed in to change notification settings - Fork 22k
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
Add Trait System to GDScript #97657
base: master
Are you sure you want to change the base?
Add Trait System to GDScript #97657
Conversation
Is there a specific reason you specified a If not, might I recommend a different return type for clarity? |
(Edited because I missed a part in the OP description) Fantastic start on this feature. Thank you! One comment: please use |
I'm not sure your reasoning lines up with your conclusion there, but I can't say I have much of a preference, what with it being a strictly cosmetic affair. |
This system seems very similar to the |
Abstract classes are still beholden to the class hierarchy: No class can inherit from two classes at a time. There is some value in having both of these, I suppose, but traits are far more powerful. |
See: Also, as DaloLorn said, these are independent features that can coexist together. Traits offer capabilities that classic inheritance cannot provide. |
This looks great, will traits be able to constrain typed collections (ie. Array[Punchable], Dictionary[String, Kickable]) ? |
Amazing that somebody cared to make this,but since the original proposal is shut down,here is some feedback
|
Considering work has already been made for signals, we should get to keep them too. (unless massive performance issues appear) |
I am a bit concerned on the performance of this in general, but that would be something that can be solved over time. I am really, really ecstatic about this. I agree. There's no reason to exclude signals from traits if the work has been done. |
36d7605
to
4088f53
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just some nitpicks, mostly you should rename uses
with impl
everywhere.
As various others have suggested to use
The 6 characters
|
what would you abbreviate
Pretty sure non-native speakers are able to understand abbreviations, by your logic
traits aren't exactly beginner stuff, when someone starts with a language they learn they might learn traits ,but for gamedev you don't learn certain stuff until you get the basics/become a casual programmer , when i was a unity developer, i didn't learn about interfaces (which are extremely similar to traits) until i had advanced enough and realised i need some other solution to inheritance
this makes no sense? let's take a look at some example code: class_name Door
extends AnimatableBody3D
impl Interactable what would "implies" mean in a progammer context?, "impels" isn't even abbreviated correctly, "implant"? seriously?, "implode" would be a function for gameplay
previous points still matter, also rust uses the |
Is using a separate file extension necessary? And if not, would it be better to stick to .gd? From a UX perspective it seems a lot simpler and easier not to. |
Extensions can be useful for quick search, filter, etc., without the need to check the content of the file nor adding extra prefix/suffix to file names (so it's better in UX terms), also can help other tools like the filesystem to implement custom icons. |
Put me down as another vote in favor of "implements", for what it's worth. I'm indifferent on "implements" versus "uses", but I'm not nearly so indifferent on "impl" versus "implements": The majority of Adriaan's concerns have not been addressed to my satisfaction. |
I think |
I agree on "impl" been a very confusing keyword. |
To resolve the class reference error: Compile godot, then run |
f27a5d9
to
8d2b91c
Compare
Initially intended, however I will rework it |
This comment was marked as resolved.
This comment was marked as resolved.
1715c33
to
8d0fcbc
Compare
In the recent push, exported trait-typed variables now align with the expected export behavior. Additionally, traits extending Nodes and Resources have been removed from various selection and creation windows. |
Made an interaction system with this, very pleased have other issues tho IssuesVariablestrait variables override the class variables and the only way to access the shadowed variables is to cast this also works even if the trait variable's type is different from the class variable also untested, but i think the overriding variables with the same name and type count as 2 different variables, not sure so check Functionstrait's functions work very weird with existing methods when the trait extends the trait and the class will fight over the function definition and if you somehow settle it, the engine will give you a warning for overriding a class method that the trait demands (in this case the Possible solutionsVariableswhile the best solution is to simply not have variables in traits, i doubt it will happen so here is another possible solution if the variable has the same name and type as a class's property, it will refer to that property (basically an alias/ptr if you will) for the different type, i was going to suggest an alt to btw did anyone check what happens if traits share a variable names? Functionsif the function definition matches an existing function in a base class, you won't need to override it, this also solves my case if a function's name but return_type/parameters don't match, there is a hacky solution i found func MyTrait.my_class_func(same: Parameter) -> AndReturnType:
pass
func other():
#`tbase` stands for trait base
tbase[MyTrait].my_class_func(blah_blah)
source: |
Exported Resources and exported Nodes work correctly now. |
So Redot has already found this bug Redot-Experimental/RedotX@ed49262. can we track it? |
We don't track bugs for un-merged features, but if the author of this wants to check that out for this PR that would make sense |
Traits should not override native class functions, that looks like a bug to me i dont think that is an expected usage of traits. If the base class is CharacterBody2D it should throw an error as soon as you try to implement If your trait doesn't inherit from a class, but names a function, in this example, move_and_collide, then it should throw an error when attempting to use it
|
extends CharacterBody2D
uses BadTrait
trait BadTrait:
extends CollisionObject2D
func move_and_collide() Fixed by providing error
Done QuickOpenDialog now asks for trait types only when the resource script is changed, making it more optimized from the previous push |
8f467ee
to
c01de46
Compare
EditorFileSystemDirectory *dir = EditorFileSystem::get_singleton()->get_filesystem_path(resource_script_path.get_base_dir()); | ||
int resource_script_index = dir->find_file_index(resource_script_path.get_file()); | ||
if (resource_script_index > -1) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use find_file()
to get both the directory and index.
@@ -176,6 +176,17 @@ String EditorFileSystemDirectory::get_file_script_class_extends(int p_idx) const | |||
return files[p_idx]->class_info.extends; | |||
} | |||
|
|||
Vector<String> EditorFileSystemDirectory::get_file_script_subtypes(int p_idx) const { | |||
if (files[p_idx]->class_info.script_subtypes_modified_time < files[p_idx]->modified_time) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's not how cached data is usually handled in EditorFileSystem.
Check how other members of class_info
are handled. The information should be stored in the filesystem cache file, so it's not re-initialized in every editor session.
GDScript Trait System
Based on the discussion opened in:
The GDScript trait system allows traits to be declared in separate
.gdt
files or within atrait SomeTrait
block.Traits facilitate efficient code reuse by enabling classes to share and implement common behaviors, reducing duplication and promoting maintainable design.
Syntax Breakdown
Declaring Traits
Traits can be defined globally or within classes.
In a
.gdt
file, declare a global trait usingtrait_name GlobalTrait
at the top.trait
used for inner traits.Traits can contain all class members: enums, signals, variables, constants, functions and inner classes.
Example:
Using Traits in Classes
Use the
uses
keyword after theextends
block, followed by the path or global name of the trait.Traits can include other traits but do not need to implement their unimplemented functions. The implementation burden falls on the class using the trait.
Example:
Creating Trait files.
How Traits Are Handled
Cases
When a class uses a trait, its handled as follows:
1. Trait and Class Inheritance Compatibility:
The trait's inheritance must be a parent of the class's inheritance (compatible), but not the other way around, else an error occurs. Also note traits are pass down by inheritance, If a class is for instance "SomeTrait" also it here classes will be so.
Example:
2. Used Traits Cohesion:
When a class uses various traits, some traits' members might shadow other traits members ,hence, an error should occur when on the trait relative on the order it is declared.
3. Enums, Constants, Variables, Signals, Functions and Inner Classes:
These are copied over, or an error occurs if they are shadowed.
4. Extending Named Enums:
Named enums can be redeclared in class and have new enum values.
Note that unnamed enum are just copied over if not shadowing.
5. Overriding Variables:
This is allowed if the type is compatible and the value is changed.
Or only the type further specified. Export, Onready, Static state of trait variables are maintained. Setter and getter is maintained else overridden (setters parameters same and the ).
6. Overriding Signal:
This is allowed if parameter count are maintained and the parameter types is compatible by further specified from parent class type.
Example:
7. Overriding Functions:
Allowed if parameter count are maintained, return types and parameter types are compatible, but the function body can be changed. Static and rpc state of trait functions are maintained.
8. Unimplemented (Bodyless) Functions:
The class must provide an implementation. If a bodyless function remains unimplemented, an error occurs. Static and rpc state of trait functions are maintained.
9. Extending Inner Classes:
Inner classes defined in used trait can be redeclared in class and have new members provide not shadow members declared inner class declared in trait. Allow Member overrides for variables, Signals and function while extending Enum and its' Inner Classes.
Example:
Special Trait Features
10. Trait can use other Traits:
A trait is allows to use another trait except it does not alter members of the trait it is using by overriding or extending.
However, cyclic use of traits (TraitA uses TraitB and TraitB uses TraitA) is not permitted and will result in error.
11. Tool Trait:
if one trait containing the
@tool
keyword is used it converts classes (except inner classes) and traits using it into tool scripts.12. File-Level Documentation:
Member documentation is copied over from trait else overridden.
System Implementation Progress
as
)is
).gdt
files unattachable to objects/nodesBugsquad edit: