codeAPI Reference
s&box ICookieSource: Cookie persistence interface
ICookieSource API Reference
ICookieSource is an interface for components that want to save their properties as cookies (persistent user preferences stored in Game.Cookies).
Type Signature
CSHARP
public interface ICookieSource
{
string CookiePrefix { get; }
}Property
- string CookiePrefix { get; } - Prefix for cookie names without a trailing period. Example: "tool.balloon"
Extension Methods
The CookieSourceExtensions class provides SaveCookies() and LoadCookies() methods:
CSHARP
public static class CookieSourceExtensions
{
public void SaveCookies();
public void LoadCookies();
}SaveCookies()
Saves any instance properties with [Property] attribute (excluding those with [JsonIgnore]) to Game.Cookies. Properties are serialized to JSON.LoadCookies()
Loads properties from Game.Cookies and deserializes them back to the instance.Usage
CSHARP
public class BalloonTool : ToolMode, ICookieSource
{
public string CookiePrefix => "tool.balloon";
[Property] public float Size { get; set; } = 1.0f;
[Property] public Color Color { get; set; } = Color.White;
protected override void OnStart()
{
LoadCookies(); // Load saved preferences
}
protected override void OnDestroy()
{
SaveCookies(); // Save preferences on destroy
}
}Notes
- Only properties with [Property] attribute are saved/loaded.
- Properties with [JsonIgnore] are excluded.
- Properties must be both readable and writable.
- Static properties are not supported.
- Cookies are stored per-user and persist across sessions.
- Uses Game.Cookies.SetString/GetString internally with JSON serialization.
- Cookie names are formatted as "{Prefix}.{PropertyName}" (lowercase).
Was this helpful?