lua-users home
lua-l archive

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


There are A LOT of things wrong here.  It's clear what you're trying to
do, but you're lacking some understanding of basic lua syntax.  Take
baby steps.

Here's a start:

Menu = {}

function Menu:new()
    local menu = setmetatable( {}, { __index = Menu } )
    menu.items = {}
    return menu
end

function Menu:add(name, id)
    local item = { name = name, id = id }
    table.insert( self.items, item )
    return self
end

mainMenu = Menu:new()
    :add("Item0", 0)
    :add("Item1", 1)

print( mainMenu.items[ 1 ].name )
print( mainMenu.items[ 1 ].id )


> 
> Hi,
> 	I tried to implement the following but came up with a 
> runtime error.
> 	I can't seem to be able to locate it can anyone help please?
> 
> function Menu.new()
>     local menu = {}
>     menu.size = 0
>     return menu
> end
> 
> function Menu:sub(name)
>     local submenu = Menu.new
>     self:add(name, submenu)
>     return submenu
> end
> 
> function Menu:add(name, id)
>     local item = {}
>     item[name] = id
>     self[self.size] = item
>     self.size = self.size+1
>     return self
> end
> 
> mainMenu = Menu.new
>     :sub "New"
>         :add("New Campaign", ID_CAMPAIGN)
>         :add("New Random Map", ID_RANDOMMAP)
>         :add("Back", ID_BACK)
>     :add("Load Game", ID_LOADGAME)
>     :add("Exit", ID_EXIT)
> 
> 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.
>