0 votes

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

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 physicsprocess(delta):
#left-right movement
var movementx = Input.getactionstrength("uiright") - Input.getactionstrength("uileft")
if movement
x != 0:
vel.x += movementx * SPEED * delta
vel.x = clamp(vel.x, -MAXSPEED, MAXSPEED)
if Input.is
actionpressed("uiright") and isonfloor():
anim.play("Run")
if Input.isactionpressed("uileft") and isonfloor():
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 onSpringbodyentered(_body):
vel.y = spring
anim.play("Jump")

func onKillBoxbodyentered(body):
Signals.emit
signal("playerDeath")
return

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

func process(delta):
if Input.isactionjustpressed("shoot"):
var Bullet = plbullet.instance()
Bullet.global
position = firePosition.globalposition
get
tree().currentscene.addchild(Bullet)

func setHealth(value):
var prevHealth = health
health = clamp(value, 0, MAXHEALTH)
if health != prevHealth:
emitsignal("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 onInvFrames_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()
newfile.open(filepath, File.WRITE)
newfile.storeline(data)
new_file.close()
print("saved")

func load():
var new
file = File.new()
if not newfile.fileexists(filepath):
_save()
return
new
file.open(filepath, File.READ)
var data = new
file.getastext()
newfile.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])

Godot version 3.4
in Engine by (56 points)

1 Answer

+1 vote

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
by (3,882 points)
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.