Today I learned - Share your newly found knowledge!

TIL that passing the params (1, 1) to the function below returns weird (expected?) results!

pawn Код:
RandomInt(min, max)
{
  new r = random(max - min) + min;
  return r;
}
I then went on the SA:MP wiki and saw that was documented. I was excited for a minute.

Reply

Quote:
Originally Posted by GRiMMREAPER
Посмотреть сообщение
TIL that passing the params (1, 1) to the function below returns weird (expected?) results!

pawn Код:
RandomInt(min, max)
{
  new r = random(max - min) + min;
  return r;
}
I then went on the SA:MP wiki and saw that was documented. I was excited for a minute.

Out of bounds integer value. random(0) would do the same.
Reply

Quote:
Originally Posted by Logic_
Посмотреть сообщение
Out of bounds integer value. random(0) would do the same.
Evidently, because 1-1=0. The thing with this is that I did not check the random function article on the Wikipedia page and thought I discovered something.
Reply

Today I learnt how I can cut the second for loop and strcmp in this code:
PHP код:
CMD:checkips(playeridparams[])
{
    for(new 
iMAX_PLAYERSi++)
    {
        new 
pIP[16];
        
GetPlayerIP(ipIPsizeof(pIP));
        for(new 
kMAX_PLAYERSk++)
        {
            new 
pIP2[16];
            
GetPlayerIP(kpIP2sizeof(pIP2));
            if(!
strcmp(pIPpIP2))
            
//IPs matched inform them
        
}
    }
    return 
1;

Think if you can work around and cut the 2nd 'for' loop and strcmp. note: checking IPs at OnPlayerConnect is not the solution...
Reply

Generally you write the thing you learned, not just that you did. How will other people learn if you don't explain?
Reply

Quote:
Originally Posted by coool
Посмотреть сообщение
Today I learnt how I can cut the second for loop and strcmp in this code:
PHP код:
CMD:checkips(playeridparams[])
{
    for(new 
iMAX_PLAYERSi++)
    {
        new 
pIP[16];
        
GetPlayerIP(ipIPsizeof(pIP));
        for(new 
kMAX_PLAYERSk++)
        {
            new 
pIP2[16];
            
GetPlayerIP(kpIP2sizeof(pIP2));
            if(!
strcmp(pIPpIP2))
            
//IPs matched inform them
        
}
    }
    return 
1;

Think if you can work around and cut the 2nd 'for' loop and strcmp. note: checking IPs at OnPlayerConnect is not the solution...
The above code can be worked around and written like this
PHP код:
CMD:checkips(playeridparams[])
{
    for(new 
iMAX_PLAYERSi++)
    {
        new 
pIP[16];
        
GetPlayerIP(ipIPsizeof(pIP));
        
        if(
GetSVarInt(pIP)) //IPs matched inform them.
        
SetSVarInt(pIP1);
    }

cutting down the second 'for' loop and strcmp!
Reply

TIL
This works
PHP код:
new arr[2] = {1,3};
if(
arr[|| 1])
    print(
"Yea, it works."); 
Got the above when lurking my old threads https://sampforum.blast.hk/showthread.php?tid=624112
People are saying that if(arr[1 || 2]) is invalid syntax... But today I checked it works.
Reply

Quote:
Originally Posted by coool
Посмотреть сообщение
TIL
This works
PHP код:
new arr[2] = {1,3};
if(
arr[|| 1])
    print(
"Yea, it works."); 
Got the above when lurking my old threads https://sampforum.blast.hk/showthread.php?tid=624112
People are saying that if(arr[1 || 2]) is invalid syntax... But today I checked it works.
What if it's:

Код:
new array[2] = {0, 0};
if (array[0 || 1]) {
    print("Yeah, it works.");
}
Reply

Quote:
Originally Posted by coool
Посмотреть сообщение
TIL
This works
PHP код:
new arr[2] = {1,3};
if(
arr[|| 1])
    print(
"Yea, it works."); 
Got the above when lurking my old threads https://sampforum.blast.hk/showthread.php?tid=624112
People are saying that if(arr[1 || 2]) is invalid syntax... But today I checked it works.
It is syntactically correct but it won't wont give the result what you expect to get. || is logical OR operator it returns true (1) if one of the operands is true (not 0) and returns false (0) if both operands are 0. arr[0 || 1] is evaluated to arr[1] in this case.
Reply

0||1 is 1
2||3 is 1
55||56 is 1

So yes, that is valid syntax, but I doubt it does what you think it does.
Reply

Thank you for correcting me..
Reply

Quote:
Originally Posted by coool
Посмотреть сообщение
TIL
This works
PHP код:
new arr[2] = {1,3};
if(
arr[|| 1])
    print(
"Yea, it works."); 
Got the above when lurking my old threads https://sampforum.blast.hk/showthread.php?tid=624112
People are saying that if(arr[1 || 2]) is invalid syntax... But today I checked it works.
For x || y, provided both x and y are numbers, will always return 1. You are then accessing arr[1], which indeed exists and is 3.

pawn Код:
new arr[2] = {1};

    if(arr[0 || 1]) {
        print("It works!");
    } else {
        print("It doesn't work."); // Expected output, because arr[0 || 1] returns true (1) and arr[1] doesn't exist.
    }
Reply

Quote:
Originally Posted by GRiMMREAPER
Посмотреть сообщение
For x || y, provided both x and y are numbers, will always return 1. You are then accessing arr[1], which indeed exists and is 3.

pawn Код:
new arr[2] = {1};

    if(arr[0 || 1]) {
        print("It works!");
    } else {
        print("It doesn't work."); // Expected output, because arr[0 || 1] returns true (1) and arr[1] doesn't exist.
    }
What about arr[0 || 0]?
Reply

0 || 0 returns 0
Reply

Today i learned that sscanf is not installable

By the way i learned & understanded fully HTML & CSS & JavaScript for websites today too.
You are not interested in this, but i am.
Reply

Quote:
Originally Posted by Wreeper
Посмотреть сообщение
Today i learned that sscanf is not installable

By the way i learned & understanded fully HTML & CSS & JavaScript for websites today too.
You are not interested in this, but i am.
holy fuck
Reply

Today I learned but I also almost lost my mind trying to figure what I was doing wrong before I finally realized the fuckup.

a.pwn
pawn Код:
enum {
    D_LOGIN,
    D_REGISTER
}

// Handle response with OnDialogResponse hook
b.pwn
pawn Код:
enum {
    D_CLICKEDPLAYER
}

// Handle response with OnDialogResponse hook
I was wondering why everytime I was handling a response of D_CLICKEDPLAYER, I'd get a popup with the authentication (specifically, D_LOGIN) dialog. Didn't take me ages to figure out the fuckup, but I was losing my mind over something as simple as this. The most obvious solution (and the only one I could think of) is to keep the dialogs in an unique file.
Reply

Quote:
Originally Posted by GRiMMREAPER
Посмотреть сообщение
...
You didn't really explain why it occurs, you just said that it occurs.

So for those who don't know, both D_LOGIN and D_CLICKEDPLAYER are equal to 0, which is why the code can't distinguish between the 2. There are 2 ways I can come up with to fix this.

Method 1:
Offset one enum with a given value.
Код:
enum {
    D_LOGIN, //Starts at 0
    D_REGISTER
}
enum {
    D_CLICKEDPLAYER = 2 //Starts at 2
}
This method is not futureproof when you add more elements to the first enum in the future, because you might forget to adjust the offset of the second enum.

Method 2:
Use a single enum with all dialogs in it and put the enum in a .inc file that you include in both scripts.
Reply

Quote:
Originally Posted by Freaksken
Посмотреть сообщение
You didn't really explain why it occurs, you just said that it occurs.

So for those who don't know, both D_LOGIN and D_CLICKEDPLAYER are equal to 0, which is why the code can't distinguish between the 2. There are 2 ways I can come up with to fix this.

Method 1:
Offset one enum with a given value.
Код:
enum {
    D_LOGIN, //Starts at 0
    D_REGISTER
}
enum {
    D_CLICKEDPLAYER = 2 //Starts at 2
}
This method is not futureproof when you add more elements to the first enum in the future, because you might forget to adjust the offset of the second enum.

Method 2:
Use a single enum with all dialogs in it and put the enum in a .inc file that you include in both scripts.
Well, I really should've added an explanation as to why it happened.
Reply

For huge arrays nowadays i stopped using loops to iterate. Now i use heapsort as well as binary search algorithm.
Furthermore i stopped the use of strtok and replaced it by sscanf.
Reply


Forum Jump:


Users browsing this thread: 7 Guest(s)