New to Lua Question

Hi guys, just started learning Lua (thanks to Corona!) this weekend and I am getting pretty close to grokking it.

I have one question which is probably best asked in code.
See “QUESTION” below:

  
local MakeShaun = function(passedIn)  
  
 local \_privateVar = -99  
  
 local \_iAmPrivateToo = {}   
 \_iAmPrivateToo.p1 = "p1"  
 \_iAmPrivateToo["p2"] = "p2"  
 \_iAmPrivateToo[1] = "one"  
 \_iAmPrivateToo[2] = "two"  
 \_iAmPrivateToo.tbl = {x = 1, y = 2}  
  
 -- private can access stuff in this closure  
 local privateFunction = function()   
 return "privateFunction::\_privateVar = "..\_privateVar.." and passedIn = "..passedIn  
 end  
  
 -- public interface   
 return {  
  
 -- I can't figure out how to access this from a   
 -- function inside the same table  
 publicVar = 2,  
  
 publicFunction = function()  
  
 print("\_privateVar = "..\_privateVar) --\> -99  
 print("passedIn = "..passedIn) --\> "hello"   
 print("\_iAmPrivateToo[2] = ".. \_iAmPrivateToo[2]) --\> "two"  
 print("\_iAmPrivateToo.tbl.y = ".. \_iAmPrivateToo.tbl.y) --\> 2  
  
 print(privateFunction()) --\> "privateFunction::\_privateVar = -99 and passedIn = hello"   
  
 -- QUESTION: How can I access "publicVar" from a function in this table?  
 print("publicVar = "..tostring(publicVar)) --\> Always prints "nil" why?  
  
 end  
  
 }  
  
end  
  
local myShaun = MakeShaun("hello");  
  
myShaun:publicFunction()   
print("myShaun.publicVar = "..myShaun.publicVar) --\> 2  
myShaun.publicVar = "Carlos"   
print("myShaun.publicVar = "..myShaun.publicVar) --\> Carlos  
  

[import]uid: 11196 topic_id: 5474 reply_id: 305474[/import]

Hmmm, I don’t think you will be able to do it that way. I think your problem is that publicVar and your function are both elements (and peers) in your table. When you define publicVar=2, this is simply creating a new entry in your table with the key ‘publicVar’ and assigning it the value 2. This does not imply anything about scoping or visibility when you create your function. If you needed to access this variable from your function, you would need to access it through the table, e.g. myTable.publicVar, but since the table is anonymous and you are in the middle of creating it, you can’t do this.

The simplest answer is that you need a variable declared before you start creating the table.
[import]uid: 7563 topic_id: 5474 reply_id: 18663[/import]

you need to rethink your structure

have a look at some of these discussions on OOP in Lua:

http://developer.anscamobile.com/forum/2010/11/09/modules-vs-classes
http://developer.anscamobile.com/forum/2010/11/05/structuring-games-without-oop
http://developer.anscamobile.com/forum/2011/01/21/oop-game-classes-objects-inheritance-methods-properties
http://developer.anscamobile.com/forum/2011/01/19/big-refactor
http://developer.anscamobile.com/forum/2010/11/09/display-objects-custom-component-question
http://developer.anscamobile.com/forum/2010/11/09/can-someone-help-me-create-oop-module-please

regards
J [import]uid: 6645 topic_id: 5474 reply_id: 19851[/import]