Invalid set index 'x' (on base: 'Vector2') with value of type 'String'.

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

Here is the code(Not all the code, just the relevant parts):

func _ready():
	savex(position.x)
	savey(position.y)
	
func savex(content):
	var file = File.new()
	file.open("user://pos_x.dat", file.WRITE)
	file.store_line(str(content))
	file.close()
func savey(content):
	var file = File.new()
	file.open("user://pos_y.dat", file.WRITE)
	file.store_line(str(content))
	file.close()

func loadx():
	var file = File.new()
	file.open("user://pos_x.dat", file.READ)
	var content = file.get_line()
	file.close()
	print(content)
	position.x = content
func loady():
	var file = File.new()
	file.open("user://pos_y.dat", file.READ)
	var content = file.get_line()
	file.close()
	print(content)
	position.y = content
func _on_Area2D_body_entered(body):
	# print("COLL")
	print("Whoa, ur so cool u won.")
	loadx()
	loady()

Full code:

extends KinematicBody2D


func _ready():
	savex(position.x)
	savey(position.y)
	
func savex(content):
	var file = File.new()
	file.open("user://pos_x.dat", file.WRITE)
	file.store_line(str(content))
	file.close()
func savey(content):
	var file = File.new()
	file.open("user://pos_y.dat", file.WRITE)
	file.store_line(str(content))
	file.close()

func loadx():
	var file = File.new()
	file.open("user://pos_x.dat", file.READ)
	var content = file.get_line()
	file.close()
	print(content)
	position.x = content
func loady():
	var file = File.new()
	file.open("user://pos_y.dat", file.READ)
	var content = file.get_line()
	file.close()
	print(content)
	position.y = content


export var jump_impulse = -20
const GRAVITY = 10700
export (int) var speed = 200
var velocity = Vector2()
var jumping = false
func get_input():
	velocity = Vector2()
	if Input.is_action_pressed("ui_right"):
		velocity.x += 1
	if Input.is_action_pressed("ui_left"):
		velocity.x -= 1
	if Input.is_action_pressed("ui_accept"):
		velocity.y -= 1
	velocity = velocity.normalized() * speed

func _physics_process(delta):
	get_input()
	velocity = move_and_slide(velocity)
	if Input.is_action_pressed("ui_right"):
		$AnimatedSprite.animation = "right"
		$AnimatedSprite.play()
		velocity.x += speed
	elif Input.is_action_pressed("ui_accept"):
		$AnimatedSprite.animation = "jump"
		$AnimatedSprite.play()
		jumping = true
		velocity.y = jump_impulse
	elif Input.is_action_pressed("ui_left"):
		$AnimatedSprite.animation = "left"
		$AnimatedSprite.play()
		velocity.x -= speed
	else:
		$AnimatedSprite.animation = "idle"
		$AnimatedSprite.play()
	get_input()
	velocity.y += GRAVITY * delta
	if jumping and is_on_floor():
		jumping = false
	velocity = move_and_slide(velocity, Vector2(0, -1))


func _on_Area2D_body_entered(body):
	# print("COLL")
	print("Whoa, ur so cool u won.")
	loadx()
	loady()
:bust_in_silhouette: Reply From: jgodfrey

I assume the problem is here:

position.x = content

That’s because you loaded the value into content via file.get_line(), which is documented to return a string.

And, since the x component of a Vector2 is of type float, you can’t set it to a string value.

Assuming the string value you’ve loaded into content can be converted to a float, one fix would be:

position.x = float(content)

Thank you so much! :slight_smile: That was dumb of me.

RockZombie4 | 2022-04-26 00:41

Hey guys,

I’m having the same error with Godot 4 and I’m scratching my head.

func _physics_process(_delta):
	motion.x = Input.get_action_strength("right") - Input.get_action_strength("left")
    

Where motion is a Vector2(). I double-check the input names and they are well written.

What am I missing?

el_pablo | 2023-04-25 13:03

If you do have the same error, that implies that motion is NOT a Vector2 - at least at the time the error is generated. With that in mind, I don’t think the problem is in the above code It’s likely somewhere else, where motion is (inadvertently) assigned a String value.

jgodfrey | 2023-04-25 13:22