Problem Using Loaded Int from Save

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

Hello, please keep in mind that I am a beginner programmer with almost no prior knowledge. thanks :slight_smile:

Using my save system I am trying to edit the player’s jump value. I am saving the int correctly because it is coming out correctly in the Output, but the int is not affecting the player’s jump. I would appreciate any help that can be directed my way. Here is the code:

Player.gd:

extends KinematicBody2D

var SPEED = 500
var MAXSPEED = 150
var FRICTION = .5
var resistance = 0.7
var grav = 500
var jump = Save.jump
var vel = Vector2.ZERO
var spring = -400
var jumpNumber = 2
var MAXFALLSPEED = 500
var is_grounded

signal killed()
signal healthUpdated(health)

var MAXHEALTH = GlobalVars.MAXHEALTH

onready var InvTimer = $InvFrames
onready var health = MAXHEALTH setget setHealth
onready var sprite = $Sprite
onready var animEffects = $Effects
onready var anim = $moveAnimations

func _ready():
health = MAXHEALTH
emit_signal(“healthUpdated”, health)

func _physics_process(delta):
#left-right movement
var movement_x = Input.get_action_strength(“ui_right”) - Input.get_action_strength(“ui_left”)
if movement_x != 0:
vel.x += movement_x * SPEED * delta
vel.x = clamp(vel.x, -MAXSPEED, MAXSPEED)
if Input.is_action_pressed(“ui_right”) and is_on_floor():
anim.play(“Run”)
if Input.is_action_pressed(“ui_left”) and is_on_floor():
anim.play(“Run”)
sprite.flip_h = movement_x > 0

#for bullet rotation direction
if sprite.flip_h == true:
	Signals.l = true
if sprite.flip_h == false:
	Signals.l = false


if is_on_floor():
	Signals.onground = true
	if movement_x == 0:
		vel.x = lerp(vel.x, 0, FRICTION)
		anim.play("Idle")
	if Input.is_action_just_pressed("jump"):
		vel.y -= jump

if !is_on_floor():
	Signals.onground = false
	if vel.y < 0:
		anim.play("Jump")
	if vel.y > 0:
		anim.play("Fall")


vel.y += grav * delta
if vel.y > MAXFALLSPEED:
	vel.y = MAXFALLSPEED
vel = move_and_slide(vel, Vector2.UP)

func _on_Spring_body_entered(_body):
vel.y = spring
anim.play(“Jump”)

func _on_KillBox_body_entered(_body):
Signals.emit_signal(“playerDeath”)
return

#shooting
onready var plbullet = preload(“res://BulletPlayer.tscn”)
onready var firePosition = $firePosition

func _process(_delta):
if Input.is_action_just_pressed(“shoot”):
var Bullet = plbullet.instance()
Bullet.global_position = firePosition.global_position
get_tree().current_scene.add_child(Bullet)

func setHealth(value):
var prevHealth = health
health = clamp(value, 0, MAXHEALTH)
if health != prevHealth:
emit_signal(“healthUpdated”, health)
if health == 0:
kill()
Signals.emit_signal(“playerDeath”)

func kill():
print(“ubad”)

func damage(amount):
if InvTimer.is_stopped():
InvTimer.start()
setHealth(health - amount)
animEffects.play(“Damage”)
animEffects.queue(“Inv”)

func _on_InvFrames_timeout():
animEffects.play(“Rest”)

And the Save.gd (which is a autoload):
extends Node

var jump = 350

onready var player = preload(“res://Player/Player.tscn”).instance()
#for when I used player vars directly
var file_path = “user://save.txt”

func _ready():
_load()
print(jump)

func _save():
var data = “”
data += "maxheath " + str(GlobalVars.MAXHEALTH)
data += “\n”
data += "jump " + str(jump)
var new_file = File.new()
new_file.open(file_path, File.WRITE)
new_file.store_line(data)
new_file.close()
print(“saved”)

func _load():
var new_file = File.new()
if not new_file.file_exists(file_path):
_save()
return
new_file.open(file_path, File.READ)
var data = new_file.get_as_text()
new_file.close()
data = data.split(“\n”)
for line in data:
if line.begins_with("maxhealth “):
GlobalVars.MAXHEALTH = int(line.split(” ")[1])
elif line.begins_with("jump “):
jump = int(line.split(” ")[1])

:bust_in_silhouette: Reply From: timothybrentwood

The problem is you are instancing the player immediately then loading in the data from the file. So the player is reading the Save.jump variable when it is set to the default value of 350 not after it loads the file. Adding this line ill fix it:

func _ready():
    _load()
    print(jump)
    player.jump = jump