Re: Useful Functions -
Slice - 09.07.2012
Thuper-duper-fast memset!
It modifies the FILL instruction's parameter on-the-fly. Linux compatible.
Demo:
pawn Код:
new test[20] = {4, ...};
memset(test, sizeof(test), 1234);
for (new i = 0; i < sizeof(test); i++) {
printf("test[%d] = %d", i, test[i]);
}
pawn Код:
stock memset(variable[], cells, value) {
new address;
#emit LOAD.S.pri variable
#emit STOR.S.pri address
raw_memset(address, cells, value);
}
stock raw_memset(address, cells, value) {
new param_adr;
#emit LCTRL 6
#emit MOVE.alt
#emit LCTRL 0
#emit ADD
#emit MOVE.alt
#emit LCTRL 1
#emit SUB.alt
#emit ADD.C 92
#emit STOR.S.pri param_adr
#emit LOAD.S.pri cells
#emit SHL.C.pri 2
#emit SREF.S.pri param_adr
#emit LOAD.S.alt address
#emit LOAD.S.pri value
#emit FILL 0
}
Re: Useful Functions -
RyDeR` - 09.07.2012
That's amazing! Also worked on this a while ago to do it without loops, but I failed. Good job.
Re: Useful Functions -
FireCat - 09.07.2012
@Ryder or ****** or anyone who knows >.<
Is there anyway to "select" all the string, till a certain character?
For example:
"Hello, my name is firecat."
Is want to copy all the string from "Hello" till "firecat" without the .
Is there any function for that?
Re: Useful Functions -
RyDeR` - 01.08.2012
What about negative numbers, that would mess it completely up. Also why converting first and then check if the money is less then thousand?
http://forum.sa-mp.com/showthread.ph...95#post1938895
Re: Useful Functions -
[KHK]Khalid - 01.08.2012
pawn Код:
islower(character)
{
if(character >= 'A' && character <= 'Z')
return 0;
return 1;
}
// returns 0 if character is in uppercase, else returns 1
// Example:
new c[4] = "Abc";
printf("%d", islower(c[0]));
// Output: 0
pawn Код:
isupper(character)
{
if(character >= 'a' && character <= 'z')
return 0;
return 1;
}
// returns 0 if character is in lowercase, else returns 1
// Example:
new c[4] = "Abc";
printf("%d", islupper(c[0]));
// Output: 1
Re: Useful Functions -
Universal - 01.08.2012
Quote:
Originally Posted by [HLF]Southclaw
That's a handy function, however the problem is it would only put them in a line on the X axis, that is East to West. Sure if you wanted North to South you could change it to the Y axis but what if you wanted it at a 47.6 degree angle?
To do that, you use the trigonometric functions Sine and Cosine (Or in Pawn: floatsin and floatcos)
You simply add the sine of the angle to the X, and the cosine of the angle to the Y, like this:
(Don't forget to use 'degrees' in the functions!)
pawn Код:
SetPlayerPos(playerid, x + floatsin(angle, degrees), y + floatcos(angle, degrees), z);
We're not done yet though, you can probably work out that this will just put all the players in one spot as we haven't used 'count' or 'distance'.
Well, fortunately, if you multiply the Sine and Cosine functions by a distance, the "projected" position (Projected from the origin point) will be that distance from the origin. You can simply use your "count * distance" equasion:
pawn Код:
SetPlayerPos(playerid, x + ((count * p2pdistance) * floatsin(angle, degrees)), y + ((count * p2pdistance) * floatcos(angle, degrees)), z);
And you will need to add the 'angle' parameter to the function header, the final result is:
pawn Код:
TeleportAllToALine(Float: x, Float: y, Float: z, Float: p2pdistance, Float:angle, interior = 0, virtualworld = 0) { new count = 0;
for(new playerid = 0; playerid < MAX_PLAYERS; playerid ++) { if(IsPlayerConnected(playerid)) { SetPlayerPos(playerid, x + ((count * p2pdistance) * floatsin(angle, degrees)), y + ((count * p2pdistance) * floatcos(angle, degrees)), z); SetPlayerFacingAngle(playerid, angle+90.0); // Set the players to face forwards, can't remember if it's + or - ... SetPlayerInterior(playerid, interior); SetPlayerVirtualWorld(playerid, virtualworld); count ++; } } }
I haven't tested this, but I hope you get the idea! 
You could also add a player angle parameter that changes the direction of the players, in case you want them to all face back or forward or at each other's backs!
|
Great addition, Southclaw!
Re: Useful Functions -
Vince - 02.08.2012
Quote:
Originally Posted by FireCat
If you're storing the value in "output" don't you mean
|
No, I don't.
Код:
Error 067: variable cannot be both a reference and an array (variable name)
A function argument may be denoted as a “reference” or as an array, but not as both.
Re: Useful Functions -
Slice - 02.08.2012
Arrays are always passed by reference.
Re: Useful Functions -
Slice - 02.08.2012
Yeah, it will.
Re: Useful Functions -
The__ - 02.08.2012
Quote:
Originally Posted by [HLF]Southclaw
Well I never knew that...
pawn Код:
main() { new str[32] = {"hello"}; func(str); print(str); }
func(arr[]) { arr[0] = 'j'; }
Will that code print "jello" then?
|
How those two are even connected ?
Re: Useful Functions -
playbox12 - 04.08.2012
Quote:
Originally Posted by The__
How those two are even connected ?
|
He sends the string to func as a parameter.
Re: Useful Functions -
MeDaKewlDude - 06.08.2012
Quote:
Originally Posted by Slice
Arrays are always passed by reference.
|
yes, unless the array is constant:
pawn Код:
main()
{
new str[32] = {"hello"};
func(str);
print(str);
}
func(const arr[])//<--here
{
arr[0] = 'j';
}
that would still print "hello".
Re: Useful Functions -
Slice - 06.08.2012
Quote:
Originally Posted by MeDaKewlDude
yes, unless the array is constant:
pawn Код:
main() { new str[32] = {"hello"}; func(str); print(str); }
func(const arr[])//<--here { arr[0] = 'j'; }
that would still print "hello".
|
No, that wouldn't compile.
Re: Useful Functions -
ddnbb - 10.09.2012
Quote:
Originally Posted by System64
ryder already made better function for that
pawn Код:
stock RemovePlayerWeapon(playerid, ...) { new iArgs = numargs(); while(--iArgs) { SetPlayerAmmo(playerid, getarg(iArgs), 0); } }
|
Is this working just like that?
Re: Useful Functions -
ev0lution - 15.10.2012
Quote:
Originally Posted by Emmet_
I don't know lol, I just started getting addicted to putting an i in front of my variables, followed by a capital letter after the i.
|
For those who style variables like that, the lowercase first letter generally refers to the type of variable - 'i' for integer in this case. If that's what you were trying to get at, your variable is oddly named "Ter". Otherwise I see you were trying to use a shortened version of 'iterator' and just had a seizure with the capitalisation.
Either way, the simple name
i is usually best for loops.
Re: Useful Functions -
Kreatyve - 15.10.2012
ev0lution$YOLO$
Your name and your signature... oh god why.
Re: Useful Functions - Emmet_ - 15.10.2012
Two functions I've made some time ago, they work perfectly:
pawn Code:
native IsValidVehicle(vehicleid); // YOU NEED TO PUT THIS IN YOUR SCRIPT!
stock RandCar()
{
new rand = random(MAX_VEHICLES);
while (!IsValidVehicle(rand)) rand = random(MAX_VEHICLES);
return rand;
}
stock RandPlayer()
{
new rand = random(MAX_PLAYERS);
while (!IsPlayerConnected(rand)) rand = random(MAX_PLAYERS);
return rand;
}
Re: Useful Functions -
Kreatyve - 15.10.2012
Emmet for this function
pawn Code:
stock Float:getdistance(Float:x1, Float:y1, Float:z1, Float:x2, Float:y2, Float:z2)
{
new Float:distance[3];
distance[0] = (x1 < x2) ? x1 - x2 : x2 - x1;
distance[1] = (y1 < y2) ? y1 - y2 : y2 - y1;
distance[2] = (z1 < z2) ? z1 - z2 : z2 - z1;
return floatsqroot(floatpower(distance[0], 2.0) + floatpower(distance[1], 2.0) + floatpower(distance[2], 2.0));
}
You could use the absolute power function...
Like this:
pawn Code:
Distance = floatsqroot(floatpower(floatabs(floatsub(X1, X2)), 2.0) + floatpower(floatabs(floatsub(Y1, Y2)), 2.0) + floatpower(floatabs(floatsub(Z1, Z2)), 2.0));
Re: Useful Functions -
Stylock - 15.10.2012
Hey, dudes, multiplying two negatives results in a positive value. The simplest way would be:
Code:
floatsqroot((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + (z1 - z2) * (z1 - z2));
Re: Useful Functions -
Kreatyve - 15.10.2012
Quote:
Originally Posted by YJIET
Hey, dudes, multiplying two negatives results in a positive value. The simplest way would be:
Code:
floatsqroot((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + (z1 - z2) * (z1 - z2));
|
What if one turns out to be a positive value and the other a negative?
The absolute value is fool-proof.