Am I wrong in this thinking?

I’ve always assumed that if I load in a class using the local require that class would be local to the scene. However after doing a print of all the variables in the class I realized that stuff I added in other scenes to the class are showing up in a different scene. 

When you load in a class locally to a scene and add something to it will it still be there when you go to a different scene?

lua doesn’t create a new instance of your code each time you require() it.  require() is like “load if you haven’t yet”.

the variable that you’re referring to your code is local to this scene, but not the code itself.

if you’re thinking in OOP terms, then if you modify a *class* then yes, wherever you load that class will retain the changes.

what you may be intending to do is have your class return instances - then modify the instances for local use.

for instance (pun intended :D):

-- myClass will be local, but the code it references may already have been loaded/modified elsewhere -- in general you probably DON'T want to be modifying \*classes\* at runtime -- else no-one "else" (ie your code elsewhere) might know what to expect from it local myClass = require("MyClass") -- myInstance will be a brand new local copy of the base class -- go ahead and modify instances at runtime local myInstance = myClass.new(..whatever..)

Man I can’t believe that I can work with a language for almost 3 years and not fully understand it lol. So everything is essentially pass by reference even if it’s locally required. The only things that are truly local to the scene would be anything that is created on that scene then? 

I guess that makes sense now that I think about it. Most of my apps have been 1 scene with overlays though so I guess I wouldn’t have run into this before. 

you got it.

btw, you can use common paradigms like “local composer = require(‘composer’)” as a reminder – ie, do you think an app with 14 scenes would have 14 unique copies of that code?  nope, just one.  (which is why, for some simple apps, it’s kinda silly to even bother with locals for corona core modules, just globally require it once, and the same might be true for certain of your classes if they’re used essentially everywhere, but that’s another topic (and a potentially contentious one as well!))

Yeah it makes perfect sense when I actually step back and think about it. On the surface though my mind was thinking local means everything is local to that scene, function etc etc. 

lua doesn’t create a new instance of your code each time you require() it.  require() is like “load if you haven’t yet”.

the variable that you’re referring to your code is local to this scene, but not the code itself.

if you’re thinking in OOP terms, then if you modify a *class* then yes, wherever you load that class will retain the changes.

what you may be intending to do is have your class return instances - then modify the instances for local use.

for instance (pun intended :D):

-- myClass will be local, but the code it references may already have been loaded/modified elsewhere -- in general you probably DON'T want to be modifying \*classes\* at runtime -- else no-one "else" (ie your code elsewhere) might know what to expect from it local myClass = require("MyClass") -- myInstance will be a brand new local copy of the base class -- go ahead and modify instances at runtime local myInstance = myClass.new(..whatever..)

Man I can’t believe that I can work with a language for almost 3 years and not fully understand it lol. So everything is essentially pass by reference even if it’s locally required. The only things that are truly local to the scene would be anything that is created on that scene then? 

I guess that makes sense now that I think about it. Most of my apps have been 1 scene with overlays though so I guess I wouldn’t have run into this before. 

you got it.

btw, you can use common paradigms like “local composer = require(‘composer’)” as a reminder – ie, do you think an app with 14 scenes would have 14 unique copies of that code?  nope, just one.  (which is why, for some simple apps, it’s kinda silly to even bother with locals for corona core modules, just globally require it once, and the same might be true for certain of your classes if they’re used essentially everywhere, but that’s another topic (and a potentially contentious one as well!))

Yeah it makes perfect sense when I actually step back and think about it. On the surface though my mind was thinking local means everything is local to that scene, function etc etc.