Jump to content

  • Log In with Google      Sign In   
  • Create Account


Lua: A return keyword followed by a table


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Michael Wojcik   Members   -  Reputation: 163

Like
0Likes
Like

Posted 23 August 2012 - 04:32 PM

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, 23 August 2012 - 04:59 PM.

Generalist Game Developer and Cofounder at Voidseer Realms

Sponsor:

#2 JTippetts   Moderators   -  Reputation: 8208

Like
3Likes
Like

Posted 23 August 2012 - 05:01 PM

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.

#3 Michael Wojcik   Members   -  Reputation: 163

Like
0Likes
Like

Posted 23 August 2012 - 05:13 PM

Perfect explanation! Fast reply! Thanks!! Posted Image

Edited by Michael Wojcik, 23 August 2012 - 05:21 PM.

Generalist Game Developer and Cofounder at Voidseer Realms




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS