Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat
Object and parameter passing - Gideros Forum

Object and parameter passing

CyberienceCyberience Member
edited February 2013 in General questions
Ok, somthing simple, language based. I have an object, with self.x and self.y inside.
I dont want to add them as paramters to the nitialisation, but can I pass the values at the initialisation stage?
like this?

MovieSprite.new("explode1",25,1,0,{x=100,y=100})

Now this doesn't work, so is there a way? or do I just create the parameters and ignore them if not used?
REAL programmers type copy con filename.exe
---------------------------------------

Comments

  • zvardinzvardin Member
    Accepted Answer
    No, you could either create parameters (or one table parameter and handle it) or add it to the object after.
    function MovieSprite:init(str,x,y,z,attributes)
       for k,v in pairs(attributes) do
          self[k] = v
       end
    end
    The best approach really depends on what you're trying to accomplish though. There is also a special param you can use "..." to mean a variable number of parameters. http://www.lua.org/pil/5.2.html

    Likes: Cyberience

    +1 -1 (+1 / -0 ) Share on Facebook
  • techdojotechdojo Guru
    Accepted Answer
    @zvardin's approach is correct, just use one of your :init() parameters as a table and you can then pass in as little or as much as you like.

    You can then use code like
    function MovieSprite:init(str,x,y,z,attributes)
       attributes = attributes or {}
       local ax = attributes.x or "some default value"
       local ay = attributes.y or "some default value"
        ...
        ...
        ...
     
    end
    To automatically check (and adapt to) any parameters not being specified either via the function call OR the supplied attribute table.

    Likes: Cyberience

    WhiteTree Games - Home, home on the web, where the bits and bytes they do play!
    #MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
    +1 -1 (+1 / -0 ) Share on Facebook
Sign In or Register to comment.