godotengine / godotengine/godot-docs

Better example of load() and save() for beginners on File Class

Open
#2,295 3 comments 9 reactions 0 assignees View on GitHub
area:class reference
Dominant language
reStructuredText
Stars
5.7k
Forks
3.8k
Avg merge
1d 20h
Merged PRs (30d)
25

Description

Hello, I speak Spanish but I have used the translator to write this, so I apologize for any inconvenience with the reading.

Since I started creating games with Github I had problems to understand how to load and save games, it seems to me that the example that appears in the File class may be better for beginners.

So far the example code that appears is this:

```
func save(content):
var file = File.new()
file.open("user://save_game.dat", File.WRITE)
file.store_string(content)
file.close()

func load():
var file = File.new()
file.open("user://save_game.dat", File.READ)
var content = file.get_as_text()
file.close()
return content
```
The example is located in: https://docs.godotengine.org/en/3.1/classes/class_file.html#description

In my case, it was confusing since `load` is a reserved word and the application of these functions was not clear to me.

That is why I would like to propose a simpler example to understand and apply for future beginners in Godot:

```
var data = {"my_data" : "value"}

func save_game():
var file = File.new()
file.open("user://save_game.dat", File.WRITE)
file.store_var(data)
file.close()

func load_game():
var file = File.new()
if not file.file_exists("user://save_game.dat"):
return
file.open("user://save_game.dat", File.READ)
data = file.get_var()
file.close()
```

-----

Hola, hablo español pero he usado el traductor para escribir esto, por lo que pido disculpas por cualquier inconveniente con la lectura.

Desde que comencé a crear juegos con Github tuve problemas para comprender como cargar y guardar partidas, me parece que el ejemplo que aparece en la clase File puede ser mejor para los principiantes.

Hasta ahora el código de ejemplo que aparece es este:
```
func save(content):
var file = File.new()
file.open("user://save_game.dat", File.WRITE)
file.store_string(content)
file.close()

func load():
var file = File.new()
file.open("user://save_game.dat", File.READ)
var content = file.get_as_text()
file.close()
return content
```
El ejemplo se ubica en: https://docs.godotengine.org/en/3.1/classes/class_file.html#description

En mi caso, fue confuso ya que `load` es una palabra reservada y la aplicación de estas funciones no me quedaba claro.

Por eso me gustaría proponer un ejemplo mas sencillo de entender y aplicar para los futuros principiantes en Godot:

```
var data = {"my_data" : "value"}

func save_game():
var file = File.new()
file.open("user://save_game.dat", File.WRITE)
file.store_var(data)
file.close()

func load_game():
var file = File.new()
if not file.file_exists("user://save_game.dat"):
return
file.open("user://save_game.dat", File.READ)
data = file.get_var()
file.close()
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.