0 votes

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,... ...}

Thank you !

in Engine by (16 points)
edited by

edited codes looks better

3 Answers

+1 vote

You can load json to dictionary like that

var f = File.new()
f.open("file path", f.READ)
var j = f.get_as_text()
f.close()   
var result = {}
result.parse_json(j)

You cannot save files to res://, you should use user:// instead. http://docs.godotengine.org/en/latest/tutorials/step_by_step/filesystem.html?highlight=user#resource-path

BTW. Its Dictionary not directory. You should edit the question because its misleading now :)

by (368 points)
edited by

Thank's I'll try

+3 votes

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.

by (797 points)
edited by

Thank's I'll try two.

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 "dicomap.parsejson (loadgame.get_line ())" can rebuild an entire dictionary indexed by vectors like :

dico[(Vector2(x,y)]=myintvariable...
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],info1)] = info[2] no ?

if you need to visualize, you can see the map (tilemap)
here : FB link

0 votes

my solution:

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()
by (16 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 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 webmaster@godotengine.org with your username.