Skip to content

Saving and Loading Worlds

There are times when you would want to save/load a world from a file. Luckily, Powdered World Engine provides a space-efficient method to save worlds as well as a built-in way to automatically load chunks from a file: the WorldStreamer class.


Sections


Creating/Opening a File

To start interacting with the filesystem, you will need to create a new WorldStreamer object. WorldStreamer has two functions for this, each for different scenarios:

  1. WorldStreamer.Create() for creating a new world file

  2. WorldStreamer.Open() for opening an existing world file

Both of these methods return a new WorldStreamer objects with the assigned file.


To Create a New File:

WorldStreamer MyStreamer = WorldStreamer.Create(WorldDirPath, FileName, Simulation);

In GDScript, the syntax would be the same except for the variable declaration and the semicolon.

This function takes a path to the desired directory to save the file, a name for the file, and a reference to the PowderSimulation node it will use to save and load from.

It creates a new .pwdr file in the specified directory, and returns a new WorldStreamer object with that file and PowderSimulation reference assigned to it. Because of this, it should be assigned to a variable.

As an example, if you wanted to save a new file called World1 to the user's AppData folder, this is what you would write:

WorldStreamer MyStreamer = WorldStreamer.Create("user://myGame/worlds/", "World1", SimRef);

To Open an Existing File:

WorldStreamer NewStreamer = WorldStreamer.Open(WorldPath, Simulation);

In GDScript, the syntax would be the same except for the variable declaration and the semicolon.

This function takes a path to the world file to open (must point to a file ending in .pwdr), and a reference to the PowderSimulation node it will use to save and load from.

It returns a new WorldStreamer object with that file and PowderSimulation reference assigned to it. Because of this, it should be assigned to a variable.

For example, if you wanted to open the file that you created in the previous section, you would write:

WorldStreamer NewStreamer = WorldStreamer.Open("user://myGame/worlds/World1.pwdr", SimRef);

Saving the World

There are four main functions you can use on a WorldStreamer object that help save parts of the world to the file: SaveWorld(), SaveChunk(), and FlushDeadChunks().

  • SaveWorld() saves all currently loaded chunks to the file, with an Override parameter which, if true, essentially clears the file and then saves the world.

  • SaveChunk() takes a Chunk object and saves that to the file. In most cases, SaveWorld() will be used over this.

  • FlushDeadChunks() does some background work to rewrite the file and remove unnecessary old chunk data. It also pushes the file changes to the actual filesystem. This is automatically called when the file is closed with WorldStreamer.Close().

  • UnloadChunk() takes a Chunk object and saves it to the file, then removes it from the simulation.

An example usage of this would be in an infrequent autosave system:

MyStreamer.SaveWorld();
MyStreamer.FlushDeadChunks();

This saves the world and rewrites the file to remove old data. For a system you would call more frequently, just SaveWorld() should suffice until you are able to call FlushDeadChunks().


Loading the World

There are two main functions for loading world data from a file in the WorldStreamer class: LoadWorld() and LoadChunk().

  • LoadWorld() Replaces all currently active chunks in the simulation with the ones saved in the file.

  • LoadChunk() takes a chunk position and either loads or adds a chunk with data from the file depending on if the chunk exists in the simulation or not.

    See Using PowderSimulation for an explanation on coordinate systems.

An example usage of LoadWorld() would be using it to load a previous save after the user left the game.


Closing the File

When you need to stop using a file, you must use the WorldStreamer.Close() method. It should automatically call when the WorldStreamer itself is freed, but it's a good idea to manually call it just in case.


Using WorldStreamer with PowderSimulation's FollowMode

See Using PowderSimulation to find out how to use its FollowMode properties.

You can assign a WorldStreamer object to a PowderSimulation node by using PowderSimulation.SetWorldStreamer(MyStreamer).

This allows the node to utilize LoadChunk() and SaveChunk in the assigned WorldStreamer object when FollowMode is true, instead of simply adding and removing chunks as needed.