Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Tables within classes

edited August 2014 in General Posts: 142

Can you have a table specific to a class like you can with a variable? E.g.

function Ball:init(d)
Self.diameter = d

If so how would you implement it?


  • Posts: 1,595

    Self.tbl = {}

  • edited August 2014 Posts: 142

    @Luatee do you have to put it in the parenthesis in the init function?
    EDIT: sorry stupid question!

  • dave1707dave1707 Mod
    edited August 2014 Posts: 9,804

    @Staples Shows 2 ways to create the class table. Pass it a table (tab1) or create the table (tab2) in the class.

    function setup() t1={1,2,3,4,5} t=tab(t1) print("tab1") for a,b in pairs(t.tab1) do print(b) end print("tab2") for a,b in pairs(t.tab2) do print(b) end end tab=class() function tab:init(t1) self.tab1=t1 self.tab2={6,7,8,9} end
Sign In or Register to comment.