Cannot convert argument 1 from int to Vector2.

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

I have completed an godot tutorial and have successfully produced a simple game demo.
However, when I am trying to add in my own design – a healing action that stops the player from moving and regain health after a duration of time. The engine says
“Invalid type in function ‘move_and_slide’ in base ‘KinematicBody2D (operation.gd)’. Cannot convert argument 1 from int to Vector2.”
I don’t really understand why it suddently crashed because I used the same structure as how other actions are designed.
the code is a bit long so I hope noting will help understanding it:

extends KinematicBody2D
const acceleration = 400
const maxspeed = 60
const friction = 500
const roll_speed = 120
const player_hurt_sound = preload("res://Action RPG Resources/Music and Sounds/special_sound_effect.tscn")
#set up basic variables that makve moving more realistic
#to change initial health check Player_stats

enum{
	move
	roll
	attack
	heal
}
#setup actions

var state = move
var velocity = Vector2.ZERO
var roll_vector = Vector2.DOWN
var stats = PlayerStats
var time_end = false

onready var animationPlayer = $AnimationPlayer
onready var animationtree = $AnimationTree
onready var animationstate = animationtree.get("parameters/playback")
onready var SwordHitbox = $hitboxpivot/sword_hitbox
onready var hurtbox = $hurtbox
onready var blinkAnimation = $blink
onready var timer = $Timer
func _ready():
	stats.connect("no_health",self, "queue_free")
	animationtree.active = true
	SwordHitbox.knockback_vector = roll_vector
	randomize()
#set up animation

func _physics_process(delta):
	match state:
		move:
			move_state(delta)
		roll:
			roll_state(delta) 
		attack:
			attack_state(delta)
		heal:
			heal_state(delta)
	#set how character move

func move_state(delta):
	var inputVector = Vector2.ZERO
	inputVector.x = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
	inputVector.y = Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up")
	inputVector = inputVector.normalized()
	#set up how input affect character
	
	if inputVector != Vector2.ZERO:
		roll_vector = inputVector
		SwordHitbox.knockback_vector = inputVector
		animationtree.set("parameters/idle/blend_position", inputVector)
		animationtree.set("parameters/move/blend_position", inputVector)
		animationtree.set("parameters/attack/blend_position", inputVector)
		animationtree.set("parameters/roll/blend_position", inputVector)
		animationstate.travel("move")
		velocity = velocity.move_toward(inputVector*maxspeed,acceleration*delta)
	else:
		animationstate.travel("idle")
		velocity = velocity.move_toward(Vector2.ZERO, friction*delta)
	#set how character move
	move()
	if Input.is_action_just_pressed("roll"):
		state = roll
		print("roll_Activated")
	#for debug
	if Input.is_action_just_pressed("attack"):
		state = attack
		print("attack_Activated")
	#for debug
	if Input.is_action_just_pressed("heal"):
		state = heal
		print("healing")
func roll_state(delta):
	velocity = roll_vector*roll_speed
	animationstate.travel("roll")
	move()
	print("rolling")
func heal_state(duration):
	velocity = 0
	timer.start(duration)
	print("heal_state")
#start healing time
	if(time_end):
		PlayerStats.health +=1
		move()
#move when healing is complete
func move():
	velocity = move_and_slide(velocity)
func attack_state(delta):
	velocity = Vector2.ZERO
	animationstate.travel("attack")
	
func attact_animation_finished():
	velocity = Vector2.ZERO
	state = move
	
func roll_animation_finished():
	state = move
	
func _on_hurtbox_area_entered(area):
	stats.health -= area.damage
	print("I'm hit")
	hurtbox.start_invincibility(1)
	hurtbox.create_hit_effect()
	var playerHurtSound = player_hurt_sound.instance()
	get_tree().current_scene.add_child(playerHurtSound)


func _on_hurtbox_invincibility_started():
	blinkAnimation.play("start")
func _on_hurtbox_invincibility_ended():
	blinkAnimation.play("stop")


func _on_Timer_timeout():
	time_end = true
	# tell heal state that healing is done
:bust_in_silhouette: Reply From: GlitchedCode

in your heal_state function, you are setting velocity to a 0 changing the type of data it is holding from a Vector2 to an int.

func heal_state(duration):
    velocity = 0

instead you should be setting it to Vector2.ZERO

This oversight of the error is why we should be type casting our variables. If the engine knew that velocity had to be a Vector2, it would have thrown an error on this line when you tried assigning it to 0.

Two ways we can do this are
The walrus operator :=

var velocity := Vector2.ZERO

This walrus will infer its type based on the initial value given to it, Vector2.ZERO

The second way to write it would be to tell it the type instead of inferring it

var velocity: Vector2 = Vector2.ZERO

fixed! Thank you!

D.Q.Li | 2023-03-29 12:52