0 votes

Hi everybody!

I am trying to make a "Castlevania II - Simon's Quest" type of game.

After studying some tutorials I have managed to organize the base of the game.
Now I am facing this problem:
the main character pushes a crate and reveals a door, then a message (a label attached to the character) appears asking the player if he wants to enter by pushing the UP button.
Normally the player wouldn't be able to move the character and only be able to push that single button.
Right now, instead, I can move the character and the label which is rather comic...
How could I "freeze" the character?

Hope my question is clear.

Best
Pancotto

Godot version 3.5.1
in Engine by (14 points)
edited by

1 Answer

0 votes

There are many ways to achieve what you want to do and state machines (FSM) are by fare the best option since you can do multiple things with the same key bindings.

State Machine

# Character.gd

extends Node

enum STATES {IDLE, WALK, INTERACT}

var state = STATES.IDLE

func _change_state(new_state):
    state = new_state

func _input(event):
    match state:
        STATES.IDLE:
            if event is InputEventKey and event.scancode == KEY_W:
                if is_near_door:
                    change_state(STATES.INTERACT)
        STATES.WALK:
            if event is InputEventKey and event.scancode == KEY_W:
                if is_door_opened:
                    change_state(STATES.INTERACT)
        STATES.INTERACT:
            if event is InputEventKey and event.scancode == KEY_W:
                if is_door_opened:
                    change_state(STATES.WALK)
                else:
                    change_state(STATES.INTERACT)

Demo

2D Finite State Machine

by (6,876 points)

hi!
This seems to be very advanced!
Not sure how to implement this.

For the moment I've found some kind of workaround with variables.

If door == 0 (the character is not touching the door) then every button is abled;
if door == 1 (the character is touching the door) all keys are disabled except the UP button.

That seems to work as expected.

Best

What you're doing is creating a bool switch.

var door : bool = false

if not door:
    door = true

However there are only 2 possible states that work like a light switch off and on and for your type of game as it grows in complexity consider using FSM as it works more like a traffic light.

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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.