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:
Code Select
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

May 29, 2021, 09:45 pm #1 Last Edit: May 29, 2021, 10:07 pm by Lupus590
Code Select
local redAsString = "red"
local redAsColour = colours[redAsString]
term.setTextColor(redAsColour )
print("test")