Godot-proposals: Add a boolean XOR operator to GDScript

Created on 14 Nov 2019  路  9Comments  路  Source: godotengine/godot-proposals

Describe the project you are working on:
Working on a simple game.

Describe the problem or limitation you are having in your project:
i was just trying to toggle a Panel visibility on and off

Describe how this feature / enhancement will help you overcome this problem or limitation:
It could be written very naturally like any other language, typing that much code for something that simple seems weird and unpractical

Show a mock up screenshots/video or a flow diagram explaining how your proposal will work:
See below

Describe implementation detail for your proposal (in code), if possible:
the syntax could be something like :
$Panel.visible = $Panel.visible ^^ true;
additionally a ^^= operator could be provided
$Panel.visible ^^= true;

If this enhancement will not be used often, can it be worked around with a few lines of script?:
it can definitely be replaced by a few line of script, but it also seems like a decent language should have that operator

Is there a reason why this should be core and not an add-on in the asset library?:
I guess it can't be an addon i that case

gdscript

Most helpful comment

This can be achieved with the following one-liner:

$Panel.visible = !$Panel.visible

GDScript also offers the & (AND), | (OR) and ^ (NOT) bitwise operators, which are useful for enabling or disabling flags:

some_texture.flags |= Texture.FLAG_FILTER

All 9 comments

Well, for booleans, it's basically equivalent to the != operator.

I'm so used to ^ in c++ i didn't even think about using != in fact

This can be achieved with the following one-liner:

$Panel.visible = !$Panel.visible

GDScript also offers the & (AND), | (OR) and ^ (NOT) bitwise operators, which are useful for enabling or disabling flags:

some_texture.flags |= Texture.FLAG_FILTER

Yes, i can agree that my request is not that great in retrospective.
It is just that is seems weird to have the bitwise xor and not the boolean xor
but we can probably all live with that

C++ doesn't have a boolean/logical XOR operator either.

Yes but you can still use it on bools

The difference between xor and != is that we can't do that:

print(123 != "string") # Error

(See https://github.com/godotengine/godot/issues/26375.)

And this is correct:

print(123 && "string") # True
print(123 || "string") # True

Even if you accept 26375, 123 != "string" must be true.

# print(123 xor "string") # False
print(bool(123) != bool("string")) # False
print(!123 && "string" || 123 && !"string" ) # False

While != works in my scenario I don't think ^^ is equal to !=. I tend to think of != and == as having lower priority than && and ||. The way I am thinking about it they always occur last.
false || true ^^ false && false would evaluate to false, while
false || true != false && false would evaluate to true
because false || true != false && false would equal (false || true) ^^ (false && false)
They also have different connotations which make it more readable to use one over the other in certain scenarios as mentioned in previous posts.

Mixing the or/|| and and/&& operators is generally discouraged; likewise, mixing them with a xor/^^ operator should be discouraged as well.

Was this page helpful?
0 / 5 - 0 ratings