Solve This Problem

4.4.1 (Stable)

"Hey game devs! I am creating such a top down 2d game that have two or more character’s movement controls

(Say , Joystick , Mouse , Keyboard)

I have script for them but the main problem I face is that how will I make them changed using 3 toggle Buttons specifically designed for each type of movement. In short how will I make a whole script be controlled through Toggle buttons e.g If I turn on Mouse then all other Script parts ( Joystick and keyboard ) will automatically be turned off until the button it turned off so that I can give users variety of Player Controler."

You could use a state machine with a state for each control input.

So the logic of detecting a control change, needs to change the state accordingly.

1 Like

The “Godot” way of doing this is to accept all three types of input at once, so hypothetically your player could be using a gamepad stick for movement but the keyboard for buttons. Unless you’re worried about mischievous brothers/sisters, this usually works fine.

If you do want to specifically set the mode, @zoern’s state machine idea would work fine:


enum InputMode { gamepad, mouse, keyboard }

var CurrentInputMode: InputMode = InputMode.gamepad

#[...]

func _process(delta: float) -> void:
    #[...]
    match CurrentInputMode:
        InputMode.gamepad:
            # do gamepad stuff
        InputMode.mouse:
            # do mouse stuff
        InputMode.keyboard:
            # do keyboard stuff

func switch_to_mouse() -> void:
    CurrentInputMode = InputMode.mouse

#[...]

A lot of the rest would just be setting up your input map.

1 Like

Sorry for the disturbance but can you please tell the basic steps for this only in words as I am really confused

I will look for that

Specifically there is a chapter about “Implementing a Finite State Machine with nodes”.
State machines are really useful. Implementing one for yourself is a good learning experience that you’ll reuse later

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.