• self in pcall?
    8 replies, posted
Is there a way to call a function with pcall that has self in it? I mean: [LUA] table = {} function table:a() PrintTable( self ) end pcall( table["a"] ) [/LUA]
[lua]pcall( table.a, table );[/lua]
[QUOTE=Nevec;25107142]pcall( table.a, table );[/QUOTE] I already known that but I need the index to be a string. So it cant be: a.a it must be a["a"] . I guess it can be done with CompileString. Or is it a too slow function for a Think hook?
[QUOTE=MDave;25107231]I already known that but I need the index to be a string. So it cant be: a.a it must be a["a"] . I guess it can be done with CompileString. Or is it a too slow function for a Think hook?[/QUOTE] [lua] pcall(table["a"],table) [/lua]
[I]table.a[/I] is the same as [I]table["a"][/I]. A function declaration like this: [lua]function table:func( ... )[/lua] Is the same as: [lua]function table.func( self, ... )[/lua] Passing the table as the second argument to pcall is what you want.
[QUOTE=ralle105;25107262]pcall(table["a"],table) [/QUOTE] Console: [CODE] ] lua_run PrintTable( { pcall( table["a"] , table ) } ) > PrintTable( { pcall( table["a"] , table ) } )... 1 = false 2 = [@lua\includes\util.lua:35] bad argument #1 to 'pairs' (table expected, got nil) [/CODE] :colbert:
[QUOTE=MDave;25107296]Console: [CODE] ] lua_run PrintTable( { pcall( table["a"] , table ) } ) > PrintTable( { pcall( table["a"] , table ) } )... 1 = false 2 = [@lua\includes\util.lua:35] bad argument #1 to 'pairs' (table expected, got nil) [/CODE] :colbert:[/QUOTE] [lua] table = {} function table:a() PrintTable(self) end pcall(table["a"], table) [/lua]
Don't, just don't declare an empty global table called "table". PLEASE. You're practically removing all table.* functions by doing that. [lua]local tbl = {} function tbl:a() print( self ) end pcall( tbl.a, tbl ) pcall( tbl["a"], tbl )[/lua] Both work.
[QUOTE=raBBish;25107379]Don't, just don't declare an empty global table called "table". PLEASE. You're practically removing all table.* functions by doing that. .[/QUOTE] I just made a quick example and THEN I realised that. Anyway thanks for the help my problem solved!
Sorry, you need to Log In to post a reply to this thread.