I have an algorithm in a game that simplified looks like this:
if click:
if a and not b and not c:
b = True
c = True
follow_mouse_pos()
if b and c:
b = False
c = False
stay_on_slot()
This is for an inventory system, and the first if statement picks up an item from the clicked slot, and the other one puts it on the clicked slot. The problem I have is that I want to have the same mouse button for both actions, but if I do that, the item just flickers between the slot and the mouse position.
a, b and c are conditions I have set up to decide wether I pick up the item or I put it in a slot.
a is irrelevant here because it is just for visual stuff. b is True if the mouse is holding an item c is True if the clicked slot is empty
The problem is that meeting one of the statements requirements will lead to meeting the conditions of the other statement, and it will loop as fast as the main loop goes. This is a problem because then I have to time it every time I want to move something in the inventory, and that is not good.
I have tried if elif statements but since this is in a loop, the only difference is that on each loop only one of them will be checked, but one of them will always be met.
Is there any way I can avoid this problem?
The full project is on github
The file that contains this part of the code is here: VoidShips/scripts/GUI/gui.py at line 203
One possibility is that instead of one event "click", you have one like "mouse button up" and another "mouse button down". Like this you could maybe build something like a drag and drop if that is what you want.