FabianTerhorst / coreclr-module

Old alt:V CoreClr (.NET Core Common Language Runtime) community made module. New: https://github.com/altmp/coreclr-module
MIT License
72 stars 67 forks source link

Alt.OffClient and AltAsync.OffClient don't work #243

Closed localcc closed 3 years ago

localcc commented 3 years ago
  1. Short description: registering OnClient in AltAsync or Alt registers fine, but when trying to unregister methods still get called when emitting event from clientside.
  2. What happens: OffClient does not unregister event
  3. What should happen: OffClient should unregister event and unregistered method souldn't be called when events are being emitted from clientside
  4. Steps and code to reproduce:
    1. Construct class
    2. Emit events, only one line is being printed Async event called! 0 or sync for sync event
    3. Call finish method, emit events, methods still get called, Async event called! 0 being printed
  5. Version: 3.0-dev16, nugets are updated to their latest versions

When trying to construct class again, two instances of class receive events, and this results in code not working properly

using System;
using System.Threading.Tasks;
using AltV.Net;
using AltV.Net.Async;

    public class TestEvents
    {
        public static int InstanceNumber = 0;
        private int _instanceNumber;

        private TestEvents()
        {
            this._instanceNumber = TestEvents.InstanceNumber;
            TestEvents.InstanceNumber++;
            AltAsync.OnClient<AltCorePlayer, int>("testEventJob:asyncEvent", this.AsyncEvent);
            Alt.OnClient<AltCorePlayer, int>("testEventJob:event", this.Event);
        }

        private async void AsyncEvent(AltCorePlayer player, int n)
        {
            Console.WriteLine($"Async event called! {this._instanceNumber}");   
        }

        private void Event(AltCorePlayer player, int n)
        {
            Console.WriteLine($"Sync event called! {this._instanceNumber}");
        }
        public void Finish()
        {
            AltAsync.OffClient<AltCorePlayer, int>("testEventJob:asyncEvent", this.AsyncEvent);
            Alt.OffClient<AltCorePlayer, int>("testEventJob:event", this.Event);
        }
    }
}
FabianTerhorst commented 3 years ago

Next dev update will make the Off functions functional by accepting the return value of the On Functions as second parameter. Using the delegate reference won't work for it since they are transformed internally.