This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
CoolGabrijel's profile picture

String fail

Started by CoolGabrijel, 10 May 2015 - 08:08 AM
CoolGabrijel #1
Posted 10 May 2015 - 10:08 AM
Okay so i'm making a space game using Computercraft
Now after ALOT of code I decided to make it simpler
Heres the code:
http://pastebin.com/JS1kSbqf
(Sorry I dont know how to put it in a spoiler or a box)
The error:
string:1: attempt to index ? (a nil value)
Lyqyd #2
Posted 10 May 2015 - 04:43 PM
When you load a string, it doesn't automatically get the current function environment. You should declare your monitor color functions as actual functions instead:


local function MonitorColorRed()
  Monitor.setTextColor(colors.red)
end

Or just use the peripheral methods directly, which seems significantly easier.
Creator #3
Posted 10 May 2015 - 05:29 PM
I you still want to use loadstring, do this:


local func = loadstring("print(\"foo bar\")")
setfenv(func,_G) -- set the env to the global one
func()

Try it!
Lyqyd #4
Posted 10 May 2015 - 05:43 PM
That wouldn't work. You should probably try things before you post them and tell others to try it. :P/>
Creator #5
Posted 10 May 2015 - 07:06 PM
I can't see anything wrong. I even looked over it twice as to be sure there are no errors.
Lyqyd #6
Posted 10 May 2015 - 07:15 PM
The code you posted might work, since print is in _G, but it won't carry over to the code he's using, as his Monitor variable would not be in _G.
CoolGabrijel #7
Posted 11 May 2015 - 10:26 AM
When you load a string, it doesn't automatically get the current function environment. You should declare your monitor color functions as actual functions instead:


local function MonitorColorRed()
  Monitor.setTextColor(colors.red)
end

Or just use the peripheral methods directly, which seems significantly easier.
I don't really understand functions,thats why I used the loadstring() so can you explain it a bit more? (Im a newbie xD)
As for using the peripheral methods direcly, I would rather type
MonitorColorRed,MonitorColorRed,MonitorColorRed,MonitorColorRed than
Monitor.setTextColor(colors.red),Monitor.setTextColor(colors.red),Monitor.setTextColor(colors.red),Monitor.setTextColor(colors.red)
theoriginalbit #8
Posted 11 May 2015 - 11:09 AM
I don't really understand functions,thats why I used the loadstring() so can you explain it a bit more? (Im a newbie xD)

if you don't understand functions then you probably shouldn't be using loadstring.

a function allows us to run a sequence of instructions multiple times without needing to write them each time, every time you see a set of parenthesis this is what is known as a function call, that is, we're going to run the instructions within the function.

for example we can define a function like this

local function clear()
  term.setBackgroundColor(colors.black)
  term.clear()
  term.setCursorPos(1, 1)
end
and each time we call the function with clear() the Computer's screen will be turned completely black and the cursor will move to the top left corner. therefore we use functions to reduce duplicate code. we can also supply arguments/parameters to some functions for use; this can be seen when you use term.setCursorPos and supply it with two numbers, these numbers (or parameters) are where you wish the cursor to move. To do that you simply add a variable name between the parenthesis when creating the function, and then giving a value when you call the function. Example


local function printColored(text, textColor)
  term.setTextColor(textColor)
  print(text)
end

printColored("Hello world", colors.green)
printColored("Goodbye world", colors.red)

hopefully you can see that the above will help you severely reduce your code down.


As for using the peripheral methods direcly, I would rather type
MonitorColorRed,MonitorColorRed,MonitorColorRed,MonitorColorRed than
Monitor.setTextColor(colors.red),Monitor.setTextColor(colors.red),Monitor.setTextColor(colors.red),Monitor.setTextColor(colors.red)
well that's not really the case, you've done Monitor.setTextColor(colors.yellow) a few times. If you'd prefer it shorter perhaps you should do a function like this

local function color(c)
  Monitor.setTextColor(c)
end
then you can simply type color(colors.red) or color(colors.yellow)
CoolGabrijel #9
Posted 11 May 2015 - 11:14 AM
Thanks,Now I understand functions
Tho I did the yellow one because I didnt know about loadstring
Now with functions this will be significantly less
This thread can be closed now