lua-users home
lua-l archive

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


----- Original Message -----
From: "Speight.Marcus" <marcus.speight@vivid-gaming.com>
To: "LUA (E-mail)" <lua@bazar2.conectiva.com.br>
Sent: Tuesday, October 14, 2003 7:51 AM
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.

Hello,

    I made the following script based on the answer of others as an exercize
to me:

updateMenus is a function that resolves references of strings to tables.
you need create the menus as:
[atable].[menutable]
and then pass [atable] to updateMenus.
Each time you need a reference to a fowarded table you write as a string.
I imagine this can help you.

                Thanx,

                                            Leandro.

teste.lua:

function updateMenus(menustbl)
        function updateMenu(tbl)
                for k, v in tbl do
                        if type(v) == type("") then
                                tbl[k] = menustbl[v]
                        end
                        print("=====")
                        print("K: ",k,"V: ",v)
                        print("K: ",k,"tbl[k]: ",tbl[k])
                end
        end
        for k, v in menustbl do
                updateMenu(v)
                _G[k]=v
        end

end

ID_CAMPAIGN = 1
ID_RANDOMMAP = 2
ID_LOADGAME = 3
ID_EXIT = 4

__menus = {}

__menus.SubMenu1 =
          {
                ["New Campaign"] = ID_CAMPAIGN,
                ["New Random Map"] = ID_RANDOMMAP,
                ["Back"] = "Menu" --< [NO MORE] Problem
          };

__menus.Menu =
        {
                ["New"] = "SubMenu1",
                ["Load Game"] = ID_LOADGAME,
                ["Exit"] = ID_EXIT
        };

updateMenus(__menus)
print()
print("SubMenu1: ", SubMenu1 )
print("=========")
for k,v in SubMenu1 do
        print("K: ",k," V: ",v)
end

print()
print("Menu: ", Menu )
print("=====")
for k,v in Menu do
        print("K: ",k," V: ",v)
end