1

How to create a "pseudo-instance" variable or access an instance variable from a simple function defined inside a class in Lua ?

I did'nt find any way to access an instance variable from a simple function inside a class. I can only access it if the function is a method i.e. with the prefix of the class before ( anyClass:method() ) usinf "self." keyword. Thanks ;)

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
Lostania
  • 145
  • 8
  • 1
    What do you mean by "instance variable"? Also, Lua doesn't have classes; it has tables. So how are you using a Lua table to mimic a class? – Nicol Bolas Aug 31 '19 at 15:12
  • Hello and thank you for your comment. I use Codea on iPad witch is Lua based. I don't know how but Codea mimics classes. I need to access an instance variable of my class (a "self." variable) but self. is not accessible from a simple function, only from a method and I don't know how to proceed then. – Lostania Aug 31 '19 at 16:42
  • Instance variables are typically stored as table entries, so you can access them outside of methods with `myObject.instanceVariable`. – luther Aug 31 '19 at 17:06
  • If it helps, you can write the function definition with an explicit self parameter, yet call it with `:`. The distinction between method and non-method functions is simply a matter of style and is independently chosen by the definition and each caller. – Tom Blodget Aug 31 '19 at 19:12

0 Answers0