0 votes

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?

asked May 8 in Engine by _matt_day_ (17 points)

1 Answer

+2 votes
Best answer

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...
answered May 8 by jgodfrey (5,742 points)
selected May 11 by _matt_day_

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
Input.set_mouse_mode(Input.MOUSE_MODE_HIDDEN)

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.

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

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.