SA-MP Forums Archive
if-goto - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: if-goto (/showthread.php?tid=311518)



if-goto - 2KY - 17.01.2012

I'm currently trying to use this bit of code which uses an if-goto, if you're not familiar with what that is, see this page.

Here is what I have so far, which compiles perfectly fine, but it does not work and appears to stall my server. (Connected message for about five minutes so far)

pawn Код:
RESELECT:
if(last_RandomMessage == RandomSelection) {
   
    //Oh no! The random selected the same message as before! This is where we make it select again and again until it gets a different message!
    //To do this, we just simply create the random again!
    RandomSelection = random(3);
    last_RandomMessage = RandomSelection;
    if(last_RandomMessage == RandomSelection)
        goto RESELECT;
    //We do not return here, because we would like to let the rest of the code run.
}
Is there anyone who is experienced in this area, and would like to offer assistance? This is confusing the hell out of me.


Re: if-goto - Scenario - 17.01.2012

I too am interested in this...


Re: if-goto - Kar - 17.01.2012

pawn Код:
last_RandomMessage = RandomSelection;
reselect:
if(last_RandomMessage == RandomSelection) {

    //Oh no! The random selected the same message as before! This is where we make it select again and again until it gets a different message!
    //To do this, we just simply create the random again!
    RandomSelection = random(3);
    last_RandomMessage = RandomSelection;
    printf("test");
    if(last_RandomMessage == RandomSelection) goto reselect;
    printf("test passed");
    //We do not return here, because we would like to let the rest of the code run.
}
test that


Re: if-goto - 2KY - 17.01.2012

Quote:
Originally Posted by Kar
Посмотреть сообщение
pawn Код:
last_RandomMessage = RandomSelection;
reselect:
if(last_RandomMessage == RandomSelection) {

    //Oh no! The random selected the same message as before! This is where we make it select again and again until it gets a different message!
    //To do this, we just simply create the random again!
    RandomSelection = random(3);
    last_RandomMessage = RandomSelection;
    printf("test");
    if(last_RandomMessage == RandomSelection) goto reselect;
    printf("test passed");
    //We do not return here, because we would like to let the rest of the code run.
}
test that
Continuously prints "test".


Re: if-goto - Kar - 17.01.2012

then it works...

you realize you have
pawn Код:
last_RandomMessage = RandomSelection;
inside the if statement right?


Re: if-goto - 2KY - 17.01.2012

Quote:
Originally Posted by Kar
Посмотреть сообщение
then it works...

you realize you have
pawn Код:
last_RandomMessage = RandomSelection;
inside the if statement right?
I didn't realise that, no, but it's still working as it should.

Quote:

[22:59:45] This is random message numero dos!

[22:59:45] This is random message numero tres!
[22:59:45] This is random message numero uno!
[22:59:45] This is random message numero uno!
[22:59:45] This is random message numero dos!
[22:59:45] This is random message numero uno!
[22:59:45] This is random message numero tres!
[22:59:45] This is random message numero dos!
[22:59:45] This is random message numero uno!
[22:59:46] This is random message numero tres!
[22:59:46] This is random message numero tres!
[22:59:46] This is random message numero tres!
[22:59:46] This is random message numero tres!
[22:59:46] This is random message numero dos!
[22:59:46] This is random message numero tres!
[22:59:46] This is random message numero tres!
[22:59:46] This is random message numero dos!
[22:59:47] This is random message numero tres!
[22:59:47] This is random message numero dos!
[22:59:47] This is random message numero tres!
[22:59:47] This is random message numero tres!
[22:59:47] This is random message numero tres!
[22:59:47] This is random message numero dos!
[22:59:47] This is random message numero uno!
[22:59:47] This is random message numero tres!
[22:59:47] This is random message numero tres!
[22:59:48] This is random message numero uno!
[22:59:48] This is random message numero dos!
[22:59:48] This is random message numero tres!
[22:59:48] This is random message numero tres!
[22:59:48] This is random message numero dos!
..

And it goes on-and-on, the integers are still repeating.


Re: if-goto - Kar - 17.01.2012

pawn Код:
reselect:
if(last_RandomMessage == RandomSelection) {

    //Oh no! The random selected the same message as before! This is where we make it select again and again until it gets a different message!
    //To do this, we just simply create the random again!
    RandomSelection = random(3);
    printf("test");
    if(last_RandomMessage == RandomSelection) goto reselect;
    last_RandomMessage = RandomSelection;
    printf("test passed");
    //We do not return here, because we would like to let the rest of the code run.
}
make sure you don't cheat the variables anywhere....


Re: if-goto - cessil - 17.01.2012

without seeing the whole thing I can't say what you're trying to do but this is my input on this.
pawn Код:
if(last_RandomMessage == RandomSelection)
    {  
        //last_RandomMessage = RandomSelection; - don't need this because it already equals it, we checked in the above if statement
        RandomSelection = random(3);
        if(last_RandomMessage == RandomSelection)
        {
            if(!last_RandomMessage) RandomMessage++;//if last_RandomMessage == 0, ++ to it, so 0 would become 1.
            else RandomMessage--;//else minus so 2 = 1, 1 = 0;
        }
        //We do not return here, because we would like to let the rest of the code run.
    }



Re: if-goto - Scenario - 17.01.2012

Quote:
Originally Posted by ******
Посмотреть сообщение
I.e. use this:

pawn Код:
do
{
    // This way you only need "random" once not twice.
    //To do this, we just simply create the random again!
    RandomSelection = random(3);
}
while (last_RandomMessage == RandomSelection);
last_RandomMessage = RandomSelection;
Could you explain that a little, ******?


Re: if-goto - Babul - 17.01.2012

pawn Код:
last_RandomMessage = RandomSelection;
    if(last_RandomMessage == RandomSelection)
this was your fault. you have set a=b; then checked if a==b, which is always true since in the line above sets a=b.
are you experimenting with loops? if you want a sober solution for a "non repeating message", then have a look at my commercials:
pawn Код:
new CommercialOld;
new Commercials[][]={
    ">>> advertisement [0]",
    ">>> advertisement [1]",
    ">>> advertisement [2]"
};

forward CommercialMsg();public CommercialMsg(){
    new Rnd=(1+CommercialOld+(random(sizeof(Commercials)-1)))%sizeof(Commercials);
    SendClientMessageToAll(MSGCOMM_COLOR,Commercials[Rnd]);
    CommercialOld=Rnd;
    return 1;
}

//OnGameModeInit():
SetTimer("CommercialMsg",180000,true);
lets insert your last shown message (CommercialOld here, lets assume 2 for now), and the sizeof your array (3 atm):
Код:
(1+CommercialOld+(random(sizeof(Commercials)-1)))%sizeof(Commercials)
(1+                   2+(random(                          3-1)))%                          3
(                     3+(random(                            2)))%                          3
(                     3+(                                    0))%                          3=0
(                     3+(                                    1))%                          3=1
if you have a circle (your array), wich starts at 0 after its max size (012 012 0), then you see why the modulo operator pwns sometimes