Skip to content

Commit

Permalink
Merge pull request #140 from BenjaTK/serialization
Browse files Browse the repository at this point in the history
Add serialization functions
  • Loading branch information
BenjaTK authored Jul 18, 2024
2 parents f4fbccc + e50c13b commit 3e2e31e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
11 changes: 11 additions & 0 deletions addons/gaea/generators/generator.gd
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ func get_seed() -> int:
return seed


## Returns the currently generated grid as a [PackedByteArray], allowing you to deserialize it later using [method deserialize].
func serialize() -> PackedByteArray:
return var_to_bytes(grid.get_grid())


## Deserializes the [param bytes] obtained from [method serialize], setting the grid to its value.
func deserialize(bytes: PackedByteArray):
grid.set_grid_serialized(bytes)
grid_updated.emit()


### Modifiers ###


Expand Down
15 changes: 15 additions & 0 deletions addons/gaea/grid.gd
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,21 @@ func set_grid(grid: Dictionary) -> void:
_grid = grid


## Sets the grid to the serialized grid that [param bytes] represents. See [method GaeaGenerator.serialize] and [method GaeaGenerator.deserialize].
func set_grid_serialized(bytes: PackedByteArray) -> void:
var grid = bytes_to_var(bytes)
if not (grid is Dictionary):
push_error("Attempted to deserialize invalid grid")
return

for layer in grid:
for tile in grid[layer]:
var object = grid[layer][tile]
if object is EncodedObjectAsID:
grid[layer][tile] = instance_from_id(object.get_object_id())
_grid = grid


## Returns the grid [Dictionary].
func get_grid() -> Dictionary:
return _grid
Expand Down

0 comments on commit 3e2e31e

Please sign in to comment.