how to implement IDrawable, IGameComponent, IUpdateable to draw automatically

I have this MeshObject class that derives from Object Model class which has it’s Draw/update/Initialize methods
How to make any instance of the MeshObject automatically be drawn when initialized without the need to include it the game draw method?
If possible?

public class MeshObject : ObjectModel
 {
 public override void Update(GameTime time)
 {

 }
 public override void Draw(GameTime time)
 {
     if (Context != null && Vertices != null)
     {

         Context.Device.DrawUserIndexedPrimitives(PrimitiveType.TriangleList, Vertices.PositionColors, 0, Vertices.PositionColors.Length, Indices.ToArray(), 0, Indices.Count / 3);

     }
base.Draw(time);
 }

 public override void Initialize()
 {
    
 }
}

    public abstract class ObjectModel : IDrawable, INotifyPropertyChanged, IGameComponent, IUpdateable
    {
        public Matrix ModelMatrix { get; set; }
        private bool _visible = true;
        private bool _isselectable = true;
        private bool _isselected = false;
        private bool _iseditable = true;
        private int _drawOrder = (int)ComponentDrawOrderEnum.Default;
        private bool _enabled = true;
        private int _updateOrder = (int)ComponentUpdateOrderEnum.Default;

        public event PropertyChangedEventHandler? PropertyChanged;
        public event EventHandler<EventArgs> DrawOrderChanged;
        public event EventHandler<EventArgs> VisibleChanged;
        public event EventHandler<EventArgs> EnabledChanged;
        public event EventHandler<EventArgs> UpdateOrderChanged;

        public int DrawOrder
        {
            get { return _drawOrder; }
            set
            {
                if (SetProperty(ref _drawOrder, value))
                {
                    var ev = DrawOrderChanged;
                    ev?.Invoke(this, EventArgs.Empty);
                }
            }
        }

        public bool Visible
        {
            get { return _visible; }
            set
            {
                if (SetProperty(ref _visible, value))
                {
                  
                    var ev = VisibleChanged;
                    ev?.Invoke(this, EventArgs.Empty);
                }
            }
        }
        public bool IsSeletable
        {
            get { return _isselectable; }
            set
            {
                if (SetProperty(ref _isselectable, value))
                {

                    var ev = VisibleChanged;
                    ev?.Invoke(this, EventArgs.Empty);
                }
            }
        }
        public bool IsSelected
        {
            get { return _isselected; }
            set
            {
                if (SetProperty(ref _isselected, value))
                {

                    var ev = VisibleChanged;
                    ev?.Invoke(this, EventArgs.Empty);
                }
            }
        }
        public bool IsEditable
        {
            get { return _iseditable; }
            set
            {
                if (SetProperty(ref _iseditable, value))
                {

                    var ev = VisibleChanged;
                    ev?.Invoke(this, EventArgs.Empty);
                }
            }
        }
        public bool Enabled
        {
            get { return _enabled; }
            set
            {
                if (SetProperty(ref _enabled, value))
                {
                    var ev = EnabledChanged;
                    ev?.Invoke(this, EventArgs.Empty);
                }
            }
        }

        public int UpdateOrder
        {
            get { return _updateOrder; }
            set
            {
                if (SetProperty(ref _updateOrder, value))
                {
                    var ev = UpdateOrderChanged;
                    ev?.Invoke(this, EventArgs.Empty);
                }
            }
        }
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        public bool SetProperty<T>(ref T storage, T value, [System.Runtime.CompilerServices.CallerMemberName] string propertyName = null)
        {
            if (object.Equals(storage, value))
            {
                return false;
            }

            storage = value;
            OnPropertyChanged(propertyName);
            return true;
        }

      public virtual void Initialize() { }
public virtual void Update(GameTime time) { }
     
public virtual void Draw(GameTime gameTime) { }
}


Are you not adding components via Game.Compnents.Add()?

1 Like