0 votes

Hi, I am quite new to Godot engine. I have written a simple FPS character (GDScript) according to a yt video. When I press play, I can look around and should be able to move, but if I press anything (even mouse buttons). Then the game freezes and takes no input at all (cant even close the window)

in Engine by (18 points)

It sounds like you have an error. Is there something in the "Debugger" window?

takes no input at all (cant even close the window)

I've had this happen to me before with an accidental infinite loop. Are you detecting input anywhere?

its kind of weird it says: Invalid get index "relative" (on base InputEventKey) eventhough i have made an if to make sure it is the mouse moving and not the keyboard pressing that triggers the get of "relative". If i understand right "relative" is a variable in the function handling mouse input? Anyway I found out that the error only occurs in editor. If i run the project from the project browser it works completely fine. Do you have any idea why? Thanks for very quick response.

Please share your code. It sounds like you're receiving a key input (all inputs are processed by _input()). You probably need to check the event type first.

extends KinematicBody

var cameraAngle = 0
var mouseSensitivity = -0.3

var velocity = Vector3()
var direction = Vector3()

const flySpeed = 1
const flyAccel = 4

#######################################################################################################################

func _ready():
    # Called when the node is added to the scene for the first time.
    # Initialization here
    pass

###########################################################################################################################

func _physics_process(delta):                 
    direction = Vector3()
    var aim = $Head/Camera.get_camera_transform().basis

    if Input.is_action_pressed("moveForward"):
        direction -= aim.z
    if Input.is_action_pressed("moveBackwards"):
        direction += aim.z
    if Input.is_action_pressed("moveLeft"):
        direction -= aim.x
    if Input.is_action_pressed("moveRight"):
        direction += aim.x

    direction = direction.normalized()

    var target = direction * flySpeed

    velocity = velocity.linear_interpolate(target, flyAccel)
    move_and_slide(velocity)

#############################################################################################################################

    #Handles Mouse Input
func _input(event):
    if event is InputEventMouseMotion:
        $Head.rotate_y(deg2rad(event.relative.x * mouseSensitivity))

    var change = event.relative.y * mouseSensitivity

    if change + cameraAngle < 90 and change + cameraAngle > -90:
        $Head/Camera.rotate_x(deg2rad(change))
        cameraAngle += change

    else:
        pass

1 Answer

+1 vote

Here's your problem (unless the indentation was messed up by your copy-and-paste):

func _input(event):
    if event is InputEventMouseMotion:
        $Head.rotate_y(deg2rad(event.relative.x * mouseSensitivity))

    var change = event.relative.y * mouseSensitivity

    if change + cameraAngle < 90 and change + cameraAngle > -90:
        $Head/Camera.rotate_x(deg2rad(change))
        cameraAngle += change

You're testing for InputEventMouseMotion, but only applying it to the rotation of the head. The line setting change is run for every input, regardless of type. You probably want to place the rest of the code under the if as well.

Note: In future, when posting code, please format it correctly so that it is readable. This means either placing 4 spaces in front of all lines or, as a shortcut, clicking the "Code Sample" formatting button (it looks like two curly brackets: {}).

by (20,677 points)

Ok thanks for the formatting tips. the copy paste didnt mess up anything. I actually thougt the change variable only were called when the if returned true. by putting all the code under the if, do you mean to just press TAB to move the code a bit to the right?

Yes. In GDScript (much like Python), the indentation indicates a code block. The code that is indented under the if statement "belongs" to it and is only run if the statement is true.

OK, i see. Im used to program in C like languages. in those languages the "if block" is put inside these {} things. Thanks for the help. It worked now whitout any errors. Do i need to mark anything as solved somewhere?

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.
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 webmaster@godotengine.org with your username.