How to use bitwise operations in gdscript

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By joda
:warning: Old Version Published before Godot 3 was released.

Hi there,

I’m trying to make a scrollable map in gdscript. The way I’m going about this is to use a bit pattern so I can distinguish between certain cases and make my camera move accordingly, depending on the position of the mouse. All possible cases are held in an enum defined on the top of my script and the pattern I want to modify is the scrollMask variable:

var scrollMask = 0
enum SCROLL_DIR { SCROLL_LEFT = 1, SCROLL_RIGHT = 2, SCROLL_TOP = 4, SCROLL_BOTTOM = 8 }

Now for the input handling, I implemented the _input method, checking the InputEventMouseMotion. Here I’d like to test if the mouse is close to the border of the screen and if that’s the case, adjust the scrollMask variable:

# check if we need not to scroll
	# between left and right bounds
	var between = event.position.x > scrollAreaDistance && event.position.x < get_node("Camera2D").get_viewport_rect().size.x - scrollAreaDistance
	# between top and bottom bounds
	between = between && event.position.y > scrollAreaDistance && event.position.y < get_node("Camera2D").get_viewport_rect().size.y - scrollAreaDistance
	if (!between):
		if (event.position.x < scrollAreaDistance):
			scrollMask = scrollMask & SCROLL_DIR.SCROLL_LEFT
			print("left")
		if (event.position.x > get_node("Camera2D").get_viewport_rect().size.x - scrollAreaDistance):
			scrollMask = scrollMask & SCROLL_DIR.SCROLL_RIGHT
			print("right")
		if (event.position.y < scrollAreaDistance):
			scrollMask = scrollMask & SCROLL_DIR.SCROLL_TOP
			print("top")
		if (event.position.y > get_node("Camera2D").get_viewport_rect().size.y - scrollAreaDistance):
			scrollMask = scrollMask & SCROLL_DIR.SCROLL_BOTTOM
			print("bottom")
		print("scrollMask: " + String(scrollMask))
	else:
		scrollMask = 0

However, the output I get is not what I’d expect:
Output of debug log

What am I missing?

And if somebody has a better idea of how to achieve what I’d like to do, every hint is also appreciated :slight_smile:

:bust_in_silhouette: Reply From: jandrewlong

I think you want | (or) not & (and), if you and anything with 0, you get 0.