I'm facing some issues with my code anim_switch and the variable new anim = srt(animation.spritedir)

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

I’m facing some issues with my code, when i put the function anim_switch and the variable new anim = srt(animation.spritedir) and the rest of code the godot return me the error invalid get index ‘spritedir’ (on base ‘string’), what i can do. The version of my godot is 3.1. I want that my character switch your animation when i press any key, but the code don’t run. Can someone help me?

I’ll put my code in there just in case someone can help

extends KinematicBody2D

const SPEED = 70

var movdirecao = Vector2(0,0)
var spritedir = “down”
var motion

func _physics_process(delta):
controls_loop()
movement_loop()
spritedir_loop()

if is_on_wall():
	if spritedir == "left" and test_move(transform, Vector2(-1,0)):
		anim_switch("push")
	if spritedir == "right" and test_move(transform, Vector2(1,0)):
		anim_switch("push")
	if spritedir == "up" and test_move(transform, Vector2(0,-1)):
		anim_switch("push")
	if spritedir == "down" and test_move(transform, Vector2(0,1)):
		anim_switch("push")
		
elif movdirecao != Vector2(0,0):
	anim_switch("walk") 
else:
	anim_switch("idie") 

func controls_loop():
var ESQUERDA = Input.is_action_pressed(“ui_left”)
var DIREITA = Input.is_action_pressed(“ui_right”)
var CIMA = Input.is_action_pressed(“ui_up”)
var BAIXO = Input.is_action_pressed(“ui_down”)

movdirecao.x = -int(ESQUERDA) + int(DIREITA)
movdirecao.y = -int(CIMA) + int(BAIXO)

func movement_loop():
var mover = movdirecao.normalized() * SPEED

move_and_slide(mover, Vector2(0,0))

func spritedir_loop():
match movdirecao:
Vector2(-1,0):
spritedir = “left”
Vector2(1,0):
spritedir = “right”
Vector2(0,-1):
spritedir = “up”
Vector2(0,1):
spritedir = “down”

func anim_switch(animation):
var newanim = str(animation.spritedir)
if $anim.current_animation != newanim:
$anim.play(newanim)

Use the { } button to format your code, this helps everyone help you:

extends KinematicBody2D

const SPEED = 70

var movdirecao = Vector2(0,0)
var spritedir = "down"
var motion

func physics_process(delta):
    controls_loop()
    movement_loop()
    spritedir_loop()

    if is_on_wall():
        if spritedir == "left" and test_move(transform, Vector2(-1,0)):
            anim_switch("push")
        if spritedir == "right" and test_move(transform, Vector2(1,0)):
            anim_switch("push")
        if spritedir == "up" and test_move(transform, Vector2(0,-1)):
            anim_switch("push")
        if spritedir == "down" and test_move(transform, Vector2(0,1)):
            anim_switch("push")

    elif movdirecao != Vector2(0,0):
        anim_switch("walk") 
    else:
        anim_switch("idie") 

func controls_loop():
    var ESQUERDA = Input.is_action_pressed("uileft")
    var DIREITA = Input.is_action_pressed("uiright")
    var CIMA = Input.is_action_pressed("uiup")
    var BAIXO = Input.is_action_pressed("ui_down")

    movdirecao.x = -int(ESQUERDA) + int(DIREITA)
    movdirecao.y = -int(CIMA) + int(BAIXO)

func movement_loop():
    var mover = movdirecao.normalized() * SPEED
    move_and_slide(mover, Vector2(0,0))

func spritedir_loop():
    match movdirecao:
        Vector2(-1,0):
            spritedir = "left"
        Vector2(1,0):
            spritedir = "right"
        Vector2(0,-1):
            spritedir = "up"
        Vector2(0,1):
            spritedir = "down"

func anim_switch(animation):
    var newanim = str(animation.spritedir)
    if $anim.current_animation != newanim:
       $anim.play(newanim)

Dlean Jeans | 2019-05-25 05:54

:bust_in_silhouette: Reply From: Dlean Jeans

SImply change your anim_switch function to this:

func anim_switch(animation):
    if $anim.current_animation != animation:
       $anim.play(newanim)

without:

    var newanim = str(animation.spritedir)

You’re calling anim_switch("walk") so animation in anim_switch is already a String which has no spritedir property.