Burgershot
  • Home
  • Members
  • Team
  • Help
  • Search
  • Register
  • Login
  • Home
  • Members
  • Help
  • Search
Burgershot SA-MP Pawn Scripting [Pawn] Post your code snippets here

 
  • 0 Vote(s) - 0 Average
Pawn Post your code snippets here
DTV
Offline

The dude
Posts: 45
Threads: 4
Joined: Apr 2019
Reputation: 4
#1
2019-04-14, 11:04 PM
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
Gravityfalls
Offline

Burgershot Member
Posts: 105
Threads: 7
Joined: Apr 2019
Reputation: 2
#2
2019-04-15, 01:46 AM
I believe I've read somewhere that go-to statements (loops?) are actually slower. Not sure but I'll let others comment about that.
iSpark
Offline

Burgershot Member
Posts: 10
Threads: 2
Joined: Apr 2019
Reputation: 0
Location: India
#3
2019-04-15, 03:50 AM
I'd rather use function than a go-to
DTV
Offline

The dude
Posts: 45
Threads: 4
Joined: Apr 2019
Reputation: 4
#4
2019-04-15, 07:07 AM
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
BlackBank
Offline

Burgershot Member
Posts: 13
Threads: 0
Joined: Apr 2019
Reputation: 0
Location: The Netherlands
#5
2019-04-15, 05:17 PM
I prefer more a while loop, then the go-to way. Since you can also achieve this in a while loop.
Codeah
Offline

Moderator

Posts: 59
Threads: 7
Joined: Apr 2019
Reputation: 4
Location: Belarus
#6
2019-04-15, 06:55 PM
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
Riddick
Offline

Moderator

Posts: 31
Threads: 6
Joined: Apr 2019
Reputation: 5
Location: Poland
#7
2019-04-16, 06:24 AM
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]
Y_Less
Offline

Administrator

Posts: 323
Threads: 16
Joined: Feb 2019
Reputation: 90
#8
2019-04-16, 11:23 AM
(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".
KJason
Offline

Burgershot Member
Posts: 1
Threads: 0
Joined: Apr 2019
Reputation: 1
#9
2019-04-16, 11:48 AM (This post was last modified: 2022-03-01, 08:55 PM by KJason.)
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;
} 
kristo
Offline

Burgershot Member
Posts: 47
Threads: 10
Joined: Feb 2019
Reputation: 7
Location: Estonia
#10
2019-04-16, 12:50 PM (This post was last modified: 2019-04-16, 12:50 PM by kristo.)
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); 
DTV
Offline

The dude
Posts: 45
Threads: 4
Joined: Apr 2019
Reputation: 4
#11
2019-04-16, 06:10 PM (This post was last modified: 2019-04-16, 06:11 PM by DTV.)
(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
Mike
Offline

Burgershot Member
Posts: 1
Threads: 0
Joined: Apr 2019
Reputation: 1
#12
2019-04-17, 02:45 AM
(2019-04-14, 11:04 PM)DTV Wrote: snip

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

The dude
Posts: 45
Threads: 4
Joined: Apr 2019
Reputation: 4
#13
2019-04-29, 07:24 PM (This post was last modified: 2019-04-29, 07:24 PM by DTV.)
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
DTV
Offline

The dude
Posts: 45
Threads: 4
Joined: Apr 2019
Reputation: 4
#14
2020-01-03, 06:33 PM (This post was last modified: 2020-01-03, 06:33 PM by DTV.)
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
Awide
Offline

Burgershot Member
Posts: 62
Threads: 5
Joined: Sep 2019
Reputation: 5
#15
2020-10-18, 07:33 PM
(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!
Desolation Roleplay has closed. You can download the gamemode here: https://www.burgershot.gg/showthread.php?tid=2272
Grate Maharlika
Offline

Burgershot Member
Posts: 18
Threads: 7
Joined: Oct 2020
Reputation: 0
#16
2020-10-19, 03:22 PM (This post was last modified: 2020-10-19, 06:20 PM by Grate Maharlika.)
(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
Expert*
Offline

Burgershot Member
Posts: 61
Threads: 2
Joined: Apr 2019
Reputation: 4
#17
2020-10-24, 12:40 PM (This post was last modified: 2020-10-24, 01:02 PM by Expert*.)
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;
}
Virsenas
Offline

Burgershot Member
Posts: 47
Threads: 0
Joined: Feb 2021
Reputation: 6
#18
2021-02-26, 11:53 PM
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);
}
Tama
Offline

Burgershot Member
Posts: 42
Threads: 8
Joined: Sep 2019
Reputation: 1
Location: India
#19
2021-10-27, 03:59 PM (This post was last modified: 2021-10-27, 04:03 PM by Tama.)
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;
} 
आपको अपने पापों पर पश्चाताप करना चाहिए, वे आपको नष्ट कर देंगे। आप कोई मौका नहीं खड़े हो रहे हैं। तुम उसके हाथ पर मर जाओगे। पछताना


« Next Oldest | Next Newest »



  • View a Printable Version
  • Subscribe to this thread
Forum Jump:

© Burgershot - Powered by our Community and MyBB Original Theme by Emerald

Linear Mode
Threaded Mode