Raised This Month: $51 Target: $400
 12% 

Print the "%" symbol


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Arkarr
Veteran Member
Join Date: Sep 2012
Location: Just behind my PC screen
Old 07-25-2016 , 16:00   Print the "%" symbol
Reply With Quote #1

The title might looks dumb, but I atually ran in that problem. How can I print the '%' ? I know in C++ it's the '%' followed by another '%' :
"This is a pourcent : 86.76%%" -> PRODUCE -> This is a pourcent : 86.76%
But it sourcemod it apperently doesn't work this way, it just create a blank space.
Also, I'm using "multicolors" to print my message using CPrintToChat. Might play a role in all this stuff.
__________________
Want to check my plugins ?

Last edited by Arkarr; 07-25-2016 at 16:00.
Arkarr is offline
psychonic

BAFFLED
Join Date: May 2008
Old 07-25-2016 , 16:19   Re: Print the "%" symbol
Reply With Quote #2

PrintToChatAll("%%"); will print a "%".

It's very possible that the included function you're using is doing something goofy.
psychonic is offline
WildCard65
Veteran Member
Join Date: Aug 2013
Location: Canada
Old 07-25-2016 , 16:44   Re: Print the "%" symbol
Reply With Quote #3

Wouldn't %% fed into VFormat correctly do 1 % remaining and then having it fed into another function that does formatting result in last remaining % being consumed?

Try "%%%%" when feeding "%" to be displayed into functions that will double format.

Logic flow:
1) Input string containing "%%" instead of "%%%%" is fed into a function that formats the string (like Format native)
2) Output contains "%" instead of "%%"
3) Given output is then fed as format string into another method that does string formatting (like Format native)
4) Output has the remaining "%" consumed instead of formatting a "%%". It's at this point where stuff like "%%s" will yield an obvious issue.
__________________

Last edited by WildCard65; 07-25-2016 at 16:47.
WildCard65 is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 07-25-2016 , 16:53   Re: Print the "%" symbol
Reply With Quote #4

Quote:
Originally Posted by WildCard65 View Post
Wouldn't %% fed into VFormat correctly do 1 % remaining and then having it fed into another function that does formatting result in last remaining % being consumed?

Try "%%%%" when feeding "%" to be displayed into functions that will double format.

Logic flow:
1) Input string containing "%%" instead of "%%%%" is fed into a function that formats the string (like Format native)
2) Output contains "%" instead of "%%"
3) Given output is then fed as format string into another method that does string formatting (like Format native)
4) Output has the remaining "%" consumed instead of formatting a "%%". It's at this point where stuff like "%%s" will yield an obvious issue.
Don't double-format strings.

So, instead of doing this:

PHP Code:
Format(percentStringsizeof(outString), "%d%%"myNum);
PrintToChat(clientpercentString); 
do this:

PHP Code:
Format(percentStringsizeof(outString), "%d%%"myNum);
PrintToChat(client"%s"percentString); 
I know this is a really contrived example, but it shows how to correctly handle passing an already formatted string to a Format function like PrintToChat.

On a side note, I fixed this in colors.inc a long time ago; I have no idea which version of colors.inc multicolors was based on.
__________________
Not currently working on SourceMod plugin development.

Last edited by Powerlord; 07-25-2016 at 16:54.
Powerlord is offline
Arkarr
Veteran Member
Join Date: Sep 2012
Location: Just behind my PC screen
Old 07-26-2016 , 06:44   Re: Print the "%" symbol
Reply With Quote #6

Yep, as pointed above, include problem. Problem fixed !
__________________
Want to check my plugins ?
Arkarr is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 07-26-2016 , 15:47   Re: Print the "%" symbol
Reply With Quote #7

Also, I should probably facepalm for not realizing that it wasn't necessarily multicolors but could also be the plugin calling CPrintToChat that needed to be fixed. Whoops.

Anyway, the double-format thing should be tossed onto the wiki somewhere as a potential gotcha if it isn't already.

Edit: Oh, it was multicolors... it was updated earlier today. So, you should update multicolors in your plugin if you use it.
__________________
Not currently working on SourceMod plugin development.

Last edited by Powerlord; 07-26-2016 at 16:50.
Powerlord is offline
WildCard65
Veteran Member
Join Date: Aug 2013
Location: Canada
Old 07-26-2016 , 16:46   Re: Print the "%" symbol
Reply With Quote #8

Quote:
Originally Posted by Powerlord View Post
Don't double-format strings.

So, instead of doing this:

PHP Code:
Format(percentStringsizeof(outString), "%d%%"myNum);
PrintToChat(clientpercentString); 
do this:

PHP Code:
Format(percentStringsizeof(outString), "%d%%"myNum);
PrintToChat(client"%s"percentString); 
I know this is a really contrived example, but it shows how to correctly handle passing an already formatted string to a Format function like PrintToChat.

On a side note, I fixed this in colors.inc a long time ago; I have no idea which version of colors.inc multicolors was based on.
Woops, forgot that "%s" can bypass need of accidental double formatting if it's used by itself with param match being true formatted string.
__________________
WildCard65 is offline
Reply



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

Forum Jump


All times are GMT -4. The time now is 14:02.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode