I dont really know how to phrase this but I've seen things in strings that are a percent sign and then what appears to me as a random letter and I have never seen this before. It seems to stand in for a variable but is a completely alien concept to me as to how I can use this to benefit my lua coding. So I would really like someone who understands this to point me in the right direction or explain it, I would be very thankful.
A paraphrased example based off something I found in the TTT health station entity
[code]
Damagelog( Format( "%a damaged %b for %c dmg"), dmginfo:GetAttacker(), dmginfo:GetDamage() )
[/code]
It's not random.
[t]http://i.imgur.com/OeJgpHz.png[/t]
The code you posted will never work.
[editline]15th July 2014[/editline]
string.format = Format
[url]http://wiki.garrysmod.com/page/Global/Format[/url]
They stand for various format flags - consider [url=http://www.cplusplus.com/reference/cstdio/printf/]printf[/url]
It makes displaying data in a human-friendly manner simpler.
[code]x = string.format( "%s was killed by %s with %s", victim:Nick( ), killer:Nick( ), weapon:GetClass( ) )[/code]
Rather than concatenating a lot of text.
You can also do interesting things with numbers, like displaying to a certain place, customized padding, if it should pad with 0s or spaces, if it should be displayed in hex, octal, or scientific, etc.
There's other specialized ones, like %q, which escapes and wraps the input in quotes, which is handy.
[code]pl:SendLua( string.format( "RunSomeFunction( %q )", user_input_here ) )[/code]
It becomes considerably harder to escape that than just piping the text in with %s.
There's other patterns when regarding os.date or patterns with find, match, gsub, and gmatch, but that's another thread.
it's also a lot more efficient than using .. for concatenation
my favorite use for it is printing floats with certain amounts of digits
[lua]
net.Receive("w/e", function(len)
print(string.format("%u bits (%.2f kb) received", len, len/8/1024))
end)[/lua]
It also makes re-using strings easier.
[code]
local F_REC_MSG = "%u bits (%.2f kb) received"
...
print( F_REC_MSG:format( len, len / 8 / 1024 ) )[/code]
Which is even handier with sql.
[code]
Q_INSERT_PLAYER = [[CALL gmod_insert_player_command( %q, %q );]]
...
handle:Query( Q_INSERT_PLAYER:format( cl:SteamId( ), net.ReadString( ) ) )[/code]
It might be a micro-optimization since strings are cheap and would be on the stack anyways, but it looks nicer to me.
[QUOTE=Robotboy655;45392297]It's not random.
[t]http://i.imgur.com/OeJgpHz.png[/t]
The code you posted will never work.
[editline]15th July 2014[/editline]
string.format = Format
[url]http://wiki.garrysmod.com/page/Global/Format[/url][/QUOTE]
This is what I wanted, I know that code wont work because I forgot the actual letters used. But this definitely looks cool and useful rather than:
[code]
print( ply1 .. " was hurt by " .. ply2 )
[/code]
Strings are kind-of a controversial subject with lua. I recommend you reading up on them so you fully understand what and how to use the various methods of string concatenation and other methods efficiently.
Sorry, you need to Log In to post a reply to this thread.