SpatialEntity.Persist
bool Persist()
Ask the system to persist this entity, giving it a
durable identity that survives across sessions! This is
asynchronous, and safe to call right away since it waits until
the entity is tracking and persistence has started up. On success,
TryGetGuid succeeds and Changed flags the Persistence
component. On failure, Status becomes Partial and the entity
stays without a Guid. If an Unpersist is still in flight, this
persists again once it lands.
| RETURNS: bool | False if this can’t work at all, like an invalid entity, or one from a capability without persistence support. Check Spatial.ComponentsFor for SpatialComponent.Persistence to see which capabilities support it. |
Examples
Keeping track of anchors by Guid
For apps that keep anchors in their own save data instead of by
name. The Guid arrives a moment after Persist, so watch for it.
List<Guid> savedAnchors = new List<Guid>();
public void StepSavedAnchors()
{
foreach (SpatialEntity anchor in SpatialEntity.With(SpatialComponent.Anchor))
{
if (anchor.HasChanged(SpatialComponent.Persistence) && anchor.TryGetGuid(out Guid guid))
savedAnchors.Add(guid);
}
// Storage no longer having a Guid shows up as Failed
foreach (SpatialEntity anchor in SpatialEntity.Removed)
{
if (anchor.Status == SpatialStatus.Failed && anchor.TryGetGuid(out Guid lost))
savedAnchors.Remove(lost);
}
}
// In a later session, each Guid loads back into its anchor
public List<SpatialEntity> RestoreAnchors()
{
List<SpatialEntity> result = new List<SpatialEntity>();
foreach (Guid guid in savedAnchors)
result.Add(SpatialEntity.FindAnchor(guid));
return result;
}
Found an issue with these docs, or have some additional questions? Create an Issue on Github!