Topic was automatically imported from the old Question2Answer platform.
Asked By
zebulon
Old Version
Published before Godot 3 was released.
Hello, I work on 2d map random generator, for that I store information in directory like: dico[Vector2( x_pos , y_pos )] = number_of_my_tile
I want to save my dictonary in file : map_01.map
I use the function:
func save_map(name):
transfert_to_dico()
var savegame = File.new()
savegame.open("res://Save/"+str(name)+".map", File.WRITE)
var enr=global.dico_3D
savegame.store_line(enr.to_json())
savegame.close()
it’s work.
But how can I reload the file map_01.map into dictonary ?
When I edit the file in text editor I can see: {"(127, 127)":8, "(127, 126)":18, "(127, 125)":19, "(127, 124)":19, "(127, 123)":19,... ...}
You just need to open the file, read the line and parse the json. Something like this:
func load_map(name):
var savegame = File.new()
savegame.open("res://Save/"+str(name)+".map", File.READ)
var game_data = {}
game_data.parse_json(savegame.get_line())
savegame.close()
return game_data
You should replace res:// with user:// though, because you can’t write at res:// on the exported game.
Thank’s I’ll try two.
zebulon | 2016-09-05 18:02
Erf, don’t work…
Save fonction:
func save_map(name):
transfert_to_dico()
var savegame = File.new()
savegame.open("res://Save/"+str(name)+".map", File.WRITE)
var enr=global.dico_3D
savegame.store_line(enr.to_json())
savegame.close()
result like my upper post…
Load fonction : (I have testing your two solutions)
func load_map(name):
var currentLine = {}
var loadgame = File.new()
if loadgame.file_exists("res://Save/"+str(name)+".map"):
loadgame.open("res://Save/"+str(name)+".map", File.READ)
var info = loadgame.get_as_text()
var dico_map = {}
dico_map.parse_json(loadgame.get_line())
loadgame.close()
return dico_map
My dico_map stay empty…
Besides that, I don’t really understand the logic …
And how “dico_map.parse_json (loadgame.get_line ())” can rebuild an entire dictionary indexed by vectors like :
dico[(Vector2(x,y)]=my_int_variable…
I’m newbie, but in my head the text in the file my_save.map
look like >>> {“(127, 127)”:8, “(127, 126)”:18, “(127, 125)”:19, “(127, 124)”:19, “(127, 123)”:19,… …}
must be cut for obtain something like: info=[127,127,8] (for each entry)
127= posx
127= posy
8= number of my tile
and after that reconstruct by something like dico[Vector2(info[0],info[1])] = info[2] no ?
if you need to visualize, you can see the map (tilemap)
here : FB link
var size_x = 10
var size_y = 10
var tiles = [0,1,2,3,4,5,6,7,8,9]
var dico_save = {}
var dico_load = {}
func create_dico():
for x in range (size_x):
for y in range (size_y):
dico_save[Vector2(x,y)] = tiles[randi()%10]
func trace_dico_save():
var test = 0
for i in dico_save:
test=dico_save[i]
get_node("save").set_cell(i.x,i.y,test)
func trace_dico_load():
var test = 0
for i in dico_load:
test=dico_load[i]
get_node("load").set_cell(i.x,i.y,test)
func save():
var info = {}
var temp = 0
for i in dico_save:
var test = dico_save[i]
info[temp] = [i.x,i.y,test]
temp +=1
var savegame = File.new()
savegame.open("res://Save_test.map", File.WRITE)
savegame.store_line( info.to_json() )
savegame.close()
func loading():
var savegame = File.new()
var infoload = {}
savegame.open("res://Save_test.map", File.READ)
while (!savegame.eof_reached()):
infoload.parse_json( savegame.get_line() )
savegame.close()
for i in infoload:
var test = infoload[i]
dico_load[Vector2(test[0],test[1])] = test[2]
func _ready():
create_dico()
trace_dico_save()
save()
loading()
trace_dico_load()