I have a building system in place and I want the player to be able to save. I don't know how to do this, and the only answers the internet gave me are for Godot 3
Thanks in advance
I have a building system in place and I want the player to be able to save. I don't know how to do this, and the only answers the internet gave me are for Godot 3
Thanks in advance
By "save a tileset" I think you mean saving tiles placed in map cells. That is pretty general question and method of doing this may vary depending on project and needs.
If you have small map with known const size you can just save info for all cells. Assuming you have map of size 100x100 you can do something like:
#Dictionary with tilemap data to save, you can save it as json or something
var myTileMapData : Dictionary = { "data": [] }
for y in 100:
for x in 100:
for layer in tilemap.get_layers_count():
if(myTileMapData.data.size() <= layer):
myTileMapData.data.append( [] )
var pos : Vector2i = Vector2i(x, y)
var coords : Vector2i = tilemap.get_cell_atlas_coords(layer, pos)
var source : int = tilemap.get_cell_source_id(layer, pos)
var altTile : int = tilemap.get_cell_alternative_tile(layer, pos)
myTileMapData.data[layer].append( { "pos": pos, "coords": coords, "source": source, "alt_tile": altTile } )
#Now you can save myTileMapData
var saveFile = FileAccess.open(savePath, FileAccess.WRITE)
if(FileAccess.get_open_error() != OK):
return false
saveFile.store_string(JSON.stringify(myTileMapData))
That is some simple example I did right now, not tested or anything.
If you have big procedural generated map then probably it would be much more efficient to store only some data about map required to rebuild it after loading (like seed from which map was generated and saving only cells modified by players) and split world into chunks as it is done in most cases (to allow easier streaming to/from hard drive).
Anyway I hope it gives you some ideas.
Of course my example of storage is very simple and not optimal for sure. You can save data in better way for example in binary format. If you want to save more space (make save files smaller) you can use even more complex structures and save your tilemap data in quad tree or similar structure.
GlyphTheWolf Thanks! I tried to make a complimentary loading script but its not working. Could you tell me whats wrong here?
`func load_world(name:String):
var Player = $"../Player"
Player.position = Vector2(0, 0)
var tilemap = $"../Blocks"
clear()
var saveFile = FileAccess.open(str("user://", name, ".save"), FileAccess.READ)
if not saveFile.file_exists(str("user://", name, ".save")):
return
while saveFile.get_position() != saveFile.get_length():
var json_string = saveFile.get_line()
var json = JSON.new()
var parse_result = json.parse(json_string)
if not parse_result == OK:
return
var tile_data = json.get_data()
tilemap.set_cell(0, tile_data["pos"], tile_data["source"], tile_data["coords"], tile_data["alt_tile"])
Player.position = Vector2(0, 0)
seed = saveFile.get_line()
create_world(seed)
func create_world(s):
seed = s
moisture.seed = seed
temperature.seed = seed
altitude.seed = seed`
Mal For loading data from my example I would do something like:
var saveFile = FileAccess.open(loadPath, FileAccess.READ)
if(FileAccess.get_open_error() != OK):
return false
var jsonData = saveFile.get_as_text()
var json_conv = JSON.new()
json_conv.parse(jsonData)
var loadedData : Dictionary = json_conv.get_data()
#loadedData should be dictionary in same format as when you saved it
#so you should be able to restore tiles in tilemap like
for y in 100:
for x in 100:
for layer in loadedData.data.size():
tilemap.set_cell(layer, loadedData.data[layer][y*100 + x].pos, loadedData.data[layer][y*100 + x].source, loadedData.data[layer][y*100 + x].coords, loadedData.data[later][y*100 + x].alt_tile)
GlyphTheWolf Everytime i try to load a file, i get this error: Trying to assign value of type 'Nil' to a variable of type 'Dictionary'.
Mal That means json_conv.get_data()
have to return null
(or type not matching with Dictionary
type), so either file loading failed or there is some error while parsing loaded data as json. You could try to check value returned by json_conv.parse(jsonData)
if there is some error, also you can try to open save file with text editor and check if its content is expected json data.