Vice City: Multiplayer

Community Projects => SLC's Squirrel Plugin => Bug Reports => Topic started by: EK.IceFlake on June 13th, 2017, 03:51 PM

Title: [Bug] ConnectOnce getting called multiple times
Post by: EK.IceFlake on June 13th, 2017, 03:51 PM
I have an event:
Code: [Select]
            player.On.Spawn.ConnectOnce(function ()
            {
                /*if (authenciated == true) return;
                else authenciated = true;*/ // << currently using this workaround

                player.LoggingIn    = true;
                player.Health       = 0;

                player.On.RequestClass.ConnectOnce(function (classid)
                {
                    player.World        = CWorlds.Default;
                    player.LoggingIn    = false;

                    player.Login();

                    player.StartStream();
                    player.StreamInt(CPacketsOutgoing.AuthenciateResult);
                    player.StreamByte(CEncodedBoolean.True);
                    player.FlushStream(true);
                });
            });
It gets called whenever I spawn despite the fact that it says ConnectOnce. I've verified that that event is getting connected only once.
Title: Re: [Bug] ConnectOnce getting called multiple times
Post by: . on June 13th, 2017, 04:06 PM
The ConnectOnce() performs a search in the signal to make sure the function/slot that you're trying to connect wasn't connected before. As you can see here(https://github.com/iSLC/VCMP-SqMod/blob/b642e233a797a85e7f08f44eca5c3e2b5d68d9f1/source/Signal.cpp#L705), it removes/eliminates any similar slots (if any) then proceeds to connecting. The purpose of this is to prevent you from connecting the same function/slot multiple times, thus receiving multiple calls during the same emission.

I assume you thought the connected function/slot is only called once and then removed? If that's the case the answer is no. And to add that functionality would impose a serious performance penalty when emitting a signal or managing connected slots.

Try to not use an anonymous function. Maybe the VM creates a new instance of that function each time. Therefore having a new address each time you connect it. And since the builtin Squirrel hashing relies on the instance address in memory to obtain a hash. It receives a new hash each time it is connected. Therefore, it can't find it connected when you call ConnectOnce() so it assumes you connect it for the first time. If that's the case.

Or, again, try not use an anonymous function (or store it somewhere) and make it so that it removes itself manually from the signal when called. The signals and slots implementation was implemented in such way that it is safe to add or remove slots even when the slot is currently emitted and even during recursive emission.