Previously you could only store a single string in the IGame.Data between map restarts. This was reset once the map changed to another map or the server restarted.
Now you will be able to store data using key-value pairs instead similar to how cookies work using javascript.
You will have access to the local storage using IGame.LocalStorage.
You will also have access to a shared storage using IGame.GetSharedStorage(..). The shared storage can be read between different scripts.
The interface will look like this:
► Show Spoiler
Code: Select all
/// <summary>
/// Gets access to local storage.
/// This storage is permanent between server restarts and between different played levels.
/// Data is stored based on the current map/script's file path.
/// Quirk: You're limited to a practical max storage limit to about 10 MB file sizes.
/// Quirk: Changing the filename or placing the script in another folder will change the file path for the stored data.
/// </summary>
public abstract IScriptStorage LocalStorage { get; }
/// <summary>
/// Gets access to session storage.
/// This storage is maintained between games for individual level scripts or extension scripts.
/// This is reset when the server changes to a different map or the server restarts.
/// For permanent storage use LocalStorage instead.
/// </summary>
public abstract IScriptStorage SessionStorage { get; }
/// <summary>
/// Gets access to a shared local storage, based on a name. Name must be valid (see parameter for more info).
/// Calling this function the first time during a map will read the storage if it exist from file.
///
/// Note: This is a shared storage and any other script or map can also access it using the same name.
/// Other scripts can update this data live.
/// Storing a key-value in one frame will not guarantee it exist in the next frame, depending on what other scripts do.
/// You are safe to write and read key-values in the same frame though.
///
/// Quirk: Getting the shared storage the first time can potentially cause the game to freeze randomly for a short period in case the end-user's disk activity is busy or currently sleeping.
/// To avoid potential freezing problems make sure to access any shared storage you may want to access in the OnStartup() function in your script
/// as your script and map has already been read from file a few moments ago and therefor the disk activity is ready to read again.
/// </summary>
/// <param name="name">The name must only contain letters (and no whitespace characters) and be a valid filename and must not exceed 40 characters and be at least 3 characters long.
/// This is case insensitive, so 'ABC', 'Abc' and 'abc' will all point to the same shared storage.</param>
/// <returns>A shared IScriptStorage handle. null if name is invalid.</returns>
public abstract IScriptStorage GetSharedStorage(string name);
► Show Spoiler
Code: Select all
/// <summary>
/// Stores a value.
/// </summary>
/// <param name="key">Key</param>
/// <param name="value">Value</param>
public abstract void SetItem(string key, string value);
/// <summary>
/// Stores a value.
/// </summary>
/// <param name="key">Key</param>
/// <param name="value">Value</param>
public abstract void SetItem(string key, int value);
/// <summary>
/// Stores a value.
/// </summary>
/// <param name="key">Key</param>
/// <param name="value">Value</param>
public abstract void SetItem(string key, bool value);
/// <summary>
/// Stores a value.
/// </summary>
/// <param name="key">Key</param>
/// <param name="value">Value</param>
public abstract void SetItem(string key, float value);
/// <summary>
/// Stores a value.
/// </summary>
/// <param name="key">Key</param>
/// <param name="value">Value</param>
public abstract void SetItem(string key, string[] value);
/// <summary>
/// Stores a value.
/// </summary>
/// <param name="key">Key</param>
/// <param name="value">Value</param>
public abstract void SetItem(string key, int[] value);
/// <summary>
/// Stores a value.
/// </summary>
/// <param name="key">Key</param>
/// <param name="value">Value</param>
public abstract void SetItem(string key, bool[] value);
/// <summary>
/// Stores a value.
/// </summary>
/// <param name="key">Key</param>
/// <param name="value">Value</param>
public abstract void SetItem(string key, float[] value);
/// <summary>
/// Gets a value.
/// </summary>
/// <param name="key">Key</param>
public abstract object GetItem(string key);
/// <summary>
/// Gets a value.
/// </summary>
/// <param name="key">Key</param>
/// <param name="value">Value</param>
/// <returns>True if the item was found in storage.</returns>
public abstract bool TryGetItem(string key, out object value);
/// <summary>
/// Gets a value.
/// </summary>
/// <param name="key">Key</param>
/// <param name="value">Value</param>
/// <returns>True if the item was found in storage.</returns>
public abstract bool TryGetItemString(string key, out string value);
/// <summary>
/// Gets a value.
/// </summary>
/// <param name="key">Key</param>
/// <param name="value">Value</param>
/// <returns>True if the item was found in storage.</returns>
public abstract bool TryGetItemInt(string key, out int value);
/// <summary>
/// Gets a value.
/// </summary>
/// <param name="key">Key</param>
/// <param name="value">Value</param>
/// <returns>True if the item was found in storage.</returns>
public abstract bool TryGetItemBool(string key, out bool value);
/// <summary>
/// Gets a value.
/// </summary>
/// <param name="key">Key</param>
/// <param name="value">Value</param>
/// <returns>True if the item was found in storage.</returns>
public abstract bool TryGetItemFloat(string key, out float value);
/// <summary>
/// Gets a value.
/// </summary>
/// <param name="key">Key</param>
/// <param name="value">Value</param>
/// <returns>True if the item was found in storage.</returns>
public abstract bool TryGetItemStringArr(string key, out string[] value);
/// <summary>
/// Gets a value.
/// </summary>
/// <param name="key">Key</param>
/// <param name="value">Value</param>
/// <returns>True if the item was found in storage.</returns>
public abstract bool TryGetItemIntArr(string key, out int[] value);
/// <summary>
/// Gets a value.
/// </summary>
/// <param name="key">Key</param>
/// <param name="value">Value</param>
/// <returns>True if the item was found in storage.</returns>
public abstract bool TryGetItemBoolArr(string key, out bool[] value);
/// <summary>
/// Gets a value.
/// </summary>
/// <param name="key">Key</param>
/// <param name="value">Value</param>
/// <returns>True if the item was found in storage.</returns>
public abstract bool TryGetItemFloatArr(string key, out float[] value);
/// <summary>
/// Checks if storage contains the key.
/// </summary>
/// <param name="key">Key</param>
public abstract bool ContainsKey(string key);
/// <summary>
/// Checks if storage contains the key-value.
/// </summary>
/// <param name="key">Key</param>
/// <param name="value">Value</param>
/// <returns>True if the item is in storage.</returns>
public abstract bool ContainsKeyValue(string key, string value);
/// <summary>
/// Checks if storage contains the key-value.
/// </summary>
/// <param name="key">Key</param>
/// <param name="value">Value</param>
/// <returns>True if the item is in storage.</returns>
public abstract bool ContainsKeyValue(string key, int value);
/// <summary>
/// Checks if storage contains the key-value.
/// </summary>
/// <param name="key">Key</param>
/// <param name="value">Value</param>
/// <returns>True if the item is in storage.</returns>
public abstract bool ContainsKeyValue(string key, bool value);
/// <summary>
/// Checks if storage contains the key-value.
/// </summary>
/// <param name="key">Key</param>
/// <param name="value">Value</param>
/// <returns>True if the item is in storage.</returns>
public abstract bool ContainsKeyValue(string key, float value);
/// <summary>
/// Checks if storage contains the key-value.
/// </summary>
/// <param name="key">Key</param>
/// <param name="value">Value</param>
/// <returns>True if the item is in storage.</returns>
public abstract bool ContainsKeyValue(string key, string[] value);
/// <summary>
/// Checks if storage contains the key-value.
/// </summary>
/// <param name="key">Key</param>
/// <param name="value">Value</param>
/// <returns>True if the item is in storage.</returns>
public abstract bool ContainsKeyValue(string key, int[] value);
/// <summary>
/// Checks if storage contains the key-value.
/// </summary>
/// <param name="key">Key</param>
/// <param name="value">Value</param>
/// <returns>True if the item is in storage.</returns>
public abstract bool ContainsKeyValue(string key, bool[] value);
/// <summary>
/// Checks if storage contains the key-value.
/// </summary>
/// <param name="key">Key</param>
/// <param name="value">Value</param>
/// <returns>True if the item is in storage.</returns>
public abstract bool ContainsKeyValue(string key, float[] value);
/// <summary>
/// Gets all current keys registered with a value.
/// </summary>
/// <returns>All keys</returns>
public abstract string[] GetKeys();
/// <summary>
/// Removes an item from storage.
/// </summary>
/// <param name="key">Key</param>
/// <returns>True if the item existed in the storage.</returns>
public abstract bool RemoveItem(string key);
/// <summary>
/// Clears all current keys in the storage.
/// </summary>
public abstract void Clear();
/// <summary>
/// Removes all key-value pairs where the value is null.
/// </summary>
/// <returns>true if any null values where removed.</returns>
public abstract bool RemoveNullValues();
/// <summary>
/// Writes the current storage data as to the output window in the map editor.
/// For debugging only.
/// </summary>
public abstract void WriteToConsole();
/// <summary>
/// Gets if any key-value pair exist.
/// </summary>
/// <returns>true if any key-value pair exist</returns>
public abstract bool HasData();
This local storage can open up for some configurable extension scripts (and maps) to the end user.
► Show Spoiler
Code: Select all
int MyX;
public void OnStartup()
{
if (!Game.LocalStorage.TryGetItemInt("X", out MyX))
{
MyX = 10; // default MyX to 10 or something
}
Events.UserMessageCallback.Start(OnUserMessage);
}
public void OnUserMessage(UserMessageCallbackArgs args)
{
if (args.IsCommand && args.Command == "SETX")
{
int value = 0;
if (int.TryParse(args.CommandArguments, out value))
{
// Update MyX to user input.
MyX = value;
Game.LocalStorage.SetItem("X", value);
}
}
}