I have an interface called IHierarchable, intended for both the 'Entity' class and the 'Scene' class which serves as the root of all entities.
/// <summary>
/// Signifies that an object can exist in the game hierarchy and can have children.
/// </summary>
public interface IHierarchable
{
public abstract Scene Scene { get; protected init; }
/// <summary>
/// The parent of this object.
/// </summary>
public abstract IHierarchable Parent { get; protected set; }
/// <summary>
/// Any children belongong to this object.
/// </summary>
public abstract ReadOnlyCollection<IHierarchable> Children { get; protected set; }
}
But when I implement this on my Scene class:
/// <summary>
/// A base 2D world, into which <see cref="Entity"/> instances can be placed.
/// </summary>
public class Scene : IHierarchable
{
Scene IHierarchable.Scene { get; init; }
public IHierarchable Parent { get; set; }
public ReadOnlyCollection<IHierarchable> Children { get; set; }
public Scene()
{
(this as IHierarchable).Scene = null;
}
}
I have the following error in the constructor:
Init-only property or indexer 'IHierarchable.Scene' can only be assigned in an object initializer, or on 'this' or 'base' in an instance constructor or an 'init' accessor
I have to implement the interface explicitly in this class, as the 'Scene' property happens to share the name of the 'Scene' class. But once I implement it this way, I'm unable to actually set the property, even though I'm setting it within Scene's constructor and using the 'this' keyword, just as the error message suggests.