Jump to content




Put in a variable string for a method?


  • You cannot reply to this topic
4 replies to this topic

#1 gknova61

  • Members
  • 74 posts

Posted 19 November 2012 - 05:44 PM

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?

#2 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

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 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 19 November 2012 - 05:53 PM

Short explanation:
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 gknova61

  • Members
  • 74 posts

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!

#5 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 19 November 2012 - 06:07 PM

View PostKingdaro, on 19 November 2012 - 05:53 PM, said:

Short explanation:
aDirection = "forward"
turtle[aDirection]()

So I was right :(/> Thanks from me too for the explanation.





3 user(s) are reading this topic

0 members, 3 guests, 0 anonymous users