Jump to content
  • Advertisement
Sign in to follow this  
Michael Wojcik

Lua: A return keyword followed by a table

This topic is 2131 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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, Edited by Michael Wojcik

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!