[Bug] ConnectOnce getting called multiple times

Started by EK.IceFlake, Jun 13, 2017, 02:51 PM

Previous topic - Next topic

EK.IceFlake

I have an event:
            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.

.

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, 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.
.