Does saving work on web based games?

Godot Version

4.4

Question

I’m working on adding saving and loading to my web based game. It works okay in the editor but not when exported to itch.io. I’m wondering if it is that it’s impossible to save web based games or not.

extends Node
var save_data = "user://variable.save"
var current_checkpoint : Checkpoint
var current_checkpoint_finale : finaleCheckpoint
var player :Player
var isrival = true
var angerlevel=0
var rivalconversation = 1
var isdead=false
var something = false
var rival2dialougeend = false
var finale = false
var deathcounter = 0
var diddialogue = false
var stoptime = true
var happybirthday = false
var px 
var py 
var gravflip=false
var debug=false
var playerpos: Vector2
var level = 1
var cansave=false
var isdashunlocked = false
var filewiped = true
var rII = false
var wipingsave = false
var needtoquit =false
func _ready() -> void:
	print(wipingsave)
	specificload()
	if wipingsave==false:
		loaddata()
	else:
		wipesave()
	if filewiped==true:
		isrival = true
	else:
		isrival = false
	
func respawn_player():

	if finale==false:
		if current_checkpoint != null:
			player.position = current_checkpoint.global_position
	if finale==true:
		player.position = current_checkpoint_finale.global_position
func _process(delta: float) -> void:
	if Input.is_action_just_pressed("jkey"):
		wipesave()
	if Input.is_action_just_pressed("quit"):
		get_tree().change_scene_to_file("res://scenes/titlescreen.tscn")
	await get_tree().create_timer(3).timeout
	if cansave:
		save()
	if needtoquit:
		await get_tree().create_timer(1).timeout
		get_tree().quit()
func save():
	var file = FileAccess.open(save_data, FileAccess.WRITE)
	if isrival ==true:
		filewiped = false
	file.store_var(level)
	file.store_var(rivalconversation)
	file.store_var(finale)
	file.store_var(deathcounter)
	file.store_var(rival2dialougeend)
	file.store_var(isdashunlocked)
	file.store_var(filewiped)
	file.store_var(rII)
	file.store_var(wipingsave)
func specificload():
	if FileAccess.file_exists(save_data):
		var file = FileAccess.open(save_data, FileAccess.READ)
		for i in range(8):
			file.get_var(wipingsave)
		wipingsave = file.get_var(wipingsave)
func loaddata():
	if FileAccess.file_exists(save_data):
		var file = FileAccess.open(save_data, FileAccess.READ)
		level = file.get_var(level)
		rivalconversation = file.get_var(rivalconversation)
		finale = file.get_var(finale)
		deathcounter = file.get_var(deathcounter)
		rival2dialougeend = file.get_var(rival2dialougeend)
		isdashunlocked = file.get_var(isdashunlocked)
		filewiped = file.get_var(filewiped)
		rII = file.get_var(rII)
		cansave = true
func wipesave():
	level = 1
	rivalconversation = 1
	finale = false
	deathcounter = 0
	rival2dialougeend = false
	filewiped = true
	rII = false
	isdashunlocked = false
	wipingsave = false
	var file = FileAccess.open(save_data, FileAccess.WRITE)
	file.store_var(level)
	file.store_var(rivalconversation)
	file.store_var(finale)
	file.store_var(deathcounter)
	file.store_var(rival2dialougeend)
	file.store_var(isdashunlocked)
	file.store_var(filewiped)
	file.store_var(rII)
	file.store_var(wipingsave)
func fullsavewipe():
	wipingsave = true
	save()
	get_tree().quit()

this is the code for the saving.

Edit:
I know the problem isn’t the saving code because it works perfectly fine when exporting to a windows build.

The web client uses the browser’s IndexedDB system to create a virtual filesystem that user:// points to. But yeah the web export should be able to save. Needs cookies permission though. Relevant docs here.

(if you’re running any privacy extensions, they might be interfering. I get tripped up by that all the time)

If it should work then I don’t know why it breaks. I’ve tried it on my personal account which has an ad blocker and a new chrome profile which has nothing and the saving doesn’t work in the web based version.

Hmm, tricky!

You don’t really need to check file_exists here, open will just return null if anything went wrong and then you can use FileAccess.get_open_error to see what happened. So you can try:

func save():
	var file = FileAccess.open(path, FileAccess.WRITE)
	if not file:
		push_error("Couldn't write %s: %s" % [path, error_string(FileAccess.get_open_error())])
		return
	file.store_var(level)
	file.store_var(rivalconversation)
	# etc

func loaddata():
	var file = FileAccess.open(path, FileAccess.READ)
	if not file:
		push_error("Couldn't read %s: %s" % [path, error_string(FileAccess.get_open_error())])
		return
	level = file.get_var()
	rivalconversation = file.get_var()
	# etc

That’ll help you see what’s going wrong at least. Make sure you export in debug mode so that the error logs end up in the console.

1 Like

removing that check worked and now the web version can save but it did sprout a multitude of problems I have to fix. Sorry that I wasn’t able to respond quickly. I had some stuff going on.

1 Like