-
-
Notifications
You must be signed in to change notification settings - Fork 97
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
Allow custom classes to define their own operator behavior #8383
Comments
Objects in Godot are quite hefty. Creating new Objects is relatively slow, so for these purposes I would discourage using them. But when structs are implemented into GDScript (it's just a matter of time), I feel like this would be a fitting upgrade to them.
But Godot as a whole supports Variant. func __add__(right: Variant) -> Complex:
if right is Complex:
return Complex.new(r + right.r, i + right.i)
if right is int or right is float:
return Complex.new(r + right, i)
# Probably generate an error here, too.
return null I'm aware it would be slower, but other things in GDScript do this as well, so it may come as expected. |
I realize my proposal is heavily pythonic, so if we'd rather make it in line with GDScript standards, having different function names (such as |
See also #279 and #7329. Operator overloading would be nice, it's a great feature in C++, C#, etc. For your specific use case, you can use Vector2 to represent complex numbers, and it will work with addition, but you would need to define your own Or, alternatively, you can use Quaternion, and leave Computing the Mandelbrot set is likely to be a heavy computation, so I would avoid using |
Just want to drop in and +1 the idea of operator overloading. Certainly one could just use C++ instead but considering how many other fantastic qol features gdscript has built in, it would be a huge benefit to be able to overload at the very least basic operators, such as the mathematical operators. |
This comment was marked as off-topic.
This comment was marked as off-topic.
@advent94 Please don't bump without contributing significant new information. Use the 👍 reaction button on the first post instead. |
I've had a number of situations where I've built a wrapper around an existing GDScript data type (such as an array) to add specific functionality, and being able to overload arithmetic and For my specific case of passing calls down to a member variable something like Ruby's method_missing would be helpful, though it feels like more of a stretch than asking for overloading |
I took a quick dive into the source code, and I don't think it should be super difficult to implement. Dynamic Type approach (python-like)
Static Type approachAlternatively, one could require registering operator overloads by explicit types, letting Godot check which overload should be used. This could be a bit more verbose but solves the I agree that this would be undesirable for many cases where operators are usually overloaded, due to objects being kind of slow, but it wouldn't be any worse than any normal object calls. I'm not sure how proposals like this are usually decided on by the Godot community, but I'm willing to take a crack at it if it's wanted and has a chance of being merged. |
Describe the project you are working on
A Mandelbrot set visualization tool
Describe the problem or limitation you are having in your project
This project requires the use of complex numbers (with a real and imaginary component). Godot, understandably, does not support complex numbers, so a custom class is needed to represent them. However to do something involving math for these complex numbers (e.g., addition), I have to now write and call custom functions to do this, instead of using the built-in math operators.
Describe the feature / enhancement and how it helps to overcome the problem or limitation
The feature enhancement would allow custom classes (such as the complex number class described above) to define their own operators so that standard mathematical (or other) operators could be supported.
Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams
I propose this should work in a similar way to Python's current implementation. This would involve having pre-defined function names for the operators that, when implemented, would open up the ability to perform those operations, with the behavior tied to the function's implementation. Essentially, this would be "syntactic sugar" that converts certain operators to a function call, if present.
This is also in keeping with GDScript's other paradigms for class functions such as
_init
,_process
,_draw
, etc. where defining those function allow someone writing GDScript to add features to a class that are automatically recognized by the complier.Here's a proposed example:
This would then allow you to do the simple addition in your other scripts:
Rather than the more convoluted function call:
Since GDScript doesn't (to my knowledge) support function overloads, there could be a variety of these magic functions to support other types (even custom types):
If no function is defined for a given math operation, then it can be a variation on the current not implemented error:
Invalid operands 'Complex' and 'Vector3' in operator '+'.
If this enhancement will not be used often, can it be worked around with a few lines of script?
This enhancement would be completely opt-in, so anyone who doesn't want to use it would be unaffected.
Is there a reason why this should be core and not an add-on in the asset library?
I believe this is a core GDScript change and do not think it could be done in an asset library.
The text was updated successfully, but these errors were encountered: