• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Pawn] Post your code snippets here
#1
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; }
Stoned Ape

  Reply
#2
I believe I've read somewhere that go-to statements (loops?) are actually slower. Not sure but I'll let others comment about that.
  Reply
#3
I'd rather use function than a go-to
  Reply
#4
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.
Stoned Ape

  Reply
#5
I prefer more a while loop, then the go-to way. Since you can also achieve this in a while loop.
  Reply
#6
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/354a67...adaa947591
  Reply
#7
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
[Image: widget.png?style=banner2]


  Reply
#8
(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".
  Reply
#9
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(dest, max_len,"%d-%02d-%02d %02d:%02d:%02d",get[0],get[1],get[2],get[3],get[4],get[5]); ? ? return 1; }
  Reply
#10
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(vehicleid, Float:partX, Float:partY, Float:partZ, &Float:x, &Float:y, &Float:z) { ? ?if (!IsValidVehicle(vehicleid)) { ? ? ? ?return 0; ? ?} ? ?new Float:a; ? ?GetVehiclePos(vehicleid, x, y, z); ? ?GetVehicleZAngle(vehicleid, a); ? ?x = partX * floatcos(a, degrees) partY * floatsin(-a, degrees); ? ?y = partX * floatsin(a, degrees) paryY * floatcos(-a, degrees); ? ?z = partZ; ? ?return 1; } // boot: GetVehicleModelInfo(GetVehicleModel(vehicleid), VEHICLE_MODEL_INFO_SIZE, x, y, z); GetVehiclePartAbsolutePos(vehicleid, 0.0, -y / 2.0, 0.0, x, y, z); // bonnet: GetVehicleModelInfo(GetVehicleModel(vehicleid), VEHICLE_MODEL_INFO_SIZE, x, y, z); GetVehiclePartAbsolutePos(vehicleid, 0.0, y / 2.0, 0.0, x, y, z); // front right tire: GetVehicleModelInfo(GetVehicleModel(vehicleid), VEHICLE_MODEL_INFO_WHEELSFRONT, x, y, z); GetVehiclePartAbsolutePos(vehicleid, x, y, z, x, y, z); // front left tire: GetVehicleModelInfo(GetVehicleModel(vehicleid), VEHICLE_MODEL_INFO_WHEELSFRONT, x, y, z); GetVehiclePartAbsolutePos(vehicleid, -x, y, z, x, y, z);
  Reply
#11
(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".
Stoned Ape

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



https://en.wikipedia.org/wiki/Spaghetti_code
  Reply
#13
Simple snippet that will make a M4 leave an explosion wherever its last shot lands:



PHP Code:
public OnPlayerWeaponShot(playerid, weaponid, hittype, hitid, Float:fX, Float:fY, Float: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; }
Stoned Ape

  Reply
#14
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
Stoned Ape

  Reply
#15
(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!
Check out Desolation Roleplay, where zombie AI and scavenging is bothered by player bandits!


  Reply
#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:
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
  Reply
#17
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; }
  Reply
#18
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); }
  Reply
#19
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; i != len; i ) { ? ? ? ? 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 i = 0; i <= strlen(str) - len; i ) { ? ? ? ? if (str[i] == startPos) { ? ? ? ? ? ? if (str[i len - 1] == '}' || IsValidHex(str[i len - 1])) { ? ? ? ? ? ? ? ? new ? ? ? ? ? ? ? ? ? ? pass; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? for (new j = 1; j < len - 1; j ) { ? ? ? ? ? ? ? ? ? ? if (IsValidHex(str[i j])) { ? ? ? ? ? ? ? ? ? ? ? ? pass ; ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? if (pass >= len - 2) { ? ? ? ? ? ? ? ? ? ? strdel(str, i, i len); ? ? ? ? ? ? ? ? ? ? pass = 0; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? } ? ? } ? ? return 1; }
???? ???? ????? ?? ???????? ???? ?????, ?? ???? ???? ?? ?????? ?? ??? ???? ???? ???? ?? ??? ???? ??? ???? ??? ?? ?? ?????? ??????





  Reply
#20
(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; }
  Reply