Converting a string to a function or API reference

Started by Sandgunslinger, May 29, 2021, 09:25 PM

Previous topic - Next topic

Sandgunslinger

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.

Lupus590

#1
local redAsString = "red"
local redAsColour = colours[redAsString]
term.setTextColor(redAsColour )
print("test")

Sandgunslinger