• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

Lua question: metatable.

Status
Not open for further replies.
Level 11
Joined
Oct 11, 2012
Messages
711
Can anyone explain "metatable" in English? I have read the Lua 5.1 Manual and still don't get it. (I only know Jass)

Also, what does this mean?
JASS:
local x = {value = 5}

local mt = {
  __add = function (lhs, rhs) -- "add" event handler
    return { value = lhs.value + rhs.value }
  end
}

setmetatable(x, mt) -- use "mt" as the metatable for "x"

local y = x + x

print(y.value) --> 10

local z = y + y -- error, y doesn't have our metatable. this can be fixed by setting the metatable of the new object inside the metamethod

I especially don't understand this part because it looks much different than the hashtable in Jass:
JASS:
local mt = {
  __add = function (lhs, rhs) -- "add" event handler
    return { value = lhs.value + rhs.value }
  end
}
How come there is a function in a variable assignment? ...

Last, what does "handler" mean? Plain English and an easy understandable example would be great. Thank you.
 
Level 15
Joined
Oct 18, 2008
Messages
1,588
Also, what does this mean?
JASS:
local x = {value = 5}

local mt = {
  __add = function (lhs, rhs) -- "add" event handler
    return { value = lhs.value + rhs.value }
  end
}

The metatable has a default, predefined function name "__add". This is usually undefined itself (only the name is defined for use), and what happens here is that we define what it does.

The {} marks a block that contains every data linked to the metatable. You can consider it like a class, if you are familiar with OOP. The metatable is a class, and you define it's functions in the block.

JASS:
setmetatable(x, mt) -- use "mt" as the metatable for "x"
You bind your metatable to x, so if you add something to x, it looks at your x's metatable and tries to find the function "__add". Since you've defined it, now LUA knows how to add two values.

To give you an easier to understand example, let's take how you add two 3-dimensional vectors, A and B:
C=A+B
but how do you add those two? They contain 3 numbers each! Do you add all of them together, the subtract by the second vector's respective number? Or just multiply them? Your application doesn't know how to do it. So you have to tell it:
C.X = A.X+B.X
C.Y = A.Y+B.Y
C.Z = A.Z+B.Z

or, in LUA:
JASS:
local mt = {
  __add = function (lhs, rhs) -- "add" event handler
    return
    {
        X = lhs.X + rhs.X
        Y = lhs.Y + rhs.Y
        Z = lhs.Z + rhs.Z
    }
  end
}
The application then substitutes lhs and rhs (left-hand-side and right-hand-side) with the values. So if you type C = A + B, what actually happens is C.__add(A, B)
JASS:
local y = x + x

print(y.value) --> 10

local z = y + y -- error, y doesn't have our metatable. this can be fixed by setting the metatable of the new object inside the metamethod
Since y doesn't have a metatable, and it has a custom structure (obviously, LUA knows how to take the sum of two integers), it doesn't know what to do when you ask for their sum.

For an easier to understand tutorial, check here: http://nova-fusion.com/2011/06/30/lua-metatables-tutorial/
 
Level 11
Joined
Oct 11, 2012
Messages
711
JASS:
local x = {value = 5}

local mt = {
  __add = function (lhs, rhs) -- "add" event handler
    return { value = lhs.value + rhs.value }
  end
}

The metatable has a default, predefined function name "__add". This is usually undefined itself (only the name is defined for use), and what happens here is that we define what it does.

The {} marks a block that contains every data linked to the metatable. You can consider it like a class, if you are familiar with OOP. The metatable is a class, and you define it's functions in the block.

JASS:
setmetatable(x, mt) -- use "mt" as the metatable for "x"
You bind your metatable to x, so if you add something to x, it looks at your x's metatable and tries to find the function "__add". Since you've defined it, now LUA knows how to add two values.

To give you an easier to understand example, let's take how you add two 3-dimensional vectors, A and B:
C=A+B
but how do you add those two? They contain 3 numbers each! Do you add all of them together, the subtract by the second vector's respective number? Or just multiply them? Your application doesn't know how to do it. So you have to tell it:
C.X = A.X+B.X
C.Y = A.Y+B.Y
C.Z = A.Z+B.Z

or, in LUA:
JASS:
local mt = {
  __add = function (lhs, rhs) -- "add" event handler
    return
    {
        X = lhs.X + rhs.X
        Y = lhs.Y + rhs.Y
        Z = lhs.Z + rhs.Z
    }
  end
}
The application then substitutes lhs and rhs (left-hand-side and right-hand-side) with the values. So if you type C = A + B, what actually happens is C.__add(A, B)
JASS:
local y = x + x

print(y.value) --> 10

local z = y + y -- error, y doesn't have our metatable. this can be fixed by setting the metatable of the new object inside the metamethod
Since y doesn't have a metatable, and it has a custom structure (obviously, LUA knows how to take the sum of two integers), it doesn't know what to do when you ask for their sum.

For an easier to understand tutorial, check here: http://nova-fusion.com/2011/06/30/lua-metatables-tutorial/

Thank you so much! This helps a lot ! +Rep
 
Status
Not open for further replies.
Top