I've been following the RPG inventory system tutorial from DevLogLogan on youtube, and I am getting an error message he isn't. Its trying to get left and right click when clicking on inventory slots, but it is trying to get the button index of the mouse movement, which is causing an error. I was wondering how I could get the button indexes of left and right click without getting this error. (I am using Godot 4.1 stable from steam)
#How to get left and right click without trying to get mouse movement
9 messages · Page 1 of 1 (latest)
This is an issue with your ands and ors and order of operations
Specifically your parentheses are wrong
Missing a : after InputEventMouseButton
There is not the issue since they are doing a multiline statement with \
As Nisovin pointed out you cannot have and and or like that in the same statement. Simply make a new if statement inside each other to simplify.
Hi there 😊,
To explain what happens here is: ands bind stronger than ors in if-statements, they take precedence.
One expects the first part of the if-statement to act as a guard against all non-button InputEvents, but it just check for Button input AND button left the or-part of the statement is completely free of the guard clause for non-button Events and gets evaluated for every non-button Event (any event even).
To achieve proper guarding one needs to put parentheses around the whole right hand side of the first and.
I presume, like so (using extensive parentheses)
if event is InputEventMouseButton and (((event.button_index == MOUSE_BUTTON_LEFT) or (event.button_index == MOUSE_BUTTON_MASK_RIGHT)) and event.is_pressed()):
...
Sent from mobile client, might contain typos
That said, there is no shame in using multiple ifs to enhance readability (like @Thiago said) or helper function to clean this up.
Hyper optimized if-statements are for later development stages if the last drops of performance juice need to be squished from the code.
One day, if not already, GDScript will just optimize multiple ifs away anyhow.