Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Question on Gideros classes — Gideros Forum

Question on Gideros classes

BigWinstonBigWinston Member
edited November 2012 in General questions
Hi There,

I'm new to Gideros, (but not development) and am giving it a test run... It seems quite sweet! I have a question on Gideros classes I'm hoping you all can help me on.

How do I create a non-static private member function (and non-static private members/variables)? I tried to do this in regular Lua a little while ago and could never figure it out. Everything I created as local would be static.

I have the following code - can someone point me in the right direction please? Is what I'm looking for possible with Gideros / Lua?




main_menu = Core.class(Sprite)

-- I WOULD LIKE THIS TO BE A PRIVATE, NON-STATIC MEMBER FUNCTION
function main_menu:drawBackground()
local bg = Bitmap.new(Texture.new("assets/gfx/bg1_fixed.png"))
self:addChild(bg)
end



function main_menu:init()
print ("main_menu - initialized...")

self:drawBackground()

-- add scene listners
self:addEventListener("enterBegin", self.onTransitionInBegin, self)
self:addEventListener("enterEnd", self.onTransitionInEnd, self)
self:addEventListener("exitBegin", self.onTransitionOutBegin, self)
self:addEventListener("exitEnd", self.onTransitionOutEnd, self)
end


Many Thanks!!!
Tagged:

Comments

  • I don't believe you can have truly private anything in Lua as every container (array, hash, object, class) is a table that can be queried for it's members.

    I don't really know how to answer the question about non-static member functions. If it is a "member" doesn't it mean it is static? Sorry, I never did learn that much cs theory so I don't know what you are trying to achieve.
  • OZAppsOZApps Guru
    Accepted Answer
    @BigWinston, in C/C++ the concept of Static determines the stack on which the code is placed and the scope of the function and/or variables.

    So in your quest to have them private, you will have to understand the way Lua works and the way Gideros works. Gideros loads or executes all of the files on start, so all the functions, etc are available. To have some functions/variables private as you want, here's a sample
     -- private.lua
    local function test()
      print("Test")
    end 
     
    function rest()
      print("Rest")
    end 
     
    function best()
      rest()
      test()
    end
      rest()
      print("--")
      best()
    You can access test from within the global functions but not test, which is only available from within the functions in that scope.

    Now you cannot assign local to any member functions as the scope is determined by the parent object. So in your case the drawBackground will have to be a non member function that can be passed the object to draw which will substitute the self object.
      local function drawBackground(self)
        local bg = Bitmap.new(Texture.new("assets/gfx/bg1_fixed.png"))
        self:addChild(bg)
      end 
     
      drawBackground(self)
    Hope that helps resolve this issue?
    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
  • Yes, thanks. I'm not in front of my development machine right now but will give what you suggest a try later today.

    For clarification, I was referring to the OOD classes usage of static and not the pure 'C' filescope description.

    In the past when I have tried to create a member variable within a Lua class implementation I could never get them to behave as a member of the class, they would always act as static - I.e. change in one instance and the value would be shared by all instances!

    I understand I'm not in my familiar C based language world now, which is fine but I might have to drop some of my overly anal styling views. Gonna be tough!! :)
  • Great stuff. The code in your last snippet did the trick.

    Thanks again!
Sign In or Register to comment.