I've been using boolean values to switch integer and float values between positive and negative (as in, if the boolean is true, the integer value is positive, else it is negative) successfully, but i can't help but feel like the way I'm doing this is in an over complicated manner, as i tend to overlook simple solutions to simple problems. Perhaps there is a simpler way of doing this? Here are the two techniques i have found to work. player_offset.x = abs(player_offset.x)*(int(LeftRight)*2-1) This one changes the X component of a Vector2 value. It does this by converting the boolean "LeftRight" to an integer, which would be either 1 or 0, then multiplying the integer by 2 and subtracting 1, resulting in either 1 if the value is true, or -1 if it is false. The absolute value of the Vector2 X value is multiplied by this result. Here is the other method: player_offset.x = abs(player_offset.x) * [-1,1][int(LeftRight)] This one works by using the boolean converted to an integer as an index for a list containing -1 and 1 to multiply the vector component. If the value is false, then the vector component is multiplied by -1, else it is multiplied by 1. Both of these methods work, but I feel like they are unnecessarily complicated, and could perhaps lead to performance issues as they are called every frame.

Does anyone know a simpler way to do this?

Personally, I’ve always just used code like this:

if (LeftRight == true):
	player_offset.x = abs(player_offset.x)
else:
	player_offset.x = abs(player_offset.x) * -1

Or another thing I sometimes use:

player_offset.x = abs(player_offset.x)
if (LeftRight == false):
	player_offset.x *= -1

I’m not sure if it is better or not, it’s certainly longer, but that’s generally what I use and it seems to work okay most of the time. I generally like longer bits of code that are (in my opinion) easier to read than shorter one liners. It is just my personal preference.


I know in JavaScript you can use ternary if statements, and apparently it is supported in GDScript through this syntax a if x else b. So, to use it with a Boolean, it would look something like this (in theory, untested)

player_offset.x = abs(player_offset.x) * (1 if LeftRight else -1)

Not sure how helpful it would be, but I might consider trying ternary if statements since they are designed, more or less, for this kind of thing.

As for performance on any of the above I wrote, I don’t know. I imagine not converting a Boolean to an Integer will yield slightly better performance overall, but likely it is to be minimal. Personally, I am of the mind to not worry about optimizations until needed, since it is easy to spend lots of time optimizing without knowing whether it will yield better performance.

Hopefully this helps!

(Side note: Welcome to the forums!)

I don't especially like the implementation of the ternary operator in gdscript, but it is probably the best way to do this. val * (1 if LeftRight else -1)

I assume the language is optimised to deal with this script more quickly than instantiating an array but I haven't run a performance test.

4 years later