Why is any input (else than mouse movement) stopping my game?

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

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)

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

kidscancode | 2018-10-21 16:28

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?

markopolo | 2018-10-22 19:07

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.

Elias | 2018-10-24 17:51

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.

kidscancode | 2018-10-24 18:31

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

Elias | 2018-10-24 20:45

:bust_in_silhouette: Reply From: kidscancode

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: {}).

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?

Elias | 2018-10-25 16:25

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.

kidscancode | 2018-10-25 18:37

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?

Elias | 2018-10-25 19:01