#How can I dynamically generate and save maps for new game sessions in a save slot system

1 messages · Page 1 of 1 (latest)

latent crescent
#

I'm working on a save slot system in Godot where every new game session (started from a new save slot) should generate a unique map. I want to:

  1. Dynamically generate a map when creating a new save.
  2. Store the map data in the save file.
  3. Load and reconstruct the map from the save file when the game is resumed.

What are the best practices for handling this? Should I include a random seed for reproducibility, and how do I make the system scalable for different map generation algorithms?

Language and software / platform info:
Godot v4, 2D, GDScript

Would love to hear your thoughts, examples, or advice!

tribal plaza
#

I implemented a flexible system roughly like this (not complete code, but perhaps some inspiration):

Base class for all scenarios:

class_name ScenarioBase extends Node3D

var config: Dictionary

func get_parameters() -> Dictionary:
    push_error("get_parameters called on ScenarioBase")
    breakpoint
    return {}

func set_config(cfg) -> bool:
    if ScenarioConfig.validate_config(cfg, get_parameters()):
        print("Config set")
        config = cfg
        return true
    else:
        print("Invalid config for scenario")
        return false

I create a bunch of scenes which extends ScenarioBase, and save them in res://scenarios.

In my main game node:

func load_scenario(scenario_name: String):
    var scenario = ResourceLoader.load("res://scenarios/" + scenario_name + ".tscn")
    if scenario != null:
        print("Loading scenario " + scenario_name)
        var instance : ScenarioBase = scenario.instantiate()

        var scenario_params = instance.get_parameters()
        var config = ScenarioConfig.create_default_config(scenario_params)
        config["target_size"] = 0.32
        config["target_health"] = 10
        if instance.set_config(config):
            HUD.prepare_scenario(20)
            add_child(instance)