• Print

Author Topic: Set max size of string  (Read 11331 times)

0 Members and 1 Guest are viewing this topic.

Offline Zmaster

  • Full Member
  • ***
  • Posts: 235
  • Karma: 25
Set max size of string
« on: January 06, 2015, 12:58:09 pm »
I'm working with a HUD still

On one part of it, the client's name is shown
The problem is, if the name is too long, it'll go outside of the box it's in

So I need to set the max size of the string

I tried doing this

Code: Lua
  1. draw.DrawText( math.Clamp( string.len( ply:Nick() ), 1, 2 )..": "..myrole, "Arial25", 30, ScrH() - 60, Color( 0, 0, 0, 255 ) )

But as soon as I loaded it ingame, I realized why it wouldn't work for me
Hint: My name is Zee, not 2


(Arial25 is a custom font, don't worry about that)

So how would I go about setting the max size for a string that is shown on the HUD?

I know I can't just remove the string.len() function because then I'll get errors, since it'll return a string instead of an integer, which the math.Clamp() needs
« Last Edit: January 06, 2015, 01:00:00 pm by Zmaster »

Offline Avoid

  • Full Member
  • ***
  • Posts: 142
  • Karma: 42
Re: Set max size of string
« Reply #1 on: January 06, 2015, 02:12:54 pm »
Hello,
rather than cutting nicknames to fit your boxes why not make it the otherway round?

Code: [Select]
surface.SetFont("MySuperFont")
local x,y = surface.GetTextSize( ply:Nick() )

Alternative you could use a smaller font if the nickname exceeds a certain length.


Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Set max size of string
« Reply #2 on: January 06, 2015, 04:56:23 pm »
Just an idea. No idea how monospaced your font is (doesn't look like it much); but;
Figure out a max characters, perhaps using the character in a nickname that takes up the most space.
if string.length ( nickname ) > <some max size character length> then var_nick_string = <I forget>
Where <I forget> would be the string commands to find the character <max size character length> into the nick name, minus 3 places back , string.sub the rest of the nick to it's end with "..."
Basically, would "truncate" any long name with ...
JamminR, if too long would be Jamm...

"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Zmaster

  • Full Member
  • ***
  • Posts: 235
  • Karma: 25
Re: Set max size of string
« Reply #3 on: January 07, 2015, 03:52:40 pm »
I don't know if I was doing it wrong, but surface.GetTextSize() was super buggy for me
It just kept switching between three random numbers idk what happened

For JamminR, what you said would've been perfect but I wasn't quite sure how to do that based on your post and I wasn't having any luck finding out on my own, and since I'm on a clock I had to find another way

In the end I used a combination of both of your posts
JamminR said "string.length ( nickname )" -- which is what I used to do V
and Avoid said "Alternative you could use a smaller font if the nickname exceeds a certain length." - which is what I did

With my final code being this
Code: Lua
  1.         if surface.GetTextSize( ply:Name() ) > 138 then
  2.                 myfont = "Arial20"
  3.         else
  4.                 myfont = "Arial25"
  5.         end
(Much simpler than I thought it would be)
As usual, thanks for the help, kind sirs
I'm giving both of you a compliment lol

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Set max size of string
« Reply #4 on: January 07, 2015, 04:32:56 pm »
Had more time to poke around the internet for string library commands. Not so much time that I verified my quick search theories though.
Though there is a way to do what I suggested using string.find, string.sub , etc, I found even more simple suggestion. string.format( %s, string), apparently, has a "limit' function for %s
For future reference, I think string.format( %<#>s, <nickname> ) would format <nickname> to # characters, AND, if less than #, PAD with spaces. Meaning, string would always be that number of characters after the format.
In theory, could use something like this to pass to your text. You'd still need to figure out max/best #.
Code: [Select]
player_name = ply:Name()
if string.len( player_name ) > <some #> then
    player_name = string.format ( %<some # -3>s, player_name ) .. "..." -- this name is static <#> characters and truncated including the ...
else
    player_name = string.format ( %<some #>s, player_name ) -- this name is static and includes spaces up to #.
end

EDIT - USE a "-" in front of the <some #> if you want LEFT justified, other wise it's RIGHT justified.
So, example, you want 30 characters of space.
My name in Steam is [ULX]JamminR
> string.format( "%30s", "[ULX]JamminR" )
= "                  [ULX]JamminR"
> string.format( "%-30s", "[ULX]JamminR" )
= "[ULX]JamminR                  "

Might be off a bit, but, you get the ideas.
Nice formatted page here - http://wiki.roblox.com/index.php?title=String_Formatting
« Last Edit: January 07, 2015, 04:45:43 pm by JamminR »
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Zmaster

  • Full Member
  • ***
  • Posts: 235
  • Karma: 25
Re: Set max size of string
« Reply #5 on: January 07, 2015, 05:05:16 pm »
Well thank you

There is an error though and I'm looking at the gmod wiki posting for that function and it says
string.format( string format, vararg formatParameters ) -- http://wiki.garrysmod.com/page/string/format

So I changed "string.format(%-9s, player_name )"
to "string.format( player_name, %-9s )"

However, no matter which way I put that function, I receive this error
Code: Lua
  1. [ERROR] addons/hudtesting/lua/autorun/zhud.lua:53: unexpected symbol near '%'
  2.   1. unknown - addons/hudtesting/lua/autorun/zhud.lua:0
(Line 53 being that line that I put in the code above)

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Set max size of string
« Reply #6 on: January 07, 2015, 05:23:43 pm »
No, the first part "is" the string statement, the second part is what your placing "in" the string edited area. (Unless Garry just totally changed string.format from lua 5)
string.format(  "%-9s", player_name )
You'll need to define player_name before using that of course, and, you'll need quotes around %-9s, my original mistake was leaving them out.

Code: [Select]
player_name = ply:Name()
if string.len( player_name ) > 9 then
    player_name = string.format ( "%6s" , player_name ) .. "..." -- this name is static <#> characters and truncated including the ...
else
    player_name = string.format ( "%-9s" , player_name ) -- this name is static and includes spaces up to #.
end
And, wow, your scoreboard or whatever can only show 9 characters? Ouch.
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Zmaster

  • Full Member
  • ***
  • Posts: 235
  • Karma: 25
Re: Set max size of string
« Reply #7 on: January 08, 2015, 03:57:05 am »
lol no I just didn't have GMod open at the time so I chose a random number to test ingame after that
I'll test what you said when I get home from school

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Set max size of string
« Reply #8 on: January 08, 2015, 03:33:44 pm »
Good news and bad news. First, good news.
I found an AWESOME (to me anyway) coding site that has terminal sessions for many different programming languages. LUA included
http://www.tutorialspoint.com/codingground.htm

Bad news, in testing string.format, I found that I'd missed a very important fact, stated in the nice formatted page I linked earlier.
That number "width" will only do anything when the string being passed to it is shorter than width. Meaning, it's only meant to pad a short string with spaces to match <width>
So, my idea as listed for now won't work.

I'm still tinkering just to figure out how to do it though. Perhaps using my original .find, .sub, etc.
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Set max size of string
« Reply #9 on: January 08, 2015, 03:48:22 pm »
I'm an idiot.
It's been too long since I've actually coded in lua, apparently, to have been real help to you.
I'm sorry for that.

string.sub ( name, 1, 9 )
Will return the first through ninth characters of any string.

I'm sure my team mates were clutching there guts going "Nooo" wanting so bad to tell me/us how to do it.

Code: [Select]
player_name = ply:Name()
if string.len( player_name ) > 9 then
    player_name = string.sub ( player_name, 1, 6 ) .. "..."
else
    player_name = string.format ( "%9s" , player_name ) -- string.format will still work here, will pad with spaces, use "-" minus in front of 9 if you want it left justified.
end
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Zmaster

  • Full Member
  • ***
  • Posts: 235
  • Karma: 25
Re: Set max size of string
« Reply #10 on: January 08, 2015, 04:03:03 pm »
lol it's fine
You're a great help, sir JamminR
You have earned one free internet cookie, since there's nothing else I can give you

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Set max size of string
« Reply #11 on: January 08, 2015, 04:30:46 pm »
You can adjust the numbers as needed of course. I stuck with 9 and 6(9-3) + ...(3 dots) = (9) because that was our example.
Just tested my code at the lua interpreter. It works. Had to add print for testing and replace ply:Nick() with a long string, of course
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

  • Print