menu_bookDocumentation
IHotloadManaged: Preserving and Restoring State Across s&box Hotloads
IHotloadManaged: Preserving State Across s&box Hotloads
When a class is hotloaded in s&box, all live instances are replaced with new instances of the updated type. Field values are automatically copied over. But if you need custom logic during the swap — for example, to clean up native resources or re-initialize connections — implement IHotloadManaged.Interface Methods
CSHARP
public interface IHotloadManaged
{
// Called on the OLD instance before it's replaced.
// Write values to 'state' to pass them to the new instance.
void Destroyed( Dictionary<string, object> state ) { }
// Called on the NEW instance after it's created.
// Read values from 'state' that were written by the old instance.
void Created( IReadOnlyDictionary<string, object> state ) { }
// Called when the instance is processed but NOT replaced (no type change).
void Persisted() { }
// Called when the instance could not be upgraded and references to it are set to null.
void Failed() { }
}Usage Example
CSHARP
public class MyManager : IHotloadManaged
{
private List<string> _registeredNames = new();
void IHotloadManaged.Destroyed( Dictionary<string, object> state )
{
// Save state to pass to the new instance
state["names"] = _registeredNames.ToList();
}
void IHotloadManaged.Created( IReadOnlyDictionary<string, object> state )
{
// Restore state from the old instance
if ( state.TryGetValue( "names", out var names ) )
_registeredNames = (List<string>)names;
}
void IHotloadManaged.Failed()
{
// Instance couldn't be upgraded — clean up any unmanaged resources
}
}[SkipHotload] Attribute
Mark a field, property, class, or struct with [SkipHotload] to skip it during hotload processing. Useful for static caches, engine-internal state, or large object graphs that don't need swapping.CSHARP
[SkipHotload]
private static Dictionary<string, object> _cache = new();[SuppressNullKeyWarning] Attribute
When a Dictionary or HashSet key becomes null during hotload (because a type was removed), the engine warns. Suppress this with [SuppressNullKeyWarning] if you're sure it's safe to silently drop those entries.
Was this helpful?