When to use self.value?

Just startet using codea, and I wonder when do I have to use self.value / value when working with classes?

Is there some “get startet toturials” that explain questions like this?

Thanks :slight_smile:

The answer to your question may depend on what you know already. Try @Reefwing’s site here or the Codea Wiki here.

When you work with a class there are 3 ways to declare variables:

function myClass:init()
   self.var1 = 0
   local var2 = 0
   var3 = 0

Var3 is global and static, so it can be accessed from any other part of the program and remains during all the program execution. Its access time is longer.
Var2 is local and not static: can be accessed only from init() function, disappears after init has run, its access is fast.
Var1 is local and static, but can be accessed from anywhere. From the object obj its access time (via self.var1) is fast, but from outside the object (via obj.var1) it is longer. If you create several objects of your class, obj1 and obj2, then there are 2 different variables “var1”: obj1.var1 and obj2.var1, but “var3” will refer to the same variable for the 2 objects.
That being set, my answer to your question:
Global variables (var3) should be the exception: it is longer, and dangerous (modif by error is easy), and you should make everything local (var2), except when you need the variable to be static and then use self.var1

Thanks. I’ll have tjek out the toturials and the wiki.

Thank jmv38 for pointing it out.