lua-users home
lua-l archive

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


Well, the only thing I dislike about the solution below is that SubMenu1 is effectively split in two parts. If the Menu table is a page long it would be easy to miss that <SubMenu1["Back"] = Menu> while browsing through the file. What if there were 50 sub menus? All of them would be split in at least (but not limited to) two parts and the file can become a real mess with time. On the other hand, if all menus were post processed at the end of the script so that the names are resolved to objects the readability would not suffer. Granted, one would have to write 10 lines long function to do this name resolution and then run it, but that's what I would do to preserve the file readability and continuity of menu records.

AB



-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br [mailto:lua-bounces@bazar2.conectiva.com.br]On Behalf Of Virgil Smith
Sent: Tuesday, October 14, 2003 9:44 AM
To: 'Lua list'
Subject: RE: Forward Declaration

Sigh, this answer has been given twice and is needlessly roundabout.
Forward referencing is a problem for systems that use strictly closed declarations (not at all like Lua and tables).  You can add a table entry at any time.

The direct answer has been given once, but I will repeat it with slightly more explanation.
Here it is...
---------
SubMenu1 =
{
        ["New Campaign"] = ID_CAMPAIGN,
        ["New Random Map"] = ID_RANDOMMAP,
};

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

SubMenu1["Back"] = Menu
---------

Note that the first table construction/assignment statement creates a table and assigns a reference to it into SubMenu1. The second assigns a reference to the same table value that is referenced by SubMenu1 (inside a new table).  The last statement adds an entry to the table referenced by SubMenu1 (and Menu.New).  This new entry happens to be a reference to the table referenced by Menu.  This of course makes for a circular reference (Menu == Menu.New.Back), however, this is not necessarily a problem since Lua uses a mark and sweep garbage collection system.  Of course if you are using anything that "blindly" recurses tables (such as a naive lazy copy or serializer mechanism) it will have a problem with this.

-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br
[mailto:lua-bounces@bazar2.conectiva.com.br]On Behalf Of Bilyk, Alex
Sent: Tuesday, October 14, 2003 11:04 AM
To: Lua list
Subject: RE: Forward Declaration


Use "Menu" instead of Menu. Then, at run time, you can resolve the name "Menu" to the object under it.

        ["Back"] = "Menu"                      <-- No problem :)

-----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.