Today I learned - Share your newly found knowledge!

Quote:
Originally Posted by GaByM
Посмотреть сообщение
TIL this:
This isn't quite good because sizeof() doesn't work on this strings but it is still interesting.
sizeof() counts characters and stops when it reaches the null terminator (0, '\0', EOS)
Reply

Quote:
Originally Posted by Eoussama
Посмотреть сообщение
sizeof() counts characters and stops when it reaches the null terminator (0, '\0', EOS)
No, strlen returns the amount of characters inside a string and stops at '\0'. sizeof() returns the size of the array, so
Код:
str[128] = "Hello"; printf("%i", sizeof(str)); //will print 128
Reply

You should never append to (or even edit) string literals. You will go out of bounds and most likely rewrite other literals.
Reply

Quote:
Originally Posted by GaByM
Посмотреть сообщение
TIL this:

You now can use strcat with a constant string (idk if i'm correct for this name) passed as parameter.

A better example:

This isn't quite good because sizeof() doesn't work on this strings but it is still interesting.
It can be easily bypassed.


PHP код:
FunctionGetActorTextLabel(text[], length sizeof(text))
{
    
strcat("YEA ->"textlength);
    print(
text);

Reply

TIL you can ignore parameters passed by reference (out parameters) by using a default value, just like you can with parameters passed by value (in parameters):

Код:
stock Float:DoSomething(playerid, &Float:a = 4.0) {
	a = 5.0;
	return 9.0;
}
The default value can be anything, since it will be ignored anyway.

Use:
Код:
DoSomething(0); //Returns 9.0

new Float:a;
DoSomething(0, a); //Returns 9.0 and variable 'a' contains 5.0
Reply

Quote:
Originally Posted by Freaksken
Посмотреть сообщение
TIL you can ignore parameters passed by reference (out parameters) by using a default value, just like you can with parameters passed by value (in parameters):

Код:
stock Float:DoSomething(playerid, &Float:a = 4.0) {
	a = 5.0;
	return 9.0;
}
The default value can be anything, since it will be ignored anyway.

Use:
Код:
DoSomething(0); //Returns 9.0

new Float:a;
DoSomething(0, a); //Returns 9.0 and variable 'a' contains 5.0
Great find!

Would expect a tag mismatch with this, but this is great.
Reply

today i learned that there is not necessary to use
PHP код:
stock 
in your gamemode.

for example,instead of
PHP код:
stock GetTruckNeedTracks(playerid)
{
    new 
truckneedtracks;
    new 
truckskill GetPlayerTruckSkill(playerid);
    if(
truckskill == 1truckneedtracks 35;
    else if(
truckskill == 2truckneedtracks 100;
    else if(
truckskill == 3truckneedtracks 220;
    else if(
truckskill == 4truckneedtracks 450;
    return 
truckneedtracks;

you can simply write
PHP код:
GetTruckNeedTracks(playerid)
{
    new 
truckneedtracks;
    new 
truckskill GetPlayerTruckSkill(playerid);
    if(
truckskill == 1truckneedtracks 35;
    else if(
truckskill == 2truckneedtracks 100;
    else if(
truckskill == 3truckneedtracks 220;
    else if(
truckskill == 4truckneedtracks 450;
    return 
truckneedtracks;

Reply

Quote:
Originally Posted by PepsiCola23
Посмотреть сообщение
...
you can simply write
PHP код:
GetTruckNeedTracks(playerid)
{
    new 
truckneedtracks;
    new 
truckskill GetPlayerTruckSkill(playerid);
    if(
truckskill == 1truckneedtracks 35;
    else if(
truckskill == 2truckneedtracks 100;
    else if(
truckskill == 3truckneedtracks 220;
    else if(
truckskill == 4truckneedtracks 450;
    return 
truckneedtracks;

There is quick tutorial to explain when you can use the keyword stock: http://forum.sa-mp.com/showpost.php?...16&postcount=1
Now, you can learn something new. It's called "switch case" statement (« tutorial here!).
PHP код:
GetTruckNeedTracks(playerid)
{
    new 
truckneedtracks;
    new 
truckskill GetPlayerTruckSkill(playerid);
    if(
truckskill == 1truckneedtracks 35;
    else if(
truckskill == 2truckneedtracks 100;
    else if(
truckskill == 3truckneedtracks 220;
    else if(
truckskill == 4truckneedtracks 450;
    return 
truckneedtracks;

Become (with some tiny optimisations)
PHP код:
GetTruckNeedTracks(playerid)
{
    switch(
GetPlayerTruckSkill(playerid))
    {
        case 
1: return 35;
        case 
2: return 100;
        case 
3: return 220;
        case 
4: return 450
    }
    return 
0;

Reply

Quote:
Originally Posted by Dayrion
Посмотреть сообщение
There is quick tutorial to explain when you can use the keyword stock: http://forum.sa-mp.com/showpost.php?...16&postcount=1
Now, you can learn something new. It's called "switch case" statement (« tutorial here!).
PHP код:
GetTruckNeedTracks(playerid)
{
    new 
truckneedtracks;
    new 
truckskill GetPlayerTruckSkill(playerid);
    if(
truckskill == 1truckneedtracks 35;
    else if(
truckskill == 2truckneedtracks 100;
    else if(
truckskill == 3truckneedtracks 220;
    else if(
truckskill == 4truckneedtracks 450;
    return 
truckneedtracks;

Become (with some tiny optimisations)
PHP код:
GetTruckNeedTracks(playerid)
{
    switch(
GetPlayerTruckSkill(playerid))
    {
        case 
1: return 35;
        case 
2: return 100;
        case 
3: return 220;
        case 
4: return 450
    }
    return 
0;

Thanks you,i ll try using this method
Reply

Today I learned about a simple BATCH instruction that serves as a cool hack when testing/debugging.

1- First, open any text editor, and paste the following instructions
PHP код:
@echo off
:s
samp
-server.exe
goto 
2- Save the file as “something.bat
3- Run the .bat file

Now, whenever you change your code source and compile, no need to exit and re-execute your samp-server.exe, just type the exit command in the console, and it will automatically close and re-open it for you.
Reply

Quote:
Originally Posted by Eoussama
Посмотреть сообщение
Today I learned about a simple BATCH instruction that serves as a cool hack when testing/debugging.

1- First, open any text editor, and paste the following instructions
PHP код:
@echo off
:s
samp
-server.exe
goto 
2- Save the file as “something.bat
3- Run the .bat file

Now, whenever you change your code source and compile, no need to exit and re-execute your samp-server.exe, just type the exit command in the console, and it will automatically close and re-open it for you.
can't you write gmx on the console?
Reply

Quote:
Originally Posted by rfr
Посмотреть сообщение
can't you write gmx on the console?
That restarts the gamemode, not the entire server.
Reply

today i learned.... what means strtok

basicly strtok means

str - is a string == 1
tok - is a token == 2

strtok(const string[], &index)

1. its porpuse is to fetch textual declaration like the callback know as 'OnPlayerCommandText' with the parameters (playerid, cmdtext[])

2. tokes porpuse is to check indexe's or the given parameter of 'playerid'

here on the given example will show you how its done

example code:

Код:
public OnPlayerCommandText(playerid, cmdtext[])
{
        new cmd[256+1], index;

        cmd = strtok(cmdtext, index);

        if(strcmp(cmd, "/info", true : false, 5)) < bool either its true or false
        {
                return true, false; < either it returns true or false
        }
        return false;
}
Reply

TIL that you can split a macro on multiple lines with '\'

PHP код:
#define add(%0,%1) ((%0)+(%1))
main()
{
    
printf("%i",
        
        
add(\
            
5\
            ,\
            
6\
            )
        );

Reply

Quote:
Originally Posted by GaByM
Посмотреть сообщение
TIL that you can split a macro on multiple lines with '\'

PHP код:
#define add(%0,%1) ((%0)+(%1))
main()
{
    
printf("%i",
        
        
add(\
            
5\
            ,\
            
6\
            )
        );

not only macros but literally anything, as far as I know, correct me if I'm wrong!
Reply

TIL you can specify a variable width or precision by using an asterisk.

Код:
Width:
printf("%5d", 10);			//prints '   10'
printf("%*d", 5, 10);			//prints '   10'

printf("%5f", 3.1415926535);		//prints '    3.141592'
printf("%*f", 5, 3.1415926535);		//prints '    3.141592'

Precision:
printf("%.5f", 3.1415926535);		//prints '3.14159'
printf("%.*f", 5, 3.1415926535);	//prints '3.14159'

printf("%.5s", "abcdefghi");		//prints 'abcde'
printf("%.*s", 5, "abcdefghi");		//prints 'abcde'
This is useful in situations where you don't want to hardcode those values.
Reply

YIL the internal buffer for formatting floating point numbers is 64 characters:

PHP код:
    new str[256];
    
format(strsizeof (str), "%7.0100f"4.4);
    print(
str);
    
printf("%7.0100f"4.4); 
Should give numbers with 100 decimal places (mostly all zeros), but doesn't:

Код:
      4.40000009536743164062500000000000000000000000000000000000
      4.40000009536743164062500000000000000000000000000000000000
Edit:

I'll also expand on what Freaksken said (I didn't learn this today, I wrote the original code for it). You can combine variable width and precision, plus zero prefixes:

PHP код:
printf("%0*.*f"354.4); // Gives "004.40000" 
Even better, this can totally replace "substring" code:

PHP код:
new str[] = "How are you?"
printf("%.3s"str[4]); // Gives "are" 
Reply

Quote:
Originally Posted by Y_Less
Посмотреть сообщение
YIL the internal buffer for formatting floating point numbers is 64 characters:

PHP код:
    new str[256];
    
format(strsizeof (str), "%7.0100f"4.4);
    print(
str);
    
printf("%7.0100f"4.4); 
Should give numbers with 100 decimal places (mostly all zeros), but doesn't:

Код:
      4.40000009536743164062500000000000000000000000000000000000
      4.40000009536743164062500000000000000000000000000000000000
Edit:

I'll also expand on what Freaksken said (I didn't learn this today, I wrote the original code for it). You can combine variable width and precision, plus zero prefixes:

PHP код:
printf("%0*.*f"354.4); // Gives "004.40000" 
Even better, this can totally replace "substring" code:

PHP код:
new str[] = "How are you?"
printf("%.3s"str[4]); // Gives "are" 
The latest example does work with sprintf in C?
Reply

I have no idea sorry. As I said, the "*" functionality is something I added to SA:MP's format when I was a dev. Test it and make your own TIL post!
Reply

Today I learnt that:
PHP код:
new array[][] = {12,13};
printf("%i", array); 
will output 8.
Reply


Forum Jump:


Users browsing this thread: 12 Guest(s)