SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Filterscripts > Includes

Reply
 
Thread Tools Display Modes
Old 26/01/2012, 09:00 AM   #1
Slice
High-roller
 
Slice's Avatar
 
Join Date: Mar 2008
Location: Sweden
Posts: 1,866
Reputation: 1267
Spray formatex - Improved "format" function!

Hey,

After seeing a great new feature in sscanf that allows you to create custom specifiers, I figured it was only right to do the same to format and printf!

For example, the following code could be made a lot shorter:
pawn Code:
// This will put into "msg" the weapon you were given by a player, and the color of the player's name will match the player's color.

new msg[128], name[MAX_PLAYER_NAME], color;

color = GetPlayerColor(playerid);

GetPlayerName(playerid, name, sizeof(name));

format(msg, sizeof(msg), "You were given a %s by {%06x}%s", g_WeaponNameArray[weapon], color >>> 8, name);

This will do the same thing:
Code:
new msg[128];

format(msg, _, "You were given %w by %P", weapon, playerid);

// msg is for example: You were given an M4 by Slice
New specifiers
SpecifierDescription
%pName of the player ID given.
%PName of the player ID given, with the player's color before it.
%CInline color (ex. {FFFFFF}) from a normal color (ex. colors you get from GetPlayerColor).
%vVehicle model name from the model given (not vehicle ID, vehicle model).
%wWeapon name, lower-case singular (to be used in sentences). Example: "an M4", "a combat shotgun", "a knife".
%WWeapon's name.
%X8-byte, unsigned hex string (ex. FFFFFFFF).
%uUnsigned integer.

You can create your own specifiers!
You can do this very easily; example of one that puts an upper-case string at %S:
Add this anywhere outside of a function:
pawn Code:
// Upper-case string
FormatSpecifier<'S'>(output[], const param[]) {
    for (new i = 0, l = min(sizeof(output), strlen(param)); i < l; i++)
        output[i] = toupper(param[i]);
}

You can now do this:
pawn Code:
printf("hello %S!", "world");

// prints: hello WORLD!

If you want an integer, float, or anything else instead of a string you just change it:
pawn Code:
// You can call the argument whatever you like, and it can be a string/array:
FormatSpecifier<'A'>(output[], Float:health) { ... }
FormatSpecifier<'B'>(output[], objectid) { ... }
FormatSpecifier<'C'>(output[], playerid) { ... }
FormatSpecifier<'D'>(output[], Text:td) { ... }
FormatSpecifier<'D'>(output[], const string[]) { ... }

Additional notes
Worth mentioning is this is a superset of format, meaning it has exactly all the features of format and those behave like always (the only exception is it's a bit friendlier to packed strings, though %s still doesn't support it).
It actually uses the original native "format" function to do the heavy lifting.

Download

Download: http://pastebin.com/xMAMtB6M
__________________
Do you like SA-MP and DayZ? Come play both: ulclan.com:1421

Do you use any scripts I've made?
Star them on GitHub or reply in their topics so I know what I should keep working on!

Last edited by Slice; 26/01/2012 at 02:56 PM.
Slice is offline   Reply With Quote
Old 26/01/2012, 09:02 AM   #2
KingHual
High-roller
 
KingHual's Avatar
 
Join Date: Jun 2010
Location: Bulgaria
Posts: 1,965
Reputation: 931
Default Re: formatex - Improved "format" function!

Kewl, I actually need this.
__________________
(c) good image not stolen

I always suspected this place was a bullshit hugbox for wapanese furries with self-diagnosed aspergers, but every now and then I'm provided with concrete evidence.
KingHual is offline   Reply With Quote
Old 26/01/2012, 09:03 AM   #3
kizla
Big Clucker
 
kizla's Avatar
 
Join Date: Dec 2009
Posts: 196
Reputation: 50
Default Re: formatex - Improved "format" function!

very nice, i gonna use it for sure

edit: Why you use "GetMaxPlayers" instead of "MAX_PLAYERS" ??
__________________
Tutorials
Abstract Machine eXecutor / emits
kizla is offline   Reply With Quote
Old 26/01/2012, 09:05 AM   #4
[HK]Ryder[AN]
High-roller
 
[HK]Ryder[AN]'s Avatar
 
Join Date: Jan 2012
Location: New Delhi, India
Posts: 1,242
Reputation: 361
Default Re: formatex - Improved "format" function!

Great.Really Great.Don't know what to say anymore.....
[HK]Ryder[AN] is offline   Reply With Quote
Old 26/01/2012, 09:10 AM   #5
Slice
High-roller
 
Slice's Avatar
 
Join Date: Mar 2008
Location: Sweden
Posts: 1,866
Reputation: 1267
Default Re: formatex - Improved "format" function!

Quote:
Originally Posted by kizla View Post
very nice, i gonna use it for sure

edit: Why you use "GetMaxPlayers" instead of "MAX_PLAYERS" ??
Well I was using it to see if the player ID was in range. It can't be more than GetMaxPlayers, so that's why I used it.
__________________
Do you like SA-MP and DayZ? Come play both: ulclan.com:1421

Do you use any scripts I've made?
Star them on GitHub or reply in their topics so I know what I should keep working on!
Slice is offline   Reply With Quote
Old 26/01/2012, 09:19 AM   #6
iPLEOMAX
High-roller
 
iPLEOMAX's Avatar
 
Join Date: Mar 2011
Location: Al Ain, UAE. Homeland: Bangladesh.
Posts: 1,296
Reputation: 723
Default Re: formatex - Improved "format" function!

There you go, another awesome release. I'm obviously gonna use it.
__________________
Proud member & developer at CODE5 Gaming (cod5samp.com)
Los Santos: Cops & Robbers
Resurrection
Total War

*In case you saw me in any other server, it's probably an impersonator or my fan!
iPLEOMAX is offline   Reply With Quote
Old 26/01/2012, 09:30 AM   #7
fiki574
Gangsta
 
fiki574's Avatar
 
Join Date: Mar 2011
Location: Croatia
Posts: 822
Reputation: 151
Default Re: formatex - Improved "format" function!

Nice one, Slice!

rep+
__________________

If you need any help with anything related to SAMP and/or scripting/programming, feel free to PM me!
I am scripting/programming for money (very low prices, from $5 to $15 or more, price depends on complexity)
fiki574 is offline   Reply With Quote
Old 26/01/2012, 09:37 AM   #8
T0pAz
Guest
 
Posts: n/a
Default Re: formatex - Improved "format" function!

Yet another useful release. Nice work Slice(The next Y_Less).
  Reply With Quote
Old 26/01/2012, 09:48 AM   #9
Kaperstone
High-roller
 
Kaperstone's Avatar
 
Join Date: May 2011
Location: In a box infront of your house stealing your WiFi
Posts: 2,725
Reputation: 364
Default Re: formatex - Improved "format" function!

awesome,thanks
__________________
KrustyGames
Kaperstone.ru
Quote:
Originally Posted by Mellnik View Post
Looks like someone's trying to attack your unprotected server :O.
Kaperstone is offline   Reply With Quote
Old 26/01/2012, 10:02 AM   #10
System64
Gangsta
 
System64's Avatar
 
Join Date: May 2011
Location: Zadar, Croatia
Posts: 986
Reputation: 140
Default Re: formatex - Improved "format" function!

fucking awesome man

rep+

edit: shit I can't give rep
__________________
All my works
I left SA:MP! Maybe once I start scripting again but now I'm based on web programming
Soon, I'll buy domain system32.com.hr but for now you can contact me on LogoOff or on my Youtube channel!
System64 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
xMovie Entertainment (3 vids): "Like a boss" "The rapist" "Jizz in my pants" xMovie Screenshots and Videos 11 27/03/2012 12:10 PM
"Warning 209: function "CreateDynamicVehicle" should return a value." rati555 Scripting Help 2 06/11/2011 07:18 PM
"Warning 209: function "cmd_enter" should return a value." Urgent help needed! pEar Scripting Help 3 10/09/2011 05:55 PM
Little "format" and "dini" help blackwave Help Archive 3 28/12/2010 12:28 PM
"Run time error 19: "File or function is not found" TouR Help Archive 2 18/06/2010 10:04 AM


All times are GMT. The time now is 07:30 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.