SpatialEntity.TryGetGuid
bool TryGetGuid(Guid& guid)
A durable identifier for this entity that stays the same
across sessions and device reboots! Entities only have one once
they’re persisted, either by the system itself, or by a call to
Persist. Store it, and pass it to FindAnchor to get the same
physical entity back in a later session.
| Guid& guid | The entity’s persistent identifier, Guid.Empty if it has none. |
| RETURNS: bool | False if this entity has no persistence component. |
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!