Hello ! I'm not sure how other people do it, but I'm making my second game with Godot and I've been missing operator overloading in each of them. Specifically, the _str
overload, like suggested in #6306
It's mostly for development and debugging convenience. For example, I made a IntVector8
-like for my internal coordinates system, with custom methods, and I have to create clunky printing functions for printing arrays of those, or all I get is [[Reference:5678], [Reference:5679], [Reference:5680], [Reference:5681], [Reference:5682]]
. And every time, I'm frustrated.
Meanwhile, I made my own Godot build with #6306 in it, but… :see_no_evil:
Hence, here is a formal request for operator overloading in gdscript, so that people can vote on to show interest.
@fire @groud
Hi ! What's confusing about operator overloading ? Is it the design pattern you don't like ? The performance cost ? Maybe it's the 8-dimensional space ? What am I missing ?
Sorry, I missclicked on my phone.
The idea might be interesting, but I'm not sure if it is worth implementing.
We have to keep the language simple and maintainable, so it might not be worth adding.
Full operator overloading looks heavy and complicated, I'll admit.
As I mentioned, I'm happy with only the _str
virtual method, but, to quote @akien-mga from #6306 :
we need to implement full operator overloading before this makes sense
I don't grok why. Also, how come you're not frustrated by the lack of _str
? I'm always printing dictionaries and arrays containing instances of custom classes, and I can't use the debugger to explore custom classes. Maybe it's because I'm learning Godot as I go, and abusing the print()
helps _a lot_ understanding stuff.
we need to implement full operator overloading before this makes sense
I was only quoting @reduz from that same issue :) I don't have a strong opinion about it either, I'm not sure what's involved by operator overloading, so IMO making _str
overriddable would be OK.
The main operator overload I would like is array access for use in custom container classes. Somthing like a func _op_index(index:int).
it would be SUPER cool if we could also get the basic math operators (+, -, *, /, %) but that's not as important. I am implementing a Matrix class in gd script right now and right now I have methods like func multiply(other:Matrix) so code like:
var new_mat = matrix1.multiply(matrix2)
which I personally find to be less clear than
var new_mat = matrix1 * matrix2
so even though in this case implementing the operator overload makes creating the original class more complex, for actual end-user usage of the class it makes things simpler.
I suggest the naming _op_xxxxxx for operator overloads
Are there any updates to this? It's been almost 3 years since #6306, and I believe having _str
overridable would be very Godot-ish. It would really come in handy for debugging because things like [Node:1337]
are not helpful at all. This requires us to write a lot of boilerplate code to use things like print(node.custom_title)
(because even class_name
cannot be used in GDString at the moment.)
Fixed by #27886.
Most helpful comment
Are there any updates to this? It's been almost 3 years since #6306, and I believe having
_str
overridable would be very Godot-ish. It would really come in handy for debugging because things like[Node:1337]
are not helpful at all. This requires us to write a lot of boilerplate code to use things likeprint(node.custom_title)
(because evenclass_name
cannot be used in GDString at the moment.)