I have been working on a custom chat for the last week or so. I managed to get most of it done. However, I have it that is the player chats something that is long it will wrap into another TextLabel. There is a problem with this though.
If I use string.sub to cut the strings to put in the separate chat TextLabel it cuts the string off at inconvenient places. Like this
Hi there, the pro
blem with my ch
at is it is way har
der to read whe
n cut up like thi
s.
I would much rather have it like this:
Hi there, it
would be a
lot better with
my chat cut
up like this.
So what I need is a function that carefully cuts a string at space but is still short enough to fit in my text label. So I did some stretches and spent a few hours making the most complex masterpiece I ever made(because I am not a pro coder) that would return a number that would be the best place to cut that string:
local function carefulCutCor(msg,i,j,front,maxSize)âmsg is the fuul msg that will be searched:
âi number of the beginin of the chuck that will be searched for space cut convinient:
âj number of the end of the chuck that will be searched for space cut convinient :
âfront wether the spaceâs should be rounded in the start of the msg or the end :
âmaxSize the maximium size of what the string will be if rounded to nearest space
if string.find(msg," ") ~= nil then â wether the msg has no space to round to
local openSpace â the varible of the position of the space
if front then -- whether the space's should be rounded in the start of the msg or the end
openSpace = i -- if its in the front then the number of the beginning of the chuck( the one that will be searched for space cut convinient) is the openSpace so far
while string.sub(msg,openSpace,openSpace) ~= " " do -- while the openspace is not a space then keep seardhing up
openSpace = openSpace + 1
end
if string.len(string.sub(msg,openSpace,j)) > maxSize then -- if what the string would be if rounded is more then the maxSize then abort funcion
openSpace = j
end
else
openSpace = j -- if its in the front then the number of the end of the chuck( the one that will be searched for space cut convenient) is the openSpace so far
while string.sub(msg,openSpace,openSpace) ~= " " do -- while the openspace is not a space then keep searching down
openSpace = openSpace - 1
end
if string.len(string.sub(msg,i,openSpace)) > maxSize then -- if what the string would be if rounded is more then the maxSize then abort funcion
openSpace = j
end
end
return openSpace
else
return j -- if no spaces return the number of the end of the chuck that will be searched for space cut convenient
end
end
MessageLine1 = string.sub(msg,0,carefulCutCor(msg,0,60,false,60))
MessageLine2 = string.sub(msg,carefulCutCor(msg,60,string.len(msg),true,85),string.len(msg))
(Sorry if comments are bad grammar, I had written them for myself)
My problem is that this function sometimes will remove a word because of the lines are searching in different directions for the openSpace.
I could make them search in the same direction for the open space but then the string would pass the max size. Example:
âThis is a string that I want to be cut into two readable lines right where said the letter w in two.â
If the both searched in the same directions the function would take this and make this:âThis is a string that I want to be cut intoâ âreadable lines right where I said the letter w in two.â
And if the search in different directions it would be: âThis is a string that I want to be cut intoâ âtwo readable lines right where I said the letter w in two.â
And the latter looks good but the second string would surpass the maxSize because the LineTwoMessage TextLabel can only take something as big as âwo readable lines right where I said the letter w in two.â
Then I would have to abort the last letter(two) in the second line to go to the third and thatâs where my brain crashes. I am just not smart enough.
So can someone please help me out with this?