SA-MP Forums Archive
Today I learned - Share your newly found knowledge! - 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: Today I learned - Share your newly found knowledge! (/showthread.php?tid=359953)

Pages: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18


Re: Today I learned - Share your newly found knowledge! - GRiMMREAPER - 22.06.2018

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.




Re: Today I learned - Share your newly found knowledge! - Logic_ - 24.06.2018

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.


Re: Today I learned - Share your newly found knowledge! - GRiMMREAPER - 26.06.2018

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.


Re: Today I learned - Share your newly found knowledge! - coool - 10.07.2018

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


Re: Today I learned - Share your newly found knowledge! - Y_Less - 11.07.2018

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


Re: Today I learned - Share your newly found knowledge! - coool - 11.07.2018

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!


Re: Today I learned - Share your newly found knowledge! - coool - 19.07.2018

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.


Re: Today I learned - Share your newly found knowledge! - xMoBi - 19.07.2018

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.");
}



Re: Today I learned - Share your newly found knowledge! - SyS - 19.07.2018

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.


Re: Today I learned - Share your newly found knowledge! - Y_Less - 19.07.2018

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.


Re: Today I learned - Share your newly found knowledge! - coool - 19.07.2018

Thank you for correcting me..


Re: Today I learned - Share your newly found knowledge! - GRiMMREAPER - 26.07.2018

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



Re: Today I learned - Share your newly found knowledge! - Spmn - 26.07.2018

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]?


Re: Today I learned - Share your newly found knowledge! - Y_Less - 01.08.2018

0 || 0 returns 0


Re: Today I learned - Share your newly found knowledge! - Wreeper - 11.08.2018

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.



Re: Today I learned - Share your newly found knowledge! - iKarim - 11.08.2018

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


Re: Today I learned - Share your newly found knowledge! - GRiMMREAPER - 11.08.2018

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.


Re: Today I learned - Share your newly found knowledge! - Freaksken - 11.08.2018

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.


Re: Today I learned - Share your newly found knowledge! - GRiMMREAPER - 11.08.2018

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.


Re: Today I learned - Share your newly found knowledge! - faxxe - 18.08.2018

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.