Here is the full code:
http://pastebin.com/f2CHf08r
Error is on line 20. You can hopefully see what i'm trying to-do. I can't explain it that well, so but basically
Example:
aDirection = forward
turtle.aDirection()
This just errors attempt to call nil ofc so anyone got any ideas on how to fix this?
Put in a variable string for a method?
Started by gknova61, Nov 19 2012 05:44 PM
4 replies to this topic
#1
Posted 19 November 2012 - 05:44 PM
#2
Posted 19 November 2012 - 05:49 PM
I've also tried this with term.setTextColours(colours[colour]). Does turtle[aDirection]() not work? This would help me too if someone posts how to get it to work.
#3
Posted 19 November 2012 - 05:53 PM
Short explanation:
Longer explanation:
In the original example you've provided, the script sets variable "aDirection" to "forward", not "turtle.forward". Variable "forward" does not exist, therefore aDirection does not exist either.
On the second line, lua can't directly reference a string from a table using dot notation. It's the same as doing
aDirection = "forward" turtle[aDirection]()
Longer explanation:
In the original example you've provided, the script sets variable "aDirection" to "forward", not "turtle.forward". Variable "forward" does not exist, therefore aDirection does not exist either.
On the second line, lua can't directly reference a string from a table using dot notation. It's the same as doing
turtle."forward"()which obviously doesn't work. To get around this, you would put brackets around your string, and remove the dot.
turtle["forward"]()
#4
Posted 19 November 2012 - 05:54 PM
Seems it does, thanks! May I ask the reasoning behind that working? I'm just trying to learn more about lua
/> Thanks KingDoro for the explanation!
3 user(s) are reading this topic
0 members, 3 guests, 0 anonymous users












