4

how do you make a default table and then use it when making other tables?

example

--default table
Button = {
 x = 0,
 y = 0,
 w = 10,
 h = 10,
 Texture = "buttonimg.png",
 onClick = function() end
}

newbutton = Button {
 onClick = function()
  print("button 1 pressed")
 end
}


newbutton2 = Button {
 x = 12,
 onClick = function()
  print("button 2 pressed")
 end
}

newbuttons will get y, w, h and texture set to default value but anything set in the brackets get overwritten

1
  • You can't do that, you have to augment the Button table with the "dot" operator.. Button.x = <something> Commented Feb 6, 2009 at 3:08

2 Answers 2

4

You can achieve what you want by merging Doug's answer with your original scenario, like this:

Button = {
   x = 0,
   y = 0,
   w = 10,
   h = 10,
   Texture = "buttonimg.png",
   onClick = function() end
}
setmetatable(Button,
         { __call = function(self, init)
                       return setmetatable(init or {}, { __index = Button })
                    end })

newbutton = Button {
   onClick = function()
                print("button 1 pressed")
             end
}

newbutton2 = Button {
   x = 12,
   onClick = function()
                print("button 2 pressed")
             end
}

(I actually tested this, it works.)

Edit: You can make this a bit prettier and reusable like this:

function prototype(class)
   return setmetatable(class, 
             { __call = function(self, init)
                           return setmetatable(init or {},
                                               { __index = class })
                        end })
end

Button = prototype {
   x = 0,
   y = 0,
   w = 10,
   h = 10,
   Texture = "buttonimg.png",
   onClick = function() end
}

...
Sign up to request clarification or add additional context in comments.

Comments

0

If you set the new table's metatable's __index to point to Button it will use the default values from the Button table.

--default table
Button = {
 x = 0,
 y = 0,
 w = 10,
 h = 10,
 Texture = "buttonimg.png",
 onClick = function() end
}

function newButton () return setmetatable({},{__index=Button}) end

Now when you make buttons with newButton() they use the default values from the Button table.

This technique can be used for class or prototype object oriented programming. There are many examples here.

1 Comment

The brackets around __index are superfluous.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.