I Need Lua Metatable Tutorials for NOOBs

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
sphyrth
Party member
Posts: 260
Joined: Mon Jul 07, 2014 11:04 am
Contact:

I Need Lua Metatable Tutorials for NOOBs

Post by sphyrth »

This question is mostly directed to Sir_Silver since he seems to use it a lot.

I want to harness the power of Lua Metatables, but I'm having a problem with the learning curve. Any links to good tutorials out there?
hamberge
Prole
Posts: 25
Joined: Wed Aug 16, 2017 2:55 pm

Re: I Need Lua Metatable Tutorials for NOOBs

Post by hamberge »

There is a learning curve but it is only a small hump, I would say.

You could read the manual:
https://www.lua.org/manual/5.1/manual.html#2.8

Here is a tutorial:
http://nova-fusion.com/2011/06/30/lua-m ... -tutorial/

This Youtube vid is interesting:
https://www.youtube.com/watch?v=aKe6YncHVAo

Do you have any specific questions?
sphyrth
Party member
Posts: 260
Joined: Mon Jul 07, 2014 11:04 am
Contact:

Re: I Need Lua Metatable Tutorials for NOOBs

Post by sphyrth »

I need something like ScriptGuilder's tutorial. That video got me started as to knowing what a metatable is. It's just too bad that he seems to be on a hiatus.

A comparison between Using Purely Normal Tables vs. Metatables might be a good start.
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: I Need Lua Metatable Tutorials for NOOBs

Post by ivan »

What do you plan to use the metatables for?
If it's for OO, you might want to check out my minimalist lib:
https://github.com/2dengine/oo.lua
Last edited by ivan on Wed Dec 15, 2021 11:33 am, edited 1 time in total.
sphyrth
Party member
Posts: 260
Joined: Mon Jul 07, 2014 11:04 am
Contact:

Re: I Need Lua Metatable Tutorials for NOOBs

Post by sphyrth »

No specific purpose. I just felt the need to learn it after watching the Entity Component System, and seeing Sir_Silver's usage of metatables in his Game Creations.
hamberge
Prole
Posts: 25
Joined: Wed Aug 16, 2017 2:55 pm

Re: I Need Lua Metatable Tutorials for NOOBs

Post by hamberge »

A purely normal table is just a store for data and functions. You access the data and functions directly via the "." operator.

A metatable is a table that you use to set the behavior of another table. Like "normal" tables, metatables have properties that you access with the "." operator. However, they must also include at least one special method called a metamethod. There is a limited number of these metamethods and they are specified in the lua manual. All such methods have a name that begins with a double underscore.

The most important ones, in terms of changing the functionality of a table, as opposed to just making your life easier as a programmer, are probably __index(), __newindex(), and __call(). __index() specifies what happens when you attempt to access a property of your table that does not exist (which normally returns nil) and may be the most commonly used metafunction. One common use for this is in replicating the behavior of object oriented programming, for which there are many guides on the internet. Note, __index can either be a method or a table. If it is a table, then when you try to access a key in your main table that isn't there, lua looks for that key in the __index table. __newindex() specifies what happens when you attempt to create a new index in your main table. __call() specifies what happens when you call your main table as a function. This is perhaps less useful than the other two as you can simply replicate this functionality with a function within your table.

To use a metatable, you need to set your main table's metatable to another with setmetatable(maintable, metatable). This function sets the metatable of maintable as metatable. Then, the metamethods of metatable will act on maintable. For example, if metatable has a __index, It will specify what happens when you try to access a property of maintable that isn't there.

One thing that can be confusing is that metatables can also act as normal tables. In this instance, such a table really serves two independent purposes, with the metamethods providing metatable functionality for another table and with the properties that are not metamethods simply acting as normal table properties for the table used as a metatable for another table. One thing that's even more confusing is when a metatable has its __index pointed to itself. This serves no purpose when accessing the metatable directly, but when you set it as a metatable for another table, when that other table attempts to access its own property that isn't there, it goes to the __index table, which is also its metatable. Lua is confusing like that. Understanding this might be the biggest part of the learning curve and why I say there is one but it's small.

My recommendation is to just search for lua __index() in youtube. Learning what __index() does will probably help you understand metatables in general.

Here is a nice tutorial that explains this stuff.
http://lua-users.org/wiki/MetamethodsTutorial
sphyrth
Party member
Posts: 260
Joined: Mon Jul 07, 2014 11:04 am
Contact:

Re: I Need Lua Metatable Tutorials for NOOBs

Post by sphyrth »

Hello! I'm here for an update that after reading and watching through those tutorials, I'm still slowly trying to understand it. The only thing that I learned is the concept of "inheritance".

But let's just give an examples of how I currently do things with a shape "class" for example:

Using a Metatable

Code: Select all

shape = {}
-- 1. Setting up Metatable
shape.mt = {}
shape.mt.__index = square.mt

-- 2. Giving Default Values
shape.mt.corners = 3

setmetatable(shape, shape.mt)

-- 3. Using __call as an Instantiation Function
function shape.mt:__call(corners)
	local s = setmetatable({}, shape.mt)
	s.corners = corners
	return s
end

-- Some random function
function shape.mt:move()
end

-- Instantiating and using its function
square = shape(4)
square:move()
Without Using a Metatable

Code: Select all

shape = {}

function shape:new(corners)
   local s = {}
   s.corners = corners and corners or 3

   function s:move()
   end

   return s
end

square = shape:new(4)
square.move()
Now, since I have yet to grasp the full power of using Metatables, I currently think that using Normal Tables is better since it's simpler.

What do you guys think? Should I use Normal Tables for simple stuffs like the example, or should I keep using the Metatable Example just to get the hang of things?
Post Reply

Who is online

Users browsing this forum: No registered users and 61 guests