lua-users home
lua-l archive

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


The explanation that makes sense for me is this:

You correctly divined that the table did not exist when you referenced
it.  There are many solutions to handle this problem, and you should
choose the one that makes the most sense for you.

The problem you ran into is that defining a table like this:

A = { foo=1, bar=2 }

Creates an entirely new table - shown easily by this snippet:

A = {} -- trying to forward declare A
Print(A)
A = {foo=1,bar=2} -- trying to fill in fields of A
Print(A)

Notice A changes address values.  It's a whole new table now.

Because it defines a new table and binds A to its value.

One way around this is to not use the A = {...} syntax so that you are
only modifying an already existing table, like so:

A = {}

A.foo = 1
A.bar = 2

Now you are fine, but you have to forward-declare all your tables - or
else lua will complain that A is not defined if you just tried:

A.foo = 1
A.bar = 2

The other option is to store the name and look it up at run time when
you need it.  That works great and depending on your system may be the
way to go.  Its just late binding in a primitive form. :)

As for recursive table references, that's a whole other issue I won't
worry about discussing. :)

-mdm



-----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 10:03 AM
To: Lua list
Subject: RE: Forward Declaration

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.