0 votes

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()
Godot version v3.4.2
in Engine by (24 points)

1 Answer

0 votes
Best answer

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)

by (16,810 points)
selected by

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

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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.