lua help with classes

BlitzMax Forums/BlitzMax Programming/lua help with classes

Leon Drake(Posted 2007) [#1]
for some reason i cant get this to work using axe.lua mod

SimpleClass = {}
SimpleClass_mt = { __index = SimpleClass }

-- This function creates a new instance of SimpleClass
--
function SimpleClass:create()
    local new_inst = {x = 3}    -- the new instance
    setmetatable( new_inst, SimpleClass_mt ) -- all instances share the same metatable
    return new_inst
end

-- Here are some functions (methods) for SimpleClass:

function SimpleClass:className()
    print( "SimpleClass" )
end

function SimpleClass:doSomething()
    print( "Doing something" )
end



simple = SimpleClass:create()
simple:className()


in bmx using this for testing as i'm learning lua.

Import axe.lua

Function luaPrint( l:Byte Ptr )
    For Local i:Int = 1 To lua_gettop( l ) ' The lua stack goes from 1 to lua_gettop( l ) -- it does not start at zero
        Select lua_type( l, i )                                     ' gets the type of the variable
            Case LUA_TBOOLEAN
                Print lua_toboolean( l, i )                         ' true/false
            Case LUA_TNUMBER
                Print lua_tonumber( l, i )                          ' double
            Case LUA_TSTRING
                Print String.FromCString( lua_tostring( l, i ) )    ' print a string - lua_tostring is an auxiliary function
                                                                    ' you should use lua_tolstring in your own code for safety
            Default
                ' other types exist, but for the sake of not writing too much code, they're excluded
        End Select

	Print "i "+i
    Next
    Return 0        ' zero arguments are returned -- if you pushed anything on the stack, you'd specify the
                    ' amount of values you wanted to return
End Function

Local state:Byte Ptr = luaL_newstate( )         ' Create the state

lua_register( state, "print", luaPrint )    ' Register the function

lua_dofile( state, "luatest.lua" )          ' Execute a script

lua_close( state )                          ' Close the state

'Input "Done>"                               ' Pause




however if i use the initial Table it works, but not if i try to create a new instance

Working Lua script

SimpleClass = {}
SimpleClass_mt = { __index = SimpleClass }

-- This function creates a new instance of SimpleClass
--
function SimpleClass:create()
    local new_inst = {x = 3}    -- the new instance
    setmetatable( new_inst, SimpleClass_mt ) -- all instances share the same metatable
    return new_inst
end

-- Here are some functions (methods) for SimpleClass:

function SimpleClass:className()
    print( "SimpleClass" )
end

function SimpleClass:doSomething()
    print( "Doing something" )
end




SimpleClass:className()




Leon Drake(Posted 2007) [#2]
you know what,

calling
luaL_openlibs(state)

would make a big difference


Leon Drake(Posted 2007) [#3]
thank you split personality that is what the problem was