lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Basically you want lazy evaluation, which you can use a function for,
but you have to avoid letting Lua make the table a closure value:

ID_CAMPAIGN,ID_RANDOMMAP = 1,2  -- just to avoid nils

SubMenu1 =
{
 	["New Campaign"] = ID_CAMPAIGN,
 	["New Random Map"] = ID_RANDOMMAP,
 	["Back"] = function(t) return t["Menu"] end
}

Menu =
{
 	["New"] = function(t) return t["SubMenu1"] end,
 	["Load Game"] = ID_LOADGAME,
 	["Exit"] = ID_EXIT
}

assert(SubMenu1==Menu.New(_G))
assert(Menu==SubMenu1.Back(_G))

for k,v in SubMenu1 do
 -- any contents that is a function we "lazily evaluate"
 if type(v)=="function" then
  print(k, v(_G))
 else
  print(k, v)
 end
end


You could put all your tables in a table called "menus" and pass this
instead of _G, which might be a little tidier.

Regards,
Nick


> -----Original Message-----
> From: Speight.Marcus [mailto:marcus.speight@vivid-gaming.com]
> Sent: Tuesday, October 14, 2003 3:51 AM
> To: LUA (E-mail)
> Subject: Forward Declaration
> 
> Hi,
> 
> I'm fairly new to LUA and hoped someone could answer a problem for me.
> 
> I have a menu system that loads an lua script. But I need a menu to
back
> reference.
> 
> e.g.
> 
> SubMenu1 =
> {
> 	["New Campaign"] = ID_CAMPAIGN,
> 	["New Random Map"] = ID_RANDOMMAP,
> 	["Back"] = Menu                      <-- Problem
> };
> 
> Menu =
> {
> 	["New"] = SubMenu1,
> 	["Load Game"] = ID_LOADGAME,
> 	["Exit"] = ID_EXIT
> };
> 
> I tried adding Menu = {}; at the beginning of the file but it did not
> help.
> How to I do this?
> 
> 
> Marcus Speight
> Software Engineer
> Vivid Gaming
> 
> The views expressed in this message are those of the sender and not
> necessarily those of IGT - UK Limited and its associated companies.
> 
>