Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing 4 Parameter ExtEvent #9

Open
fnnbrr opened this issue Nov 15, 2022 · 1 comment
Open

Missing 4 Parameter ExtEvent #9

fnnbrr opened this issue Nov 15, 2022 · 1 comment

Comments

@fnnbrr
Copy link

fnnbrr commented Nov 15, 2022

Hey there, I'm looking to replace UnityEvents in my project with ExtEvents but there doesn't seem to be an ExtEvent<T1, T2, T3, T4> available to replace UnityEvent<T0, T1, T2, T3> which I'm currently using.

I imagine it would look like this (just a modification of ExtEvent<T1, T2, T3>), let me know if you'd like a PR:

namespace ExtEvents
{
    using System;
    using System.Runtime.CompilerServices;
    using JetBrains.Annotations;

    [Serializable]
    public class ExtEvent<T1, T2, T3, T4> : BaseExtEvent
    {
        private readonly unsafe void*[] _arguments = new void*[4];

        private Type[] _eventParamTypes;
        protected override Type[] EventParamTypes => _eventParamTypes ??= new Type[] { typeof(T1), typeof(T2), typeof(T3), typeof(T4) };

        /// <summary>
        /// The dynamic listeners list that you can add your listener to.
        /// </summary>
        [PublicAPI]
        public event Action<T1, T2, T3, T4> DynamicListeners;
        internal override Delegate _dynamicListeners => DynamicListeners;

        /// <summary>
        /// Invokes all listeners of the event.
        /// </summary>
        [PublicAPI]
        public void Invoke(T1 arg1, T2 arg2, T3 arg3, T4 arg4)
        {
            unsafe
            {
                _arguments[0] = Unsafe.AsPointer(ref arg1);
                _arguments[1] = Unsafe.AsPointer(ref arg2);
                _arguments[2] = Unsafe.AsPointer(ref arg3);
                _arguments[3] = Unsafe.AsPointer(ref arg4);

                // ReSharper disable once ForCanBeConvertedToForeach
                for (int index = 0; index < _persistentListeners.Length; index++)
                {
                    _persistentListeners[index].Invoke(_arguments);
                }
            }

            DynamicListeners?.Invoke(arg1, arg2, arg3, arg4);
        }

        public static ExtEvent<T1, T2, T3, T4> operator +(ExtEvent<T1, T2, T3, T4> extEvent, Action<T1, T2, T3, T4> listener)
        {
            if (extEvent == null)
                return null;

            extEvent.DynamicListeners += listener;
            return extEvent;
        }

        public static ExtEvent<T1, T2, T3, T4> operator -(ExtEvent<T1, T2, T3, T4> extEvent, Action<T1, T2, T3, T4> listener)
        {
            if (extEvent == null)
                return null;

            extEvent.DynamicListeners -= listener;
            return extEvent;
        }
    }
}
@fnnbrr
Copy link
Author

fnnbrr commented Nov 15, 2022

For anyone else who also needs this functionality now, these steps worked for me to get ExtEvent<T1, T2, T3, T4> working:

  1. Make a new folder where you'll place your code
  2. In that folder, add an Assembly Definition Reference that references the ExtEvents Assembly Definition
  3. Create a file named ExtEvent`4.cs and paste in the code from my post above

Works as expected with version 1.7.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant