• String formatting a darkRPVar
    8 replies, posted
Hello, I have asked a question similar to this before but I didn't want to reopen that old thread so I am making a new one. I was wondering how I can format a darkRPVar string. I am making a custom agenda hud and I don't know how I can format the string returned from LocalPlayer():getDarkRPVar("agenda"). I checked the original darkrp files but it looks like they made their agenda hud long enough that they don't have to worry about making it multiple lines. How can I format the string returned? I have been playing around with surface.getsize() but im not sure how to insert a \n inside the string that I cannot even see.
You should read up on concatenation [url]https://www.lua.org/pil/3.4.html[/url]
[QUOTE=txike;51729572]You should read up on concatenation [url]https://www.lua.org/pil/3.4.html[/url][/QUOTE] I know what concatenation is, but the agenda DarkRPVar returns a string that I cannot see or change.. It's stored in the actual [CODE]LocalPlayer():getDarkRPVar("agenda")[/CODE]... I don't understand how I can concat in a string that I cannot actually manipulate or change.
[QUOTE=MrRalgoman;51730215]I know what concatenation is, but the agenda DarkRPVar returns a string that I cannot see of change.. It's stored in the actual [CODE]LocalPlayer():getDarkRPVar("agenda")[/CODE]... I don't understand how I can concat in a string that I cannot actually manipulate or change.[/QUOTE] From what little I understand about DarkRP I'm assuming that ply:getDarkRPVar("agenda") returns a string. You can concat strings with other strings to make even bigger strings. Simple.
[QUOTE=txike;51730240]From what little I understand about DarkRP I'm assuming that ply:getDarkRPVar("agenda") returns a string. You can concat strings with other strings to make even bigger strings. Simple.[/QUOTE] Right, but it returns a string in it's entirety. How could I concat INSIDE the string? I want to insert a \n after the surface.getsize hit's a certain character length. Here is an image so you can better understand what I am trying to do. [IMG]https://i.gyazo.com/9d2c6ffdb6e47299cc896b7f9e3ab1bf.png[/IMG]
You can do stringVar:length, and if it is over x amount then you simply insert a n
[QUOTE=MrRalgoman;51730285]Right, but it returns a string in it's entirety. How could I concat INSIDE the string? I want to insert a \n after the surface.getsize hit's a certain character length. Here is an image so you can better understand what I am trying to do. [IMG]https://i.gyazo.com/9d2c6ffdb6e47299cc896b7f9e3ab1bf.png[/IMG][/QUOTE] I would do it like this [code] local function FmtTextToWidth(str,chars_per_line) local lines = {} local line = 1 local line_length = 0 str:gsub("(%a+)",function(s) lines[line] = lines[line] or {} if line_length + #s <= chars_per_line then --Word can fit on this line table.insert(lines[line],s) line_lenth = line_length + #s elseif #s > chars_per_line then --Word can't fit on any line, format it lines[line+1] = {string.format("%." .. chars_per_line-3 .. "s...",s)} line = line + 2 line_length = 0 else line = line + 1 line_length = #s lines[line] = {} table.insert(lines[line],s) end end) --Get words for k,v in pairs(lines) do lines[k] = table.concat(v," ") end return table.concat(lines,"\n") end [/code] *note, this will trim words, so "this is a string" will appear as "this is a string"
I use this: [code]local TextCut = function(str, font, width, cut) surface.SetFont(font); if (surface.GetTextSize(str) > width) then while (surface.GetTextSize(str .. cut) > width) do str = str:sub(1, #str - 1); end str = str .. cut; end return str; end[/code] to cut strings short if they're longer than a the specified width. I'm sure if you had at least half a brain cell you'd be able to utilise it to add a newline instead.
:snip: I need to read better
Sorry, you need to Log In to post a reply to this thread.