State not setting properly

Started by EK.IceFlake, May 06, 2017, 04:49 AM

Previous topic - Next topic

EK.IceFlake

Exec("Creating custom events", function ()
{
    SqCore.On().CPlayerRequestSpawn    <- SqCreateSignal();
    SqCore.On().CPlayerSpawned         <- SqCreateSignal();
});

SqCore.On().CPlayerRequestSpawn.Connect(function (player)
{
    player.Message(MessageType.Information, "V0t? U Request Spown?");
    SqCore.SetState(false);
});
            local last_state = SqCore.GetState();
            SqCore.SetState(1);

            SqCore.On().CPlayerRequestSpawn.Emit(player);

            local return_state = SqCore.GetState();
            SqCore.SetState(last_state);

            if (return_state == false || return_state == 0) return;
It doesn't feel much like returning for some reason...
::edit:: it was CPlayerRequestSpawn.Emit.
Fixed.

.

If the request spawn did not came from the server, then the state is useless and it has no effect on blocking someone from spawning or something else for example. When the plugin receives the event from the server. It sets the state to the default value and proceeds to distributing the event to whoever is interested. And then returns the last state that was found https://github.com/iSLC/VCMP-SqMod/blob/master/source/Main.cpp#L297

And I just realized that there's a weakness to this approach because other events are likely to overwrite the state if are emitted while another event is already emitted. Gonna try and do a fix for this as soon as possible. Probably by implementing a stack or something.

Anyway, I feel like there are missing pieces here that prevent me from understanding what the intention is. Therefore I can't understand the question itself.
.

EK.IceFlake

#2
Quote from: . on May 06, 2017, 01:36 PMIf the request spawn did not came from the server, then the state is useless and it has no effect on blocking someone from spawning or something else for example. When the plugin receives the event from the server. It sets the state to the default value and proceeds to distributing the event to whoever is interested. And then returns the last state that was found https://github.com/iSLC/VCMP-SqMod/blob/master/source/Main.cpp#L297

And I just realized that there's a weakness to this approach because other events are likely to overwrite the state if are emitted while another event is already emitted. Gonna try and do a fix for this as soon as possible. Probably by implementing a stack or something.

Anyway, I feel like there are missing pieces here that prevent me from understanding what the intention is. Therefore I can't understand the question itself.
Well... I saved the previous state, set the state to 1, called a requestspawn event, saved the return state, set the state to the previous state and checked if the return state was 0. If it is 0, it returns preventing the spawn code.

Anyways, check the code again, I noticed that I was calling PlayerRequestSpawn and not CPlayerRequestSpawn (weird, in my code I was calling CPlayerRequestSpawn).

.

#3
I've forgot to mention but the state is an integer. And here you do the same test:

if (return_state == false || return_state == 0) return;
Since integers of 0 or less will evaluate to false. This is essentially the same as doing: if (x == false  || x == false ) return;

And here "SqCore.SetState(1);" you set the state to 1. So unless a slot connected to that signal does a "SqCore.SetState(0);" Then "return_state" should be 1. Do a print of "return_state" and see what it does. Then try adding a function which changes the state, like:

SqCore.On().CPlayerRequestSpawn.Connect(this, function(player) {
    SqCore.SetState(34);
});

And see if "return_state" is 34.

Anyway, I've added some things which prevent recursive events, sent by the server, from overwriting the state value. Now every event has it's own state value and at the end, they restore what was previously there.
.

.

#4
Also, if you're trying to know whether a signal slot failed, you should probably use other methods of emitting the events. The implementation provides 5 of them Emit(), Query(), Consume(), Approve() and Request(). Here are some examples that demonstrate how they work and behave:


Emit:
print("==Emit==");
{
    local s = SqCreateSignal();

    s.Connect(this, function(n) {
        print("Hello from A "+n);
    });
    s.Connect(this, function(n) {
        print("Hello from B "+n);
    });
    s.Connect(this, function(n) {
        print("Hello from B "+n);
    });
    s.Connect(this, function(n) {
        print("Hello from C "+n);
    });
    local forward = 10;
    // Regular slot emission where the slots simply receive the signal
    s.Emit(14);
}

Query:
print("==Query==");
{
    local s = SqCreateSignal();

    s.Connect(this, function(n) {
        return n + 2;
    });
    s.Connect(this, function(n) {
        return n - 2;
    });
    s.Connect(this, function(n) {
        return n * 2;
    });
    s.Connect(this, function(n) {
        return n / 2;
    });
    local forward = 10;
    // All slots are invoked and their result is given as a parameter
    // to the function that we specified
    s.Query(this, function(r) {
        print("result was: " + r);
    }, forward);
}

Consume:
print("==Consume==");
{
    local s = SqCreateSignal();

    s.Connect(this, function(n) {
        if (n == 'A') {
            print("Consumed by A");
            return true;
        }
    });
    s.Connect(this, function(n) {
        if (n == 'B') {
            print("Consumed by B");
            return true;
        }
    });
    s.Connect(this, function(n) {
        if (n =='C') {
            print("Consumed by C");
            return true;
        }
    });
    s.Connect(this, function(n) {
        if (n == 'D') {
            print("Consumed by D");
            return true;
        }
    });
    // First slot that doesn't return null or false is considered to have consumed the value
    // => the boolean result is given back to us here
    if (s.Consume('C'))
    {
        print("was consumed");
    }

    if (!s.Consume('X'))
    {
        print("was not consumed");
    }
}

Approve:
print("==Approve==");
{
    local s = SqCreateSignal();

    s.Connect(this, function(n) {
        if (n < 2) {
            print("Criteria A failed");
            return false;
        }
    });
    s.Connect(this, function(n) {
        if (n < 3) {
            print("Criteria B failed");
            return false;
        }
    });
    s.Connect(this, function(n) {
        if (n < 4) {
            print("Criteria C failed");
            return false;
        }
    });
    s.Connect(this, function(n) {
        if (n < 5) {
            print("Criteria D failed");
            return false;
        }
    });
    // All slots must return null or true for the signal to be considered appropved
    // => the boolean result is given back to us here
    if (s.Approve(9)) // 9 is greater than all so it should be approved by all
    {
        print("was approved");
    }
    if (!s.Approve(3)) // 3 is less than 4 so that should disapprove
    {
        print("was not approved");
    }
}

Request:
print("==Request==");
{
    local s = SqCreateSignal();

    s.Connect(this, function(n) {
        if (n == 'A') {
            return "A satisfied the request";
        }
    });
    s.Connect(this, function(n) {
        if (n == 'B') {
            return "B satisfied the request";
        }
    });
    s.Connect(this, function(n) {
        if (n =='C') {
            return "C satisfied the request";
        }
    });
    s.Connect(this, function(n) {
        if (n == 'D') {
            return "D satisfied the request";
        }
    });
    // The first slot that doesn't return null is considered to have satisfied the request
    // => the reurned value is given back to us here
    print(s.Request('C'));
}
.