ゲームの保存

はじめに

ゲームを保存するのは複雑です。例えば、 複数のレベルにまたがった複数のオブジェクトによる情報が保存される事が望ましい場合があります。 より高度なゲームシステムのセーブでは、任意の数のオブジェクトに関する追加情報の保存ができる必要があります。 これにより、ゲームがより複雑に成長するにつれて、保存機能を拡張できます。

注釈

ユーザー設定を保存する場合は、この目的で ConfigFile クラスを使用できます。

永続オブジェクトの識別

まず、ゲームセッション間で保持するオブジェクトと、それらのオブジェクトから保持する情報を特定する必要があります。このチュートリアルでは、グループを使用して保存するオブジェクトをマークおよび処理しますが、他の方法ももちろん可能です。

We will start by adding objects we wish to save to the "Persist" group. We can do this through either the GUI or script. Let's add the relevant nodes using the GUI:

../../_images/groups.png

これが完了したら、ゲームを保存する必要があるときに、このスクリプトを使用して、保存するすべてのオブジェクトを取得し、それをすべて保存するように指示できます:

var save_nodes = get_tree().get_nodes_in_group("Persist")
for i in save_nodes:
    # Now, we can call our save function on each node.

シリアル化

次のステップは、データをシリアル化することです。これにより、ディスクからの読み取りとディスクへの保存がはるかに簡単になります。この場合、グループ Persist の各メンバーはインスタンス化されたノードであり、したがってパスを持っていると想定しています。GDScriptには to_json()parse_json() などのヘルパー関数があり、辞書を使用します。ノードには、このデータを返す保存関数を含める必要があります。保存機能は次のようになります:

func save():
    var save_dict = {
        "filename" : get_filename(),
        "parent" : get_parent().get_path(),
        "pos_x" : position.x, # Vector2 is not supported by JSON
        "pos_y" : position.y,
        "attack" : attack,
        "defense" : defense,
        "current_health" : current_health,
        "max_health" : max_health,
        "damage" : damage,
        "regen" : regen,
        "experience" : experience,
        "tnl" : tnl,
        "level" : level,
        "attack_growth" : attack_growth,
        "defense_growth" : defense_growth,
        "health_growth" : health_growth,
        "is_alive" : is_alive,
        "last_attack" : last_attack
    }
    return save_dict

これにより、スタイルが { "variable_name":value_of_variable } の辞書が得られます。これはロード時に役立ちます。

データの保存と読み取り

ファイルシステム チュートリアルで説明したように、ファイルを開いて、書き込みまたは読み取りができるようにする必要があります。グループを呼び出して関連データを取得する方法ができたので、to_json() を使用して、簡単に保存できる文字列に変換し、ファイルに保存しましょう。このようにすると、各行が独自のオブジェクトになります。そのため、ファイルからデータを簡単に引き出すことができます。

# Note: This can be called from anywhere inside the tree. This function is
# path independent.
# Go through everything in the persist category and ask them to return a
# dict of relevant variables.
func save_game():
    var save_game = File.new()
    save_game.open("user://savegame.save", File.WRITE)
    var save_nodes = get_tree().get_nodes_in_group("Persist")
    for node in save_nodes:
        # Check the node is an instanced scene so it can be instanced again during load.
        if node.filename.empty():
            print("persistent node '%s' is not an instanced scene, skipped" % node.name)
            continue

        # Check the node has a save function.
        if !node.has_method("save"):
            print("persistent node '%s' is missing a save() function, skipped" % node.name)
            continue

        # Call the node's save function.
        var node_data = node.call("save")

        # Store the save dictionary as a new line in the save file.
        save_game.store_line(to_json(node_data))
    save_game.close()

ゲームが保存されました!読み込みも非常に簡単です。そのために、各行を読み取り、parse_json() を使用してそれを辞書に戻し、反復処理を行って辞書の値を読み取ります。ただし、最初にオブジェクトを作成する必要があり、ファイル名と親の値を使用してそれを実現できます。読み込み関数は次のとおりです:

# Note: This can be called from anywhere inside the tree. This function
# is path independent.
func load_game():
    var save_game = File.new()
    if not save_game.file_exists("user://savegame.save"):
        return # Error! We don't have a save to load.

    # We need to revert the game state so we're not cloning objects
    # during loading. This will vary wildly depending on the needs of a
    # project, so take care with this step.
    # For our example, we will accomplish this by deleting saveable objects.
    var save_nodes = get_tree().get_nodes_in_group("Persist")
    for i in save_nodes:
        i.queue_free()

    # Load the file line by line and process that dictionary to restore
    # the object it represents.
    save_game.open("user://savegame.save", File.READ)
    while save_game.get_position() < save_game.get_len():
        # Get the saved dictionary from the next line in the save file
        var node_data = parse_json(save_game.get_line())

        # Firstly, we need to create the object and add it to the tree and set its position.
        var new_object = load(node_data["filename"]).instance()
        get_node(node_data["parent"]).add_child(new_object)
        new_object.position = Vector2(node_data["pos_x"], node_data["pos_y"])

        # Now we set the remaining variables.
        for i in node_data.keys():
            if i == "filename" or i == "parent" or i == "pos_x" or i == "pos_y":
                continue
            new_object.set(i, node_data[i])

    save_game.close()

これで、シーン ツリーのほぼ任意の場所に配置された任意の数のオブジェクトを保存して読み込むことができます!各オブジェクトは、保存する必要があるデータに応じて異なるデータを格納できます。

いくつかの注意事項

ゲームの状態を読み込むための設定に焦点をあてて説明をおこないました。最終的には、このロジックの多くがプロジェクトの作成者に委ねられます。これはしばしば複雑であり、個々のプロジェクトのニーズに基づいて大幅にカスタマイズする必要があります。

さらに、この実装では、Persistオブジェクトは他のPersistオブジェクトの子ではないと想定しています。そうしないと、無効なパスが作成されます。ネストされた永続オブジェクトに対応するには、オブジェクトを段階的に保存することを検討してください。最初に親オブジェクトをロードして、子オブジェクトがロードされたときに add_child() 呼び出しで使用できるようにします。NodePath はおそらく無効になるため、子を親にリンクする方法も必要です。