Text File Issues When Exporting

Godot Version

4.2.2

Question

I’m making this game where I store the user’s information in a text file. It works just fine in godot but when I export it, the text file doesn’t work (I’m not sure if it is having issues with reading the file or writing to it).

Code

extends Node2D

var on_loading_screen = true
var timer_set_counter = 0
var timer_has_finished = false
var previous_word = “”
var on_level_1 = false
var on_level_2 = false
var on_level_3 = false
var score_file = “res://scene/world/player_score.txt”
var get_user_score = “”
var score_plus_file_score = 0
var user_score = 0

var essere_verbs = {
“io sono”: “I am”,
“tu sei”: “You are (singular)”,
“lei e”: “She is”,
“lui e”: “He is”,
“noi siamo”: “We are”,
“voi siete”: “You are (plural)”,
“loro sono”: “They are”
}

var avere_verbs = {
“io ho”: “I have”,
“tu hai”: “You have (singular)”,
“lei ha”: “She has”,
“lui ha”: “He has”,
“noi abbiamo”: “We have”,
“voi avete”: “You have (plural)”,
“loro hanno”: “They have”
}

var avere_and_essere_verbs = {
“io sono”: “I am”,
“tu sei”: “You are (singular)”,
“lei e”: “She is”,
“lui e”: “He is”,
“noi siamo”: “We are”,
“voi siete”: “You are (plural)”,
“loro sono”: “They are”,
“io ho”: “I have”,
“tu hai”: “You have (singular)”,
“lei ha”: “She has”,
“lui ha”: “He has”,
“noi abbiamo”: “We have”,
“voi avete”: “You have (plural)”,
“loro hanno”: “They have”
}

var current_phrase: String = “”
var current_translation: String = “”

Called when the node enters the scene tree for the first time.

func _ready():
read_user_score()
make_a_timer()

Called when the Timer reaches 0 (timeout)

func _on_timer_timeout():
timer_has_finished = true
timer_set_counter += 1

func make_a_timer():
# Create a Timer node and set its properties
var timer = Timer.new()
timer.wait_time = 3.0 # Wait for 3 seconds
timer.one_shot = true # Run only once

# Connect the "timeout" signal to the _on_timer_timeout method
timer.connect("timeout", Callable(self, "_on_timer_timeout"))

# Add the Timer node as a child
add_child(timer)

# Start the timer
timer.start()

func display_random_phrase_level_1():
while current_phrase == previous_word:
current_phrase = essere_verbs.keys()[randi() % essere_verbs.size()]
current_translation = essere_verbs[current_phrase]
$“Levels/Level 1/Question”.text = current_phrase
previous_word = current_phrase # Update the previous phrase

func display_random_phrase_level_2():
while current_phrase == previous_word:
current_phrase = avere_verbs.keys()[randi() % avere_verbs.size()]
current_translation = avere_verbs[current_phrase]
$“Levels/Level 2/Question”.text = current_phrase
previous_word = current_phrase # Update the previous phrase

func display_random_phrase_level_3():
while current_phrase == previous_word:
current_phrase = avere_and_essere_verbs.keys()[randi() % avere_and_essere_verbs.size()]
current_translation = avere_and_essere_verbs[current_phrase]
$“Levels/Level 3/Question”.text = current_phrase
previous_word = current_phrase # Update the previous phrase

Show Level 1

func level_1():
$“Map and Loading Screen/Map”.hide()
$“Levels/Level 1”.show()
$“Levels/Level 2”.hide()
display_random_phrase_level_1() # Display the first phrase

func level_2():
$“Map and Loading Screen/Map”.hide()
$“Levels/Level 2”.show()
$“Levels/Level 1”.hide()
display_random_phrase_level_2() # Display the first phrase

func level_3():
$“Map and Loading Screen/Map”.hide()
$“Levels/Level 3”.show()
$“Levels/Level 2”.hide()
$“Levels/Level 1”.hide()
display_random_phrase_level_3() # Display the first phrase

func _physics_process(delta):
if on_loading_screen:
$“Levels/Level 1”.hide()
$“Levels/Level 2”.hide()
$“Levels/Level 3”.hide()
$“Back Button”.hide()
$XP.hide()

else:
	$XP.show()
	$XP/XP.text = str(user_score)

if timer_has_finished == true:
	if timer_set_counter == 1:
		# Hide the loading screen
		$"Map and Loading Screen/LoadingScreen".hide()
		on_loading_screen = false
		$"Map and Loading Screen/Map".show()
		timer_has_finished = false
	if on_level_1 == true:
		# Hide the loading screen
		$"Map and Loading Screen/LoadingScreen".hide()
		on_loading_screen = false
		level_1()
		timer_has_finished = false
	if on_level_2 == true:
		# Hide the loading screen
		$"Map and Loading Screen/LoadingScreen".hide()
		on_loading_screen = false
		level_2()
		timer_has_finished = false
	if on_level_3 == true:
		# Hide the loading screen
		$"Map and Loading Screen/LoadingScreen".hide()
		on_loading_screen = false
		level_3()
		timer_has_finished = false

func _on_user_input_1_text_submitted(text: String):
var cleaned_text = text.strip_edges().to_lower() # Convert to lowercase
var cleaned_translation = current_translation.to_lower() # Convert to lowercase
if cleaned_text == cleaned_translation:
# Correct translation
$“Levels/Level 1/User_input1”.text = “”
$“Levels/Level 1/User_input1”.placeholder_text = “Correct”
var previous_word = current_translation
display_random_phrase_level_1()
user_score += 1
update_user_score_file()
else:
# Incorrect translation
var user_input = $“Levels/Level 1/User_input1”.text
var question_text = $“Levels/Level 1/Question”.text
var placeholder_text = str(“Incorrect, you wrote "”, user_input, “". The correct definition of "”, question_text, “" is "”, current_translation, “".”)
$“Levels/Level 1/User_input1”.placeholder_text = placeholder_text
$“Levels/Level 1/User_input1”.text = “”
var previous_word = current_translation
display_random_phrase_level_1()

func _on_user_input_2_text_submitted(text: String):
var cleaned_text = text.strip_edges().to_lower() # Convert to lowercase
var cleaned_translation = current_translation.to_lower() # Convert to lowercase
if cleaned_text == cleaned_translation:
# Correct translation
$“Levels/Level 2/User_input2”.text = “”
$“Levels/Level 2/User_input2”.placeholder_text = “Correct”
var previous_word = current_translation
display_random_phrase_level_2()
user_score += 2
update_user_score_file()
else:
# Incorrect translation
var user_input = $“Levels/Level 2/User_input2”.text
var question_text = $“Levels/Level 2/Question”.text
var placeholder_text = str(“Incorrect, you wrote "”, user_input, “". The correct definition of "”, question_text, “" is "”, current_translation, “".”)
$“Levels/Level 2/User_input2”.placeholder_text = placeholder_text
$“Levels/Level 2/User_input2”.text = “”
var previous_word = current_translation
display_random_phrase_level_2()

func _on_user_input_3_text_submitted(text: String):
var cleaned_text = text.strip_edges().to_lower() # Convert to lowercase
var cleaned_translation = current_translation.to_lower() # Convert to lowercase
if cleaned_text == cleaned_translation:
# Correct translation
$“Levels/Level 3/User_input3”.text = “”
$“Levels/Level 3/User_input3”.placeholder_text = “Correct”
var previous_word = current_translation
display_random_phrase_level_3()
user_score += 3
update_user_score_file()
else:
# Incorrect translation
var user_input = $“Levels/Level 3/User_input3”.text
var question_text = $“Levels/Level 3/Question”.text
var placeholder_text = str(“Incorrect, you wrote "”, user_input, “". The correct definition of "”, question_text, “" is "”, current_translation, “".”)
$“Levels/Level 3/User_input3”.placeholder_text = placeholder_text
$“Levels/Level 3/User_input3”.text = “”
var previous_word = current_translation
display_random_phrase_level_3()

func _on_level_1_pressed():
OS.delay_msec(500)
timer_has_finished = true
on_level_1 = true
on_level_2 = false
on_level_3 = false
$“Back Button”.show()

func _on_level_2_pressed():
OS.delay_msec(500)
timer_has_finished = true
on_level_1 = false
on_level_2 = true
on_level_3 = false
$“Back Button”.show()

func _on_level_3_pressed():
OS.delay_msec(500)
timer_has_finished = true
on_level_1 = false
on_level_2 = false
on_level_3 = true
$“Back Button”.show()

func _on_back_pressed():
$“Levels/Level 1”.hide()
$“Levels/Level 2”.hide()
$“Levels/Level 3”.hide()
$“Map and Loading Screen/Map”.show()
$“Back Button”.hide()

func update_user_score_file():
score_file = FileAccess.open(“res://scene/world/player_score.txt”, FileAccess.WRITE)
get_user_score = score_file.get_as_text()
score_plus_file_score = (int(user_score) + int(get_user_score))
score_file.store_string(str(score_plus_file_score))
score_file.close()

func read_user_score():
if FileAccess.file_exists(score_file):
var file = FileAccess.open(score_file, FileAccess.READ)
user_score = int(file.get_as_text())
file.close()
else:
var file = FileAccess.open(score_file, FileAccess.WRITE)
if file != null:
score_plus_file_score = (int(user_score) + int(get_user_score))
file.store_string(str(score_plus_file_score)) # Initialize the score to 0
file.close()

func _exit_tree():
update_user_score_file()

you can paste code with the </> button on a new line like so

```
type or paste code here
```

You have to add text files to exported resources.

That still doesn’t work. Sorry I don’t really know how to paste the code. I’m pretty new to godot.

You cannot write to “res://” after exporting, you probably want to store it in “user://player_score.txt”

I have modified the code but where do I put the file?

You will have to determine if it exists and create it at runtime. It will not be packed.

Here is where “user://” ends up for each platform.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.