Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

The Storage String

Malware edited this page Aug 15, 2023 · 6 revisions

Sometimes you need to store information that must survive reloading or shutting down the game, or during recompilation of your script. The grid program currently provides only one way to do this: The Storage property. This property resides in your script's base class MyGridProgram and as such can be accessed by any method or property in your script - but not directly by any subclass (see the bottom of this page - "The Grid Terminal System and Subclasses" - for a way to get around that problem).

The Storage string is persisted when the Save method is called by the game.

(See Handling configuration and storage for a more advanced and flexible way to deal with the actual data in Storage)

The Storage property is simply a string property:

public void Save()
{
    Storage = "My String"
}

As you can see, nothing special about its use. The difference between this property and any other is the fact that its content is saved to your disk when the game is saved.

Changing it to just any old string, that's not very useful, is it. What if you need to save something more complicated? Well, I'm afraid you're gonna have to do the hard work yourself.

The absolutely most simple and primitive way of storing multiple values in the Storage is by using string concatenation or string.Join for saving, and string.Split for loading.

I would recommend you using Program() to read your Storage, and Save() to write to it and leave it at that, to avoid doing too much work too often.

string _someTextDescription;
int _someImportantNumber;

public Program() 
{
    string[] storedData = Storage.Split(';');
    // If there's at least one item in the storage data...
    if (storedData.Length >= 1)
    {
        // Retrieve first item. C# arrays are 0-indexed, meaning the first item in the list is item 0.
        _someTextDescription = storedData[0];
    }

    // If there's at least two items in the storage data...
    if (storedData.Length >= 2)
    {
        // Retrieve the second item. This time we want to try to convert it into a number.
        int.TryParse(storedData[1], out _someImportantNumber);
    }
}

public void Save()
{
    // Combine the state variables into a string separated by the ';' character
    Storage = string.Join(";",
        _someTextDescription ?? "",
        _someImportantNumber
    );
}

Note the use of int.TryParse in the Program(). We use this because we want to do our best to gracefully fail if the string contains something we don't understand. This can happen if we change the format of the string, or if we somehow manage to make a mistake during saving. Better safe than sorry.

In the same vein, note that we add the null-coalescing operator ?? to the line containing _someTextDescription. This is to handle the case where that field might be null, in which case we store an empty string instead.

This method of using Storage should be enough for most smaller scripts.

Clone this wiki locally