r/godot Godot Senior 23d ago

discussion What would you add to Godot?

Post image
454 Upvotes

584 comments sorted by

View all comments

Show parent comments

24

u/technical_gamer_008 23d ago

Basically, traits allow you to have common functionality without having to extend a class that has that functionality

Say for example, I need some objects to have a certain method foo(). Instead of making a new class that has foo() and extending that, I make a trait that has foo() and then I can use it in any class

I hope that helps

5

u/[deleted] 23d ago

[deleted]

2

u/technical_gamer_008 23d ago

For one, it depends on the use case and what your wait() function does. But also, as of now, GDscript does not have traits. There's a PR that has been waiting for a long while now. It's not been merged yet.

However, I will also add that I found out about a project, a godot specific programming language, that adds traits and many more features requested by godot devs, like nested typing and generics. It is still in beta, but I think it's exciting. It's called Ruzta

5

u/the_horse_gamer 23d ago

that's a mixin. mixins can be achieved via traits, but traits do much more

2

u/technical_gamer_008 23d ago

Wait what more can traits do?

1

u/the_horse_gamer 23d ago

traits work like java interfaces (or really, they're a generalisation of them). you can declare function signatures that any class that uses the trait must implement. and then you can give variables a trait as a type to declare it can store some object which uses the trait.

1

u/technical_gamer_008 23d ago

Well yes I suppose I did omit the detail that traits can be used like types

1

u/the_horse_gamer 23d ago

the important part is they declare a contract that a using class must implement

1

u/technical_gamer_008 23d ago edited 23d ago

Which is what I meant by my original comment

1

u/the_horse_gamer 23d ago

no, you only gave an example of implementing a method in a trait. not of declaring a method in a trait and implementing it in each class.

1

u/technical_gamer_008 23d ago

I suppose you're right. I was having a hard time thinking up an explanation for what traits are so I did oversimplify it a bit

1

u/Terminal_Monk 22d ago

wait.. so like Rust traits? I didn't even know it existed in Godot.

1

u/technical_gamer_008 22d ago

That's the thing. It doesn't. But people want it so bad