Metatables – ShiVa Engine

Metatables

Metatables in LUA are a kind of hashtable. You can associate a key with a value. Here, the value can be anything (number, string, object…).
This tutorial is only viable for games using the Lua script, it doesn’t work for games exported in native code, because it uses Lua elements.

--this declaration...
local myMetatable = {"value0", 32, hObject}
--...is the same as this declaration:
local myMetatable = {}
myMetatable[0] = "value0"
myMetatable[1] = 32
myMetatable[2] = hObject
--use metatables like this:
--------------------------------------------------------------------------------
function MyGame.getMonthName ( nMonth )
--------------------------------------------------------------------------------
    local month_of_year = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}
    return month_of_year[nMonth]
--------------------------------------------------------------------------------
end
--------------------------------------------------------------------------------



Need more answers?

  • slackBanner