ComputerCraft Forums

ComputerCraft => Ask a Pro => Topic started by: Sandgunslinger on May 29, 2021, 09:25 PM

Title: Converting a string to a function or API reference
Post by: Sandgunslinger on May 29, 2021, 09:25 PM
I am trying to make code that takes a string, such as "red" and turns it into a color in the colors API. So far I've come up with something like this:
local color = "red"
color = "colors."..color
-- put code here that turns the string "colors.red" into the actual API reference colors.red
term.setTextColor(color)
print("test")

What I want is for the terminal to print "test" in red text but I can't do that without turning the string into a function or API reference (or whatever it's called). I've tried using loadstring to convert it but it always returns as nil.
Title: Converting a string to a function or API reference
Post by: Lupus590 on May 29, 2021, 09:45 PM
local redAsString = "red"
local redAsColour = colours[redAsString]
term.setTextColor(redAsColour )
print("test")
Title: Converting a string to a function or API reference
Post by: Sandgunslinger on May 29, 2021, 10:01 PM
Lupus590 it works now, thanks!