Re: Today I learned - Share your newly found knowledge! -
Eoussama - 23.01.2018
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)
Re: Today I learned - Share your newly found knowledge! -
GaByM - 23.01.2018
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
Re: Today I learned - Share your newly found knowledge! -
Spmn - 23.01.2018
You should never append to (or even edit) string literals. You will go out of bounds and most likely rewrite other literals.
Re: Today I learned - Share your newly found knowledge! -
Dayrion - 23.01.2018
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 ->", text, length);
print(text);
}
Re: Today I learned - Share your newly found knowledge! -
Freaksken - 06.02.2018
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
Re: Today I learned - Share your newly found knowledge! -
ISmokezU - 06.02.2018
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.
Re: Today I learned - Share your newly found knowledge! -
PepsiCola23 - 12.02.2018
today i learned that there is not necessary to use
in your gamemode.
for example,instead of
PHP код:
stock GetTruckNeedTracks(playerid)
{
new truckneedtracks;
new truckskill = GetPlayerTruckSkill(playerid);
if(truckskill == 1) truckneedtracks = 35;
else if(truckskill == 2) truckneedtracks = 100;
else if(truckskill == 3) truckneedtracks = 220;
else if(truckskill == 4) truckneedtracks = 450;
return truckneedtracks;
}
you can simply write
PHP код:
GetTruckNeedTracks(playerid)
{
new truckneedtracks;
new truckskill = GetPlayerTruckSkill(playerid);
if(truckskill == 1) truckneedtracks = 35;
else if(truckskill == 2) truckneedtracks = 100;
else if(truckskill == 3) truckneedtracks = 220;
else if(truckskill == 4) truckneedtracks = 450;
return truckneedtracks;
}
Re: Today I learned - Share your newly found knowledge! -
Dayrion - 12.02.2018
Quote:
Originally Posted by PepsiCola23
...
you can simply write
PHP код:
GetTruckNeedTracks(playerid)
{
new truckneedtracks;
new truckskill = GetPlayerTruckSkill(playerid);
if(truckskill == 1) truckneedtracks = 35;
else if(truckskill == 2) truckneedtracks = 100;
else if(truckskill == 3) truckneedtracks = 220;
else if(truckskill == 4) truckneedtracks = 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 == 1) truckneedtracks = 35;
else if(truckskill == 2) truckneedtracks = 100;
else if(truckskill == 3) truckneedtracks = 220;
else if(truckskill == 4) truckneedtracks = 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;
}
Re: Today I learned - Share your newly found knowledge! -
PepsiCola23 - 12.02.2018
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 == 1) truckneedtracks = 35;
else if(truckskill == 2) truckneedtracks = 100;
else if(truckskill == 3) truckneedtracks = 220;
else if(truckskill == 4) truckneedtracks = 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
Re: Today I learned - Share your newly found knowledge! -
Eoussama - 12.02.2018
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 s
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.
Re: Today I learned - Share your newly found knowledge! -
rfr - 18.02.2018
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 s
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?
Re: Today I learned - Share your newly found knowledge! -
Gforcez - 19.02.2018
Quote:
Originally Posted by rfr
can't you write gmx on the console?
|
That restarts the gamemode, not the entire server.
Re: Today I learned - Share your newly found knowledge! -
Mobtiesgangsa - 02.03.2018
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;
}
Re: Today I learned - Share your newly found knowledge! -
GaByM - 06.03.2018
TIL that you can split a macro on multiple lines with '\'
PHP код:
#define add(%0,%1) ((%0)+(%1))
main()
{
printf("%i",
add(\
5\
,\
6\
)
);
}
Re: Today I learned - Share your newly found knowledge! -
Eoussama - 06.03.2018
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!
Re: Today I learned - Share your newly found knowledge! -
Freaksken - 01.04.2018
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.
Re: Today I learned - Share your newly found knowledge! -
Y_Less - 02.04.2018
YIL the internal buffer for formatting floating point numbers is 64 characters:
PHP код:
new str[256];
format(str, sizeof (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", 3, 5, 4.4); // Gives "004.40000"
Even better, this can totally replace "substring" code:
PHP код:
new str[] = "How are you?"
printf("%.3s", str[4]); // Gives "are"
Re: Today I learned - Share your newly found knowledge! -
kurta999 - 02.04.2018
Quote:
Originally Posted by Y_Less
YIL the internal buffer for formatting floating point numbers is 64 characters:
PHP код:
new str[256];
format(str, sizeof (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", 3, 5, 4.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?
Re: Today I learned - Share your newly found knowledge! -
Y_Less - 02.04.2018
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!
Re: Today I learned - Share your newly found knowledge! -
coool - 22.06.2018
Today I learnt that:
PHP код:
new array[][] = {12,13};
printf("%i", array);
will output 8.