Re: Useful Functions -
RyDeR` - 18.09.2010
Info
This function converts a number to a string seperated with commas.
Function
pawn Код:
stock FormatNumber(iNum, const szChar[] = ",")
{
new
szStr[16]
;
format(szStr, sizeof(szStr), "%d", iNum);
for(new iLen = strlen(szStr) - 3; iLen > 0; iLen -= 3)
{
strins(szStr, szChar, iLen);
}
return szStr;
}
Example
pawn Код:
printf("%s", FormatNumber(50000000));
will become:
Note
Please note that you can't go over value 2147483647 because that's the integer limit; I also have no idea how to increase. An altirnative solution is to use the value as a string.
Re: Useful Functions -
GaGlets(R) - 18.09.2010
Can someone make a little hash or replacement code. For example i want it for password..
if there is any "a" then use "c" etc.. .
Password: amana - would be - cmcnc
Im too lazy to figure it out by myself
Re: Useful Functions - [03]Garsino - 18.09.2010
Mind showing me 1 of the object loading lines in the file?
Re: Useful Functions - [03]Garsino - 18.09.2010
shouldn't it be sscanf(line, "'('p<,>dffffff", modelid, px, py, pz, rx, ry, rz
');'", modelid, px, py, pz, rx, ry, rz);
Re: Useful Functions -
Kyosaur - 18.09.2010
Quote:
Originally Posted by [HLF]Southclaw
Yeah I did that, still not loading properly
pawn Код:
sscanf(line, "'('p<,>dffffff');'", modelid, px, py, pz, rx, ry, rz);
Edit: Working now, I removed the brackets now, it's just the data.
I have to do search and replace and remove the function name and brackets but at least it works now
pretty much the same as the original code
pawn Код:
stock LoadDynamicObjectsFromFile(filename[]) { new File:file_ptr, line[300], modelid, Float:px, Float:py, Float:pz, Float:rx, Float:ry, Float:rz; file_ptr=fopen(filename, io_read); if(!file_ptr)return printf("ERROR! File %s Doesn't Exist", filename); while(fread(file_ptr, line)>0) { sscanf(line, "p<,>dffffff", modelid, px, py, pz, rx, ry, rz); CreateDynamicObject(modelid, px, py, pz, rx, ry, rz);
printf("%d, %f, %f, %f, %f, %f, %f", modelid, px, py, pz, rx, ry, rz); printf("%s", line); } return 1; }
|
Using sscanf without checking for errors is a really bad idea. You should shove it in an if statement, that way your not just assuming that everything parsed correctly.
Re: Useful Functions - [03]Garsino - 18.09.2010
pawn Код:
if(!sscanf(line, "p<,>dffffff", modelid, px, py, pz, rx, ry, rz))
{
// Creating object etc
}
else // error
Re: Useful Functions -
smeti - 19.09.2010
Quote:
Originally Posted by RyDeR`
Info
This function converts a number to a string seperated with commas.
Function
pawn Код:
stock convertNumber(value) { new string[24] ; format(string, sizeof(string), "%d", value); for(new i = (strlen(string) - 3); i > (value < 0 ? 1 : 0) ; i -= 3) { strins(string[i], ",", 0); } return string; }
Example
pawn Код:
printf("%s", convertNumber(50000000));
will become:
Note
Please note that you can't go over value 2147483647 because that's the integer limit; I also have no idea how to increase. An altirnative solution is to use the value as a string.
|
Nice.
Try:
http://forum.sa-mp.com/showpost.php?...&postcount=966
Re: Useful Functions -
RyDeR` - 19.09.2010
@smeti:
Ah, never tried in the negative way.. But no problem, it's fixable.
I will fix as quick as possible.
EDIT: Fixed. You can try again if you wish. Also can you please delete the quote? Because the wrong code is in there.
pawn Код:
stock convertNumber(value)
{
new
string[24]
;
format(string, sizeof(string), "%d", value);
for(new i = (strlen(string) - 3); i > (value < 0 ? 1 : 0) ; i -= 3)
{
strins(string[i], ",", 0);
}
return string;
}
https://sampforum.blast.hk/showthread.php?tid=38965&p=843781
Re: Useful Functions - [03]Garsino - 19.09.2010
Nice function!
it's spelled negative btw
Re: Useful Functions -
RyDeR` - 19.09.2010
I use these ones:
stringToBool:
pawn Код:
stock stringToBool(boolean[])
{
new
bools[2][7] =
{
"false",
"true"
}
;
if(!strcmp(boolean, bools[1], true)) return true;
return false;
}
boolToString:
pawn Код:
stock boolToString(boolean)
{
new
bools[2][7] =
{
"false",
"true"
}
;
return bools[boolean];
}
Re: Useful Functions -
Luka P. - 19.09.2010
date() function - returns a current date in the following format: "day of month, year".. for today it would be "19th of September, 2010"...
Here you go:
http://pastebin.com/SWLR7HuK
Re: Useful Functions -
RyDeR` - 19.09.2010
Info
Reverses a string.
Code
pawn Код:
stock reverseString(input[])
{
new
string[128]
;
for(new x = (strlen(input) - 1), i = x; i > -1; i--)
{
string[x - i] = input[i];
}
return string;
}
Example
pawn Код:
printf("%s", reverseString("Testing my reverseString function!"));
will return:
pawn Код:
!noitcnuf gnirtSesrever ym gnitseT
Re: Useful Functions -
LarzI - 19.09.2010
Nice one!
Re: Useful Functions -
smeti - 19.09.2010
Quote:
Originally Posted by Luka P.
date() function - returns a current date in the following format: "day of month, year".. for today it would be "19th of September, 2010"...
Here you go: http://pastebin.com/SWLR7HuK
|
@Luka P.
pawn Код:
stock
date()
{
new
_month[][] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" },
_day[][] = { "st", "nd", "rd", "th" },
day,
month,
year,
output[24];
getdate(year, month, day);
format(output, sizeof output, "%d%s of %s, %i", day, (day > 3)?(_day[3]):(_day[day-1]), _month[month-1], year);
return output;
}
Re: Useful Functions -
SlashPT - 20.09.2010
ohh so it divides by the lowest number and the remainder its what gives?
ps added you on msn if you wanna talk... :P
ps 2: ITS MY 1000 POST HEHE
Re: Useful Functions -
RyDeR` - 20.09.2010
Alright.
Here's a better example (this will make you understand it better):
(10 % 5)
Code:
10 |_5___
-10 | 2
___ |
0 |
Do you still remember this from the very beginning of school?
Let's say that example in words:
How many times can 5 join 10? 2 times. So 2.5 = 10 ; (also we write this (2) on the right side under the divider). Then you need to substract it so 10 -10 = 0. The remainder is 0.
Here's another example:
(4 % 3)
Code:
4 |_3___
-3 | 1
___ |
1 |
In words:
How many times can 3 join 4? 1 time. So 1.3 = 3 ; (don't forgot to write this (1) on the right side under the divider). Then you need to substract from it so 4 - 3 = 1. The remainder is 1.
Re: Useful Functions -
SlashPT - 20.09.2010
yea what i said thanks buddy and keep the good work...
Re: Useful Functions -
RyDeR` - 20.09.2010
Quote:
Originally Posted by DarK TeaM PT
yea what i said thanks buddy and keep the good work...
|
No problem, and thanks ^^
Re: Useful Functions -
SlashPT - 20.09.2010
hehe y_less this i already expected... :P but thanks for the explanation anyways
Ryder see this one based on SetSpeed i did a GetSpeed
pawn Code:
new
Float:poskm[3]
;
cmd(speed,playerid,params[])
{
new
string[35]
;
format(string, sizeof string, "Speed: KMH %d MPH %d",GetSpeed(playerid, 1),GetSpeed(playerid, 0));
SendClientMessage(playerid, 0xF60000AA, string);
return 1;
}
stock
GetSpeed(playerid, mode = 1)
{
if(IsPlayerInAnyVehicle(playerid))
{
GetVehicleVelocity(GetPlayerVehicleID(playerid), poskm[0], poskm[1], poskm[2]);
return floatround(((!mode) ? floatmul(floatsqroot(floatadd(floatadd(floatpower(poskm[0], 2), floatpower(poskm[1], 2)), floatpower(poskm[2], 2))), 105.0) : floatmul(floatsqroot(floatadd(floatadd(floatpower(poskm[0], 2), floatpower(poskm[1], 2)), floatpower(poskm[2], 2))), 170.0)) * 1);
}
else
return false;
}
Tested and worked if you want theres all you need to test :P
Mode 1 = KMH
Mode 0 = MPH
EDIT : SOmethings confused with my GM xD
Re: Useful Functions -
RyDeR` - 20.09.2010
@Y_Less:
I'm sorry - I'm not good in explain as you are.
Maybe it wasn't clear but enough to understand what the '%' operator does.