0 votes

Just trying to continue a 2d isometric game from Godot 2 to 3. Have an issue with character controller, 'func _input(event):' related.
Here's the script:

extends KinematicBody2D

# movement related variables and constants
var vel
var moveDir

# direction constants
var DIR_LEFT = Vector2(-64, -32).normalized()
var DIR_RIGHT = Vector2(64, 32).normalized()
var DIR_UP = Vector2(64, -32).normalized()
var DIR_DOWN = Vector2(-64, 32).normalized()
var DIR_ZERO = Vector2(0, 0)

const MAX_SPEED = 1.30
const MOVE_FORCE = 30.0
const GRID = 20

var animSprite
var animPlayer

func _ready():
    # initializing params
    vel = DIR_ZERO
    moveDir = DIR_ZERO
    animSprite = get_node("animSprite")
    animPlayer = get_node("animPlayer")

    # enabling process
    set_physics_process(true)
    set_process_input(true)


func _physics_process(delta):
    # calculate velocity
    var deltaV = moveDir * MOVE_FORCE * delta
    if vel.length() + deltaV.length() <= MAX_SPEED:
        vel += deltaV
    else: # cap  max velocity
        vel = moveDir * MAX_SPEED

    # move the char if dir is non-zero
    if moveDir != DIR_ZERO:
        move_and_collide((vel)


# process button press from player
func _input(event):
    # controls for PC
    if event is InputEventKey:
        # deciding the direction of movement and setting animation
        if event.is_action_pressed("down_move") && !event.is_echo():
            moveDir = DIR_DOWN
            animSprite.set_frame(67)
            animPlayer.play("walk_T2B")

        if event.is_action_pressed("up_move") && !event.is_echo():
            moveDir = DIR_UP
            animSprite.set_frame(50)
            animPlayer.play("walk_B2T")

        if event.is_action_pressed("left_move") && !event.is_echo():
            moveDir = DIR_LEFT
            animSprite.set_frame(57)
            animPlayer.play("walk_R2L")

        if event.is_action_pressed("right_move") && !event.is_echo():
            moveDir = DIR_RIGHT
            animSprite.set_frame(62)
            animPlayer.play("walk_L2R")

        # set moveDir as zero upon release and pause the animation
        else (event.is_action_released("down_move") && moveDir == DIR_DOWN)
            || (event.is_action_released("up_move") && moveDir == DIR_UP)
            || (event.is_action_released("left_move") && moveDir == DIR_LEFT)
            || (event.is_action_released("right_move")&& moveDir == DIR_RIGHT):
                    moveDir = DIR_ZERO
                    # pause the animation
                    animPlayer.stop(true)

    #controls for touch devices — to be expanded later
    if event.type == InputEvent.SCREEN_TOUCH:
        moveDir = DIR_RIGHT
asked Jan 5 in Projects by jymis (15 points)

Are you saying that somewhere in this script you have an error? Can you indicate what line?

It says at 'func _input(event):' line.
But i guess error comes from inside that function.

1 Answer

0 votes
Best answer

The line before the error is the cause:

move_and_collide((vel)

Unbalanced parentheses.

answered Jan 5 by kidscancode (13,248 points)
selected Jan 5 by jymis

Indeed.

But now i have an error at that line:
'else (event.isactionreleased("downmove") && moveDir == DIRDOWN)'
expecting ':' at the end of the line

There is a ':' but with several '||' operators between, maybe that's the problem, but i don't know how to correct it.

An else line can't contain anything else, it's just else:

Maybe you meant elif ? I don't know, that code seems a mess.

It was 'if' at first, I've put it back instead of 'else' right now,
but i get the same error.

Maybe that part with all action_released stuff should be outside of that function ?

Sry, I'm not a dev, but an artist trying to move an old project to Godot 3

Ok i've fixed it.

All '[direction]move' were the old InputMap. It's 'ui[direction]' now.

Plus, I've breaked the 'isactionreleased' part down into four 'if'.

Thank you :)

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.