// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved.
using System;
namespace FlaxEngine
{
///
/// The scripting object soft reference. Objects gets referenced on use (ID reference is resolving it).
///
public struct SoftObjectReference : IComparable, IComparable
{
private Guid _id;
private Object _object;
///
/// Gets or sets the object identifier.
///
public Guid ID
{
get => _id;
set
{
if (_id == value)
return;
_id = value;
_object = null;
}
}
///
/// Gets the object reference.
///
/// The object type.
/// The resolved object or null.
public T Get() where T : Object
{
if (!_object)
_object = Object.Find(ref _id, typeof(T));
return _object as T;
}
///
/// Sets the object reference.
///
/// The object.
public void Set(Object obj)
{
_object = obj;
_id = obj?.ID ?? Guid.Empty;
}
///
public override string ToString()
{
if (_object)
return _object.ToString();
return _id.ToString();
}
///
public override int GetHashCode()
{
return _id.GetHashCode();
}
///
public int CompareTo(object obj)
{
if (obj is SoftObjectReference other)
return CompareTo(other);
return 0;
}
///
public int CompareTo(SoftObjectReference other)
{
return _id.CompareTo(other._id);
}
}
}