Access flag value from one class to another class

How can i save a value in a class and pass it to another class in corona?

I want to access ishold parameter outside the class A? Everything works fine on A, value turns true/false whenever objects is clicked.

I want to acces it in class B, but when i print values out comes that classB buttonish gets called before buttonish class A, so value never gets saved and it turns always false (value that is in the constructor A)

Any idea please?

Class A.lua

local a={} local a\_mt={\_\_index=card} function a.new(x,y) local a\_new={} a\_new.ishold=false a\_new.x=x a\_new.y=y a\_new.tap\_event\_listener=nil -- view. a\_new.view=display.newGroup() a\_new.view.x=a\_new.x a\_new.view.y=a\_new.y return setmetatable(a\_new, a\_mt) end -- listen to the group. function a:buttonish() local function event\_listener(event) if (self.ishold == true) then self.ishold = false self.view.y = 300 else self.ishold = true self.view.y = 257 end end self.tap\_event\_listener=event\_listener self.view:addEventListener("tap", event\_listener) end

Class B.lua

local a\_t=require("a") local b={} function b.new(x,y) b.a\_index=0 b.a\_list={} for i=1,5 do table.insert(b.a\_list, a\_t.new(x, y)) end end function b.a\_buttonish(a\_index) b.a\_index=a\_index -- as a button index reference. b.a\_list[a\_index]:buttonish() end

Class Main.lua

local b\_t=require("b") main.b=b\_t.new(150, 300) -- initialize for index=1,5 do b\_t.a\_buttonish(index) end

@blablu1212,

  1. I noticed recently,  you’ve been asking a lot of ‘class’ questions.  Is this for school?  If not, I’d suggest not trying to force OOP behavior on LUA and Corona via metatables, etc.  You’d be better served understanding how to make basic modules.
  • If you’re really interested in learning OOP practices, I’d learn C (to get a grounding in the base language), then focus on C++ (not C# or objective C).
  • LUA does’t really support true classes/OOP.  Some will argue, but I’m saying this from the perspective of someone who learned OOP using C++ 
  • Note: I’m all for learning, but in this case, if you want to learn Corona, learn about making games, and learn OOP you might be pressing a bit hard.  Also, if you start off learning basic module design, that will itself lead to a better understanding of the other principles you’re trying to accomplish via metatable manipulation.
  1. We talked about having two modules require each other in this thread (StarCrunch’s suggestion of the lazy require is awesome and I now use it regularly): 

http://forums.coronalabs.com/topic/56136-two-modules-require-each-other/

PS - Not trying to discourage you, but help you stay on a path to achieving understanding of Corona and game making with it.

Thank you @roaminggamer,

Sorry for my 'naive 'questions on the forum.

The project i am working on it’s build using oo corona, so i need to follow that structure.

I come from oo background, but i am finding it difficult to adapt that in lua.

I think problem i am having it’s because i want to access a global var inside a local function outside of that class.

-- listen to the group. function a:buttonish() local function event\_listener(event) ............. end self.tap\_event\_listener=event\_listener self.view:addEventListener("tap", event\_listener) end

If i make event_listener function outside, i don’t know how to call it anymore:

local function a:event\_listener(event) ............ end function a:buttonish() self.tap\_event\_listener=event\_listener self.view:addEventListener("tap", self:event\_listener() ) //??how to call a:event\_listener(event) end

Thanks in advance

Hi.  To be clear, I didn’t use the word naive, which I think would be taken as condescending.  I mean no condescension implied or otherwise in my responses.

As far as OOP and Lua.  I don’t personally thing they are a good fit.  Some will disagree, and that’s fine.  However, if you must proceed as you are, let’s address that.

First, I’m confused by this statement:

I think problem i am having it’s because i want to access a global var inside a local function outside of that class.

Globals, by their very nature should pre s ent few if any scope or visibility challenges.  They have global scope and are visible as soon as they are defined (in Lua there is no Global declaration; only locals can be delcared).

I think what you’re really struggling with the visibility in code which is defined in two different places or files.

Second, What you’re trying to do with that second bit of code is just not not right.  However if ‘a’ in both cases REALLY are the same object, then do this:

local function a:tap( event ) -- self implied because of : end function a:buttonish() self:addEventListener("tap") end

However, my guess is this won’t work because the implied arrangement (names in your code) don’t match the true arrangement.  

If you want an example of OOP  and button code try this: 

@blablu1212,

  1. I noticed recently,  you’ve been asking a lot of ‘class’ questions.  Is this for school?  If not, I’d suggest not trying to force OOP behavior on LUA and Corona via metatables, etc.  You’d be better served understanding how to make basic modules.
  • If you’re really interested in learning OOP practices, I’d learn C (to get a grounding in the base language), then focus on C++ (not C# or objective C).
  • LUA does’t really support true classes/OOP.  Some will argue, but I’m saying this from the perspective of someone who learned OOP using C++ 
  • Note: I’m all for learning, but in this case, if you want to learn Corona, learn about making games, and learn OOP you might be pressing a bit hard.  Also, if you start off learning basic module design, that will itself lead to a better understanding of the other principles you’re trying to accomplish via metatable manipulation.
  1. We talked about having two modules require each other in this thread (StarCrunch’s suggestion of the lazy require is awesome and I now use it regularly): 

http://forums.coronalabs.com/topic/56136-two-modules-require-each-other/

PS - Not trying to discourage you, but help you stay on a path to achieving understanding of Corona and game making with it.

Thank you @roaminggamer,

Sorry for my 'naive 'questions on the forum.

The project i am working on it’s build using oo corona, so i need to follow that structure.

I come from oo background, but i am finding it difficult to adapt that in lua.

I think problem i am having it’s because i want to access a global var inside a local function outside of that class.

-- listen to the group. function a:buttonish() local function event\_listener(event) ............. end self.tap\_event\_listener=event\_listener self.view:addEventListener("tap", event\_listener) end

If i make event_listener function outside, i don’t know how to call it anymore:

local function a:event\_listener(event) ............ end function a:buttonish() self.tap\_event\_listener=event\_listener self.view:addEventListener("tap", self:event\_listener() ) //??how to call a:event\_listener(event) end

Thanks in advance

Hi.  To be clear, I didn’t use the word naive, which I think would be taken as condescending.  I mean no condescension implied or otherwise in my responses.

As far as OOP and Lua.  I don’t personally thing they are a good fit.  Some will disagree, and that’s fine.  However, if you must proceed as you are, let’s address that.

First, I’m confused by this statement:

I think problem i am having it’s because i want to access a global var inside a local function outside of that class.

Globals, by their very nature should pre s ent few if any scope or visibility challenges.  They have global scope and are visible as soon as they are defined (in Lua there is no Global declaration; only locals can be delcared).

I think what you’re really struggling with the visibility in code which is defined in two different places or files.

Second, What you’re trying to do with that second bit of code is just not not right.  However if ‘a’ in both cases REALLY are the same object, then do this:

local function a:tap( event ) -- self implied because of : end function a:buttonish() self:addEventListener("tap") end

However, my guess is this won’t work because the implied arrangement (names in your code) don’t match the true arrangement.  

If you want an example of OOP  and button code try this: