lua-users home
lua-l archive

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

2007/5/31, RJP Computing <>:
I am new so please be excuse this question if it is simple. I am trying to
create a module in lua that has a function that registers a table in a,
local to that module, table. I am having a problem getting the table that is
passed into the register function to actually be added in the index of the
table. I think an example will make this question a bit easier to see.

<widgetbase.lua - Base module that a bunch of other files will use to
register there existence>
-- import dependencies
local table = require( 'table' )
local print = print
local pairs = pairs
local type = type

-- Create a new module
module( ... )

-- Table of all the registered widgets.
-- This can be used later to iterate through them and pick out the
-- properties of interest.
registeredWidgets = {}

-- Returns the registered widgets container.
-- @return Table that contains all the registered widgets.
function GetRegisteredWidgets()
    return registeredWidgets

-- Registers the widget with the common table, that will contain all the
-- widgets and their properties.
-- This is one big table that all the widgets  and there inherited classes
will be in.
-- The key is the widget's name.
-- @param name The name of the table that will hold all the widgets
-- @param widget This is a table containing the total widget after all items
have been created.
function RegisterWidget( name, widget )
    registeredWidgets[name] = widget

<common.lua - Here is a lua file that uses the RegisterWidget()>
-- Include the module "WidgetBase" to make this an official widget.
WidgetBase = require( "widgetbase" )

-- Create a table that will hold all the properties.
local window = {}

-- Add the properties to the widget.                = WidgetBase.wxID_ANY
window.pos                = { x = -1, y = -1 }
window.size            = { width = -1, height = -1 }
window.minimum_size    = { width = -1, height = -1 }
window.maximum_size    = { width = -1, height = -1 }

-- Register the widget with the Lua engine after all the properties have
been created.
WidgetBase.RegisterWidget( "window", window )

<test.lua - Now to test all this>
-- Load the base classed first.
dofile( "common.lua" )

-- Lets see if we can get at the list of registered widgets.
print( "---- registeredWidgets ----" );
for name, var in pairs( WidgetBase.GetRegisteredWidgets() ) do
    print( name, "\t", val );
print ("---- END - registeredWidgets ----\n\n");

--- OUTPUT ---
---- registeredWidgets ----
-- wxWindow                        nil
-- wxButton                        nil
---- END - registeredWidgets ----

Sorry for the large example, but I am really stuck. I didn't expect the
'nil' for the register "name". Can somebody please help?

pairs cannot return nil for either the key or the value, so the
problem has to be in your test loop.

As you can see, you wrote "for name, var in pairs(", with a variable
called var, with an R. Inside the loop, you print the content of the
variable val, with an L, which is nil since it's never used anywhere