Change mouse interaction behaviour based on selected "power"

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By matt_day

So I’m very new to Godot but have some very basic knowledge of Python and stuff so i can just about understand things. I’m creating a 2D platformer where the player has different powers accessed through pressing the number keys 1-4, where each key means the mouse performs different function. For example, pressing 1 selects a telekinesis ability where the player can drag and drop physics objects to solve puzzles and stuff, and pressing 2 switched this ability to an electro-bolt that can be used to power machinery and stuff like that. The idea being that once you switch from telekinesis to electro-bolt, you cant drag and drop physics objects any more. What I’m asking is how I could possibly start to manage this?

:bust_in_silhouette: Reply From: jgodfrey

The details of actually doing the things you mention (telekenesis, electro-bolt, …) are too broad for this message. However, for basic management of the mouse, I’d suggest using some sort of global mouse_state variable that indicates which state the mouse is currently in via a set of appropriate enum values. So, something like:

# create appropriate mouse states
enum mouse_states { NORMAL, TELEKENESIS, ELECTRO_BOLT, ..., ..., ... }

Then, when the number 1-4 keys are pressed on the keyboard, update some global mouse state variable to track the current state. You could even assign some named keyboard actions to your 4 number keys.

var mouse_state = mouse_states.NORMAL

func _process(delta):
   if Input.is_action_just_pressed("telekenesis"):
       mouse_state = mouse_states.TELEKENESIS
   elif Input.is_action_just_pressed("electrobolt"):
        mouse_state = mouse_states.ELECTRO_BOLT

Then, in your various functions to handle the “powers”, you’d just need to verify that the mouse is in correct mode for a given power. So, something like:

func do_telekenesis():
    if mouse_state != mouse_states.TELEKENESIS: return
    # do telekenesis stuff here...

This all makes perfect sense to me and i can’t see why it shouldn’t work, but something does’t seem to work.
Did this:

enum mouse_power_state { NORMAL, TK }
var mouse_state = mouse_power_state.NORMAL

Currently i have this here to set the mouse to the right mode:

func _process(delta):
if Input.is_action_just_pressed("ui_up"):
	mouse_state = mouse_power_state.TK

Then below as a test i have this section of code:

func _do_tk():
if mouse_state != mouse_power_state.TK: return

The idea being that if i press up the overall result should be the mouse cursor disappearing to test this section of code, yet nothing happens when i press up.

matt_day | 2020-05-11 16:14

How / where does _do_tk() get called? Also, the posted _do_tk() function body isn’t properly indented (at least as posted).

jgodfrey | 2020-05-11 16:33

Lmao im gonna sound like such an idiot but i commented it out and only just realised ._.
All works fine now ive un-commented it

matt_day | 2020-05-11 16:46