-
-
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
Add XOR operator to GDScript #3849
Comments
Related to #230 (which was previously rejected). |
Why? What doesn't work?
|
|
Do you have an example where bool does not work but xor would be expected to? I can't think of one. And if there is, that would be more likely a problem with bool instead of the lack of xor. |
Exactly, wrapping each condition in |
Here's a list of related discussions so far starting from 2014:
It appears to me that @vnen is not completely against the feature according to those discussions, I think it's just that it's one of those features which has low priority. However, my interpretation may be certainly wrong so we need to get @vnen to either reject or approve this once and for all. Sounds good? Otherwise I'm afraid these XOR proposals will come up from time to time. You have to be aware of Godot's development philosophy. |
Does anyone have an example where Also note that for integers, Godot already has a bitwise XOR |
There are currently no And it's strange to force cast to
|
I have to repeat the first condition as a redundant else, otherwise i could just XOR the first one. |
Well, this can still be two branches, you can just expand XOR into two checks, no? (A OR B) AND (A <> B) for the second branch would allow you to remove the first. |
You didn't really do what I'd described. You don't need the first case. You need only the second case and your old "else" case to achieve the XOR behavior. When (A OR B) AND (A <> B), go into your second case, otherwise use your old else case.
PS. I assume there is some typo, because the first case you have is for when neither is true which already won't be captured by the second case which is for when either is true. Either way, you have two boolean flags, so the overall point of mine stands, as that's what XOR expands into, whatever logic you actually want inside of the conditional branch. PPS. Well, like Aaron mentioned, |
Ah okay:
That seems to work, I still think XOR would be useful to keep things simpler and less verbose, though. |
Does it? That condition doesn't make sense, because when the left part is |
I did it this way instead:
Me still want XOR. |
XOR makes very little sense in programming as a separate operation. Some languages have it for convenience mostly. The easiest way to implement XOR in GDScript would be |
That's the point, to make it more convenient. |
For me this is all about readability of source code and with better readability comes better maintainability. GDScript supports both an OR and an AND operator although one can be transformed into the other - see |
@elvisish in your code above you treat XOR (exclusive or) as NAND (not and), those are different operators NAND:
NAND can be implemented as XOR:
XOR can be implemented as |
@elvisish also if you want bitwise NAND then you can just use anyway, this proposal appears to be about NAND operator: but in that case there also would need to be operators for NOR (not or) and XNOR (not xor): NOR:
NOR is XNOR:
XNOR for booleans is already |
@dalexeev if you read the code in the first post |
I still think that |
Quite recently I had to hand-implement XOR for cycles detection in my racer. (And I do mean XOR, 0 XOR 0 = 0 and 0 XOR 1 = 1, I wanted to detect the latter case) |
maybe practical examples where |
I quite often write |
I honestly didn't know there was so much to this, it's been a real education! (At least I know I wasn't looking for XOR!) |
I think the readability is the strongest argument here, despite few talking about it. Godot's styleguide encourages using OR, AND, and even NOT instead of ||, &&, and ! (though I admit that last one feels a bit cursed in many cases). We should also allow for XOR instead of != and probably ^^ instead of != as well. What I'm currently working on with reflection is a decent showcase of where it does not read well with != where XOR would be more clear and also the opposite.
I'd prefer this for readability. Notice I'm using both XOR and != with booleans because in one case I am thinking "either this or that" and the other I am thinking "this does not equal that".
It seems the PR is most of the way through being merged but still wanted to comment as this reasoning didn't seem properly displayed and there had been some push-back in the threads I've read. |
Describe the project you are working on
Any game.
Describe the problem or limitation you are having in your project
These don't work as a workaround for XOR:
collision.get("normal").y > 0.99 != get_floor_normal().y > 0.99
collision.get("normal").y > 0.99 ^ get_floor_normal().y > 0.99
I have to invert the logic by returning from the function if both are true, rather than not satisfying the condition if either both not both are true:
collision.get("normal").y > 0.99 and get_floor_normal().y > 0.99: return
Describe the feature / enhancement and how it helps to overcome the problem or limitation
As above.
Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams
Maybe the word XOR and ^ extended from just bitwise operations.
If this enhancement will not be used often, can it be worked around with a few lines of script?
As written above, but not the best solution.
Is there a reason why this should be core and not an add-on in the asset library?
It's a GDScript enhancement.
The text was updated successfully, but these errors were encountered: