Advertisement

Lua: A return keyword followed by a table

Started by August 23, 2012 10:32 PM
1 comment, last by Michael Wojcik 12 years, 3 months ago
I recently obtained a lua file like this,


return {
file="Optimus.png",
height=62,
description={
family="Optimus",
style="Regular",
size=32
},
metrics={
ascender=46,
descender=-16,
height=62
},
texture={
file="IM:Textures:1:Textures/Optimus.png",
width=512,
height=256
},
chars={
{char=" ",width=13,x=1,y=39,w=0,h=0,ox=0,oy=0},
{char="!",width=10,x=2,y=9,w=8,h=30,ox=0,oy=30},
{char='"',width=20,x=11,y=9,w=17,h=11,ox=0,oy=30},
-- so on...
},
}


I don't see where the return keyword followed by the table would put the table in the global enviroment and can't seem to shed light on it from my lua book and googling. I am trying to parse it in luaj api and am in need of knowing where are the table is stored in the global enviroment after running the script. I do know that technically, a function in lua is a closure assigned to a variable. I would peronally just use the three tables as global variables here, but now I am quite curious what the original author did here.

Thanks,
Generalist Game Developer and Cofounder at Voidseer Realms
Each file in Lua is compiled as a chunk and executed as if it were an anonymous function. So the return statement essentially boils down to a single anonymous function that returns a table. The returned table contains 4 inner tables, named description, metrics, texture and chars.

If the above source file were named script.lua for example, and you executed the line t=dofile("script.lua") then after execution, the variable t would hold a reference to the table created and returned by the above. This is one trick for using Lua as a data description language.
Advertisement
Perfect explanation! Fast reply! Thanks!! biggrin.png
Generalist Game Developer and Cofounder at Voidseer Realms

This topic is closed to new replies.

Advertisement