C#
C#

help

Root Question Message

Velcer
Velcer2/21/2023
❔ Any Danger to using Actions in this way?

    public class Entity
    {
        public event Action<int> OnUpdateFrame;

        internal void OnNewTick(int tick) 
        {
            OnUpdateFrame?.Invoke(tick);
        }
    }

    public class Frog : Entity
    {
        public Frog() 
        {
            OnUpdateFrame += Frog_OnUpdateFrame;
        }

        private void Frog_OnUpdateFrame(int obj)
        {
            throw new NotImplementedException();
        }
    }

    public class SuperFrog : Frog
    {
        public SuperFrog() : base()
        {
            OnUpdateFrame += SuperFrog_OnUpdateFrame;
        }

        private void SuperFrog_OnUpdateFrame(int obj)
        {
            throw new NotImplementedException();
        }
    }
x0rld ⛄ 🎄
x0rld ⛄ 🎄2/21/2023
what is Frog_OnUpdateFrame ?
x0rld ⛄ 🎄
x0rld ⛄ 🎄2/21/2023
you should have OnUpdateFrame protected to not allow external access
Velcer
Velcer2/21/2023
Thank you. So I want to use Actions to let higher level classes subscribe to the update method.
Velcer
Velcer2/21/2023
I want to know if this is potentially going to hurt the performance of my code versus just using overrides.
x0rld ⛄ 🎄
x0rld ⛄ 🎄2/21/2023
you can declare it virtual protected in your entity
x0rld ⛄ 🎄
x0rld ⛄ 🎄2/21/2023
you can do a minimal usage and test the 2 way with Benchmark.NET
ryzngard
ryzngard2/21/2023
if you're only going to do one handler for the event, a virtual method would be best
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy