open.mp forum
[Pawn] Post your code snippets here - Printable Version

+ open.mp forum (https://forum.open.mp)
-- Forum: SA-MP (https://forum.open.mp/forumdisplay.php?fid=3)
--- Forum: Pawn Scripting (https://forum.open.mp/forumdisplay.php?fid=10)
--- Thread: [Pawn] Post your code snippets here (/showthread.php?tid=218)



Post your code snippets here - DTV - 2019-04-14

I remember seeing a thread like this on SA-MP forums so it'd be nice to share with each other little tricks to making our lives easier with pawn, I'll start:



Actually don't know what this is formally called, but you can use it to skip parts of code when needed (good in a situation where the ending part of code is always the same but you don't need to copy/paste that code in every if/switch statement)

Code:
someFunc(params) {



? ? if(params) {



? ? ? ? SendClientMessage(playerid,-1,"Not skipped!");

? ? ? ? return 1;

? ? }



? ? else { goto skipBlock; } //this is how you'd call for the code to skip to `skipBlock`



? ? skipBlock: //this is how you'd create it



? ? SendClientMessage(playerid,-1,"Skipped!");

? ? return 1;

}



RE: Post your code snippets here - Gravityfalls - 2019-04-15

I believe I've read somewhere that go-to statements (loops?) are actually slower. Not sure but I'll let others comment about that.


RE: Post your code snippets here - iSpark - 2019-04-15

I'd rather use function than a go-to


RE: Post your code snippets here - DTV - 2019-04-15

I didn't post it to say "this is faster than blah blah", its there because others might not know about it and maybe could get a use out of it.


RE: Post your code snippets here - BlackBank - 2019-04-15

I prefer more a while loop, then the go-to way. Since you can also achieve this in a while loop.


RE: Post your code snippets here - Codeah - 2019-04-15

Code:
new vehicleComponentPrices[] =

{

    400, 550, 200, 250, 100, 150, 80, 500, 500, 200, 1000, 220, 250, 100, 400, 500,

    200, 500, 350, 300, 250, 200, 150, 350, 50, 1000, 480, 480, 770, 680, 370, 370,

    170, 120, 790, 150, 500, 690, 190, 390, 500, 390, 1000, 500, 500, 510, 710, 670,

    530, 810, 620, 670, 530, 130, 210, 230, 520, 430, 620, 720, 530, 180, 550, 430,

    830, 850, 750, 250, 200, 550, 450, 550, 450, 1100, 1030, 980, 1560, 1620, 1200,

    1030, 900, 1230, 820, 1560, 1350, 770, 100, 1500, 150, 650, 450, 100, 750, 350,

    450, 350, 1000, 620, 1140, 1000, 940, 780, 830, 3250, 1610, 1540, 780, 780, 780,

    1610, 1540, 0, 0, 3340, 3250, 2130, 2050, 2040, 780, 940, 780, 940, 780, 860, 780,

    1120, 3340, 3250, 3340, 1650, 3380, 3290, 1590, 830, 800, 1500, 1000, 800, 580, 470,

    870, 980, 150, 150, 100, 100, 490, 600, 890, 1000, 1090, 840, 910, 1200, 1030, 1030,

    920, 930, 550, 1050, 1050, 950, 650, 450, 550, 850, 950, 850, 950, 970, 880, 990,

    900, 950, 1000, 900, 1000, 900, 2050, 2150, 2130, 2050, 2130, 2040, 2150, 2040, 2095,

    2175, 2080, 2200, 1200, 1040, 940, 1100

};



stock GetVehicleComponentCost(componentid)

{

    return vehicleComponentPrices[componentid - 1000];

}



I remember spending many hours manually getting all of these component prices.

https://gist.github.com/thecodeah/354a67c603bdd8eb6956aeadaa947591


RE: Post your code snippets here - Riddick - 2019-04-16

Animations data:


  • Library

  • Name

  • Index


I find that really useful to determine if player uses specific animation with GetPlayerAnimationIndex?function?(better than using string comparison for animation names).

Additionally useful for FCNPC_SetAnimation?function from FCNPC (which requires animation index).



Data:

https://pastebin.com/raw/eqrJX5KD


RE: Post your code snippets here - Y_Less - 2019-04-16

(2019-04-14, 11:04 PM)DTV Wrote: I remember seeing a thread like this on SA-MP forums so it'd be nice to share with each other little tricks to making our lives easier with pawn, I'll start:



Actually don't know what this is formally called, but you can use it to skip parts of code when needed (good in a situation where the ending part of code is always the same but you don't need to copy/paste that code in every if/switch statement)

Code:
someFunc(params) {



? ? if(params) {



? ? ? ? SendClientMessage(playerid,-1,"Not skipped!");

? ? ? ? return 1;

? ? }



? ? else { goto skipBlock; } //this is how you'd call for the code to skip to `skipBlock`



? ? skipBlock: //this is how you'd create it



? ? SendClientMessage(playerid,-1,"Skipped!");

? ? return 1;

}



I'm not quite sure why this is trying to do. It would be exactly the same without the majority of this code:



Code:
someFunc(params) {



    if(params) {



        SendClientMessage(playerid,-1,"Not skipped!");

        return 1;

    }



    SendClientMessage(playerid,-1,"Skipped!");

    return 1;

}



The "return" ends the function early, so only code that passes your first check prints "Skipped".


RE: Post your code snippets here - KJason - 2019-04-16

a function i use to return the current time like: 2019-04-16 16:13:28

PHP Code:
ReturnTime(dest[], max_len sizeof(dest))
{
? ??new 
get[6];
? ? 
getdate(get[0],get[1],get[2]);
? ? 
gettime(get[3],get[4],get[5]);
? ? 
format(destmax_len,"%d-%02d-%02d %02d:%02d:%02d",get[0],get[1],get[2],get[3],get[4],get[5]);
? ? return 
1;




RE: Post your code snippets here - kristo - 2019-04-16

Something I wrote for TommyB for getting the world positions of vehicle components. Does not take X and Y rotations into account.

PHP Code:
stock GetVehiclePartAbsolutePos(vehicleidFloat:partXFloat:partYFloat:partZ, &Float:x, &Float:y, &Float:z) {
 ? ?if (!
IsValidVehicle(vehicleid)) {
? ? ? ?return 
0;
? ?}

? ?new 
Float:a;
? ?
GetVehiclePos(vehicleidxyz);
? ?
GetVehicleZAngle(vehicleida);

? ?
partX floatcos(adegrees)  partY floatsin(-adegrees);
? ?
partX floatsin(adegrees)  paryY floatcos(-adegrees);
? ?
partZ;

? ?return 
1;
}

// boot:
GetVehicleModelInfo(GetVehicleModel(vehicleid), VEHICLE_MODEL_INFO_SIZExyz);
GetVehiclePartAbsolutePos(vehicleid0.0, -2.00.0xyz);

// bonnet:
GetVehicleModelInfo(GetVehicleModel(vehicleid), VEHICLE_MODEL_INFO_SIZExyz);
GetVehiclePartAbsolutePos(vehicleid0.02.00.0xyz);

// front right tire:
GetVehicleModelInfo(GetVehicleModel(vehicleid), VEHICLE_MODEL_INFO_WHEELSFRONTxyz);
GetVehiclePartAbsolutePos(vehicleidxyzxyz);

// front left tire:
GetVehicleModelInfo(GetVehicleModel(vehicleid), VEHICLE_MODEL_INFO_WHEELSFRONTxyz);
GetVehiclePartAbsolutePos(vehicleid, -xyzxyz); 



RE: Post your code snippets here - DTV - 2019-04-16

(2019-04-16, 11:23 AM)Y_Less Wrote:
(2019-04-14, 11:04 PM)DTV Wrote: I remember seeing a thread like this on SA-MP forums so it'd be nice to share with each other little tricks to making our lives easier with pawn, I'll start:

Actually don't know what this is formally called, but you can use it to skip parts of code when needed (good in a situation where the ending part of code is always the same but you don't need to copy/paste that code in every if/switch statement)
Code:
-snip-

I'm not quite sure why this is trying to do. ?It would be exactly the same without the majority of this code:

Code:
-snip-

The "return" ends the function early, so only code that passes your first check prints "Skipped".

I couldn't come up with a better example off the top of my head?without pulling out code that I can't show to everyone here.? The code I made wouldn't (hopefully) be a real life example of how you'd use but it was just to show "this is how you create it, this is how you use it, etc." rather than "this is a example of how you'd use it practically".


RE: Post your code snippets here - Mike - 2019-04-17

(2019-04-14, 11:04 PM)DTV Wrote: snip



https://en.wikipedia.org/wiki/Spaghetti_code


RE: Post your code snippets here - DTV - 2019-04-29

Simple snippet that will make a M4 leave an explosion wherever its last shot lands:



PHP Code:
public OnPlayerWeaponShot(playeridweaponidhittypehitidFloat:fXFloat:fYFloat:fZ)

{

? ??if(
weaponid == WEAPON_M4)

? ??{

? ? ? ??new 
Float:dummy_float,Float:fHitPosX,Float:fHitPosY,Float:fHitPosZ;

? ? ? ??
GetPlayerLastShotVectors(playerid,dummy_float,dummy_float,dummy_float,fHitPosX,fHitPosY,fHitPosZ);

? ? ? ??
CreateExplosion(fHitPosX,fHitPosY,fHitPosZ,0,10.0);

? ??}

? ? return 
1;





RE: Post your code snippets here - DTV - 2020-01-03

Simple way to do basic chance based outcomes:

PHP Code:
if(!random(2)) //50%
if(!random(4)) //25%
if(random(4)) //75%
if(!random(10)) //10%
if(random(10)) //90%
//etc 



RE: Post your code snippets here - Awide - 2020-10-18

(2020-01-03, 06:33 PM)DTV Wrote: Simple way to do basic chance based outcomes:



PHP Code:
if(!random(2)) //50%

if(!random(4)) //25%

if(random(4)) //75%

if(!random(10)) //10%

if(random(10)) //90%

//etc 



These are pretty nice!


RE: Post your code snippets here - Grate Maharlika - 2020-10-19

(2019-04-16, 11:23 AM)Y_Less Wrote:
(2019-04-14, 11:04 PM)DTV Wrote: I remember seeing a thread like this on SA-MP forums so it'd be nice to share with each other little tricks to making our lives easier with pawn, I'll start:

Actually don't know what this is formally called, but you can use it to skip parts of code when needed (good in a situation where the ending part of code is always the same but you don't need to copy/paste that code in every if/switch statement)
Code:
someFunc(params) {

? ? if(params) {

? ? ? ? SendClientMessage(playerid,-1,"Not skipped!");
? ? ? ? return 1;
? ? }

? ? else { goto skipBlock; } //this is how you'd call for the code to skip to `skipBlock`

? ? skipBlock: //this is how you'd create it

? ? SendClientMessage(playerid,-1,"Skipped!");
? ? return 1;
}

I'm not quite sure why this is trying to do.? It would be exactly the same without the majority of this code:

Code:
someFunc(params) {

? ? if(params) {

? ? ? ? SendClientMessage(playerid,-1,"Not skipped!");
? ? ? ? return 1;
? ? }

? ? SendClientMessage(playerid,-1,"Skipped!");
? ? return 1;
}

The "return" ends the function early, so only code that passes your first check prints "Skipped".

removed


RE: Post your code snippets here - Expert* - 2020-10-24

As far as i can remember use of goto is considered to be a bad practise, because it can lead to "spaghetti" code.



One useful way to use it is to get out of nested loops.



Also it's naming convention is Ex: skip_loop_block. ? ( with _? ? ?after_each_word )





I'm not sure if that is correct, i never had a need to use it or learn how to use it so...





Code:
new sometext[ 10 ][ 20 ][ 128 ];

// ...

public OnGameModeInit( playerid )

{

// ...



? ? format( sometext[ 1 ][ 2 ], 63, "Print this one" );

? ? format( sometext[ 2 ][ 4 ], 63, "Print this one too." );

? ? format( sometext[ 3 ][ 8 ], 63, "Print, print, print!" );

? ? format( sometext[ 4 ][ 16 ], 63, "End nested loop NOW!" );

? ? format( sometext[ 5 ][ 19 ], 63, "This one will not be printed!" );



for( new i; i < 10; i )

{

for( new j; j < 20; j )

{

? ? if( !isnull( sometext[ i ][ j ] ) )

? ? {

? ? if( strcmp( "End nested loop NOW!", sometext[ i ][ j ], false ) == 0 )

? ? {

? ? goto skip_loop_block; // we don't have to break; 2 loops now!

? ? }

? ? else printf( "%s", sometext[ i ][ j ] );

? ? }

}

}



printf( "This one will not be printed if we use goto!" );



skip_loop_block:



print( "Done" );



return 1;

}



RE: Post your code snippets here - Virsenas - 2021-02-26

SetPlayerFacePlayer function. Function was not created by me. I think it was created by a mad german scientist whose name I can't remember.



Code:
SetPlayerFacePlayer(playerid, faceplayerid)

{

? ? new Float:x1, Float:y1, Float: a1;

? ? GetPlayerPos(playerid, x1, y1, a1);

? ? new Float:fpX, Float:fpY, Float: fpZ;

? ? GetPlayerPos(faceplayerid, fpX, fpY, fpZ);

? ? a1 = floatabs(atan((fpY-y1)/(fpX-x1)));

? ? if(fpX <= x1 && fpY >= y1) a1 = floatsub(180, a1);

? ? else if(fpX < x1 && fpY < y1) a1 = floatadd(a1, 180);

? ? else if(fpX >= x1 && fpY <= y1) a1 = floatsub(360.0, a1);

? ? a1 = floatsub(a1, 90.0);

? ? if(a1 >= 360.0) a1 = floatsub(a1, 360.0);

? ? if(!IsPlayerInAnyVehicle(playerid)) SetPlayerFacingAngle(playerid, a1);

? ? else SetVehicleZAngle(GetPlayerVehicleID(playerid), a1);

}



RE: Post your code snippets here - Tama - 2021-10-27

IsValidRoleplayName without regex.



PHP Code:
stock IsValidRoleplayName(const name[], short_name_len 3) {

? ? new 

? ? ? ? 
len strlen(name),

? ? ? ? 
underscore_pos strfind(name"_"true);



? ? 
// The name is empty

? ? if (isnull(name)) return false;



? ? 
// Underscore not found

? ? if (underscore_pos == -1) return false;

? ? 

? ? 
// Firstname and lastname is not capital

? ? #define isupper(%0) (%0 != (%0 | 0x20))

? ? if (!isupper(name[0]) || !isupper(name[underscore_pos  1])) return false;



? ? 
// Firstname is too short

? ? if (underscore_pos short_name_len) return false;



? ? 
// Lastname is too short

? ? if (((len 1) - underscore_pos) < short_name_len) return false;



? ? 
// Invalid characters

? ? for (new i!= len) {

? ? ? ? switch (
name[i]) {

? ? ? ? ? ? case 
'A'..'Z''a'..'z''_': continue;

? ? ? ? ? ? default: {

? ? ? ? ? ? ? ? return 
false;

? ? ? ? ? ? }

? ? ? ? }

? ? }

? ? return 
true;





IsValidRoleplayName with regex (PawnPlus)



PHP Code:
stock IsValidRoleplayName(const name[])

{

? ? new 
String:tmp str_format(name);

? ? return 
str_match(tmp"^[A-Z]{1}[a-z]{2,12}_[A-Z]{1}[a-z]{2,12}$");







No Colors/Remove color format

PHP Code:
stock NoColors(str[], startPos '{'len 8) {

? ? for (new 
0<= strlen(str) - len) {

? ? ? ? if (
str[i] == startPos) {

? ? ? ? ? ? if (
str[i  len 1] == '}' || IsValidHex(str[i  len 1])) {

? ? ? ? ? ? ? ? new 

? ? ? ? ? ? ? ? ? ? 
pass;

? ? ? ? ? ? ? ? 

? ? ? ? ? ? ? ? for (new 
1len 1) {

? ? ? ? ? ? ? ? ? ? if (
IsValidHex(str[i  j])) {

? ? ? ? ? ? ? ? ? ? ? ? 
pass ;

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? if (
pass >= len 2) {

? ? ? ? ? ? ? ? ? ? 
strdel(strii  len);

? ? ? ? ? ? ? ? ? ? 
pass 0;

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? }

? ? }

? ? return 
1;





RE: Post your code snippets here - brayeksizzerr - 2024-04-08

(2019-04-14, 11:04 PM)DTV Wrote: I remember seeing a thread like this on SA-MP forums so it'd be nice to share with each other little tricks to making our lives easier with pawn, I'll start:



Actually don't know what this is formally called, but you can use it to skip parts of code when needed (good in a situation where the ending part of code is always the same but you don't need to copy/paste that code in every if/switch statement)

Code:
someFunc(params) {



? ? if(params) {



? ? ? ? SendClientMessage(playerid,-1,"Not skipped!");

? ? ? ? return 1;

? ? }



? ? else { goto skipBlock; } //this is how you'd call for the code to skip to `skipBlock`



? ? skipBlock: //this is how you'd create it



? ? SendClientMessage(playerid,-1,"Skipped!");

? ? return 1;

}