Can we overload functions in GDScript? Like - at all?
Can we overload functions in GDScript? Like - at all?
How cool would that be!? Like having multiple constructors for a class or having a move
method with the parameters (x:float, y:float) OR (relative:Vector2) ! That'd be super cool and useful I thinks <3
It's not a thing and I totally agree it should exist, there's a proposal for it on GitHub.
If you want to handle different types, the right way of doing it is giving your parameter a generic type then checking what it is in the function.
This prints
10 is an integer
andHello is a string
.If you really, really need to have a variable amount of arguments in your function, you can pass an array. It's pretty inefficient but you can get away with it.
Then you can pass [10, 20, 30] into it or something. It's a useful trick.
I prefer passing a dict, easier to check what was given and what wasn't.