How do I only color part of a string?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Posts: 18
Joined: Tue Apr 24, 2018 7:37 pm

How do I only color part of a string?

Post by ITISITHEBKID » Sat Jun 23, 2018 5:02 pm

I am working on getting text to display properly within a box. It works perfectly so far, and I have properly gotten it to identify tags. The problem comes when I try to print the text to the screen. If I have "This is <Red>text<White>", the program will properly identify red and white as tags and change the color accordingly. This is good but because the entire thing is one string, when <white> is called it just turns the whole thing red and if just red is called it turns the whole thing red. What do I need to do to fix this?

Code for identifying tags

Code: Select all

	local chars = {} --empty table. will be filled with individual charachters from (text)
	text:gsub(".",function(c) table.insert(chars,c) end) --fill table as described above
	textimer = timer:every(textspeed,function() --the timer. calls this function every textspeed of a second
		if chars[1] == "<" then --if the beginning of a tag
			textcolor = "" -- the color is blank
			table.remove(chars,1) --remove tag beginning 
			while chars[1] ~= ">" do --while the tag is still going on
				textcolor = textcolor .. chars[1] --the current character is added to the textcolor
				table.remove(chars,1) --and then removed from the table.
			table.remove(chars,1)--this only executes once the tag is finished, because of the while function
			textcolor = _G[textcolor] --this turns the string into a variable to work with setColor
In love.draw

Code: Select all,0.25,0.67) -- set color to black"fill",box.x-13,box.y,box.width,Font:getHeight()*box.lines,15,15,3) -- this box is drawn first, so everything else is drawn over it. It makes the background of the box non transparent --set color to white,box.x,box.y)--draw the actual text --thicker line"line",box.x-13,box.y,box.width,Font:getHeight()*box.lines,15,15,3) --draw the box --regular line
(1.29 MiB) Downloaded 11 times

Party member
Posts: 497
Joined: Sat Jul 22, 2017 7:43 pm

Re: How do I only color part of a string?

Post by grump » Sat Jun 23, 2018 5:48 pm

There's a built-in variant of that takes a table of strings and colors and prints the strings accordingly. I recommend using that.

Post Reply

Who is online

Users browsing this forum: Exabot [Bot], Google [Bot] and 6 guests