lua-users home
lua-l archive

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



> On Behalf Of Luiz Henrique de Figueiredo
> Sent: January 28, 2003 10:40 AM
> To: Multiple recipients of list
> Subject: Re: Calling function by string name
> 
> >     call( "ent_" .. name, args) -- ERROR
> 
> Try
> 
>      call( getglobal("ent_" .. name), args)
> 
> But perhaps it's simpler to do
> 
>      ent = {}
> 
>      function ent.wall() ... end
>      function ent.door() ... end
> 
>      function entity_spawn(name)
> 	ent[name]()
>      end
> 
> --lhf


The advantage of doing it this was as well is you won't create a string
which has to be collected later.

Lua script:

name = "wall"
call(globals()["ent_" .. name])

luac generates the follow output:

main <0:@/tmp/wlzQPuia> (11 instructions/44 bytes at 0x8052af0)
0 params, 4 stacks, 0 locals, 5 strings, 0 numbers, 0 functions, 4 lines
     1	[2]	PUSHSTRING 	1	; "wall"
     2	[2]	SETGLOBAL  	0	; name
     3	[3]	GETGLOBAL  	2	; call
     4	[3]	GETGLOBAL  	3	; globals
     5	[3]	CALL       	1 1
     6	[3]	PUSHSTRING 	4	; "ent_"
     7	[3]	GETGLOBAL  	0	; name
     8	[3]	CONCAT     	2                ** New string **
     9	[3]	GETTABLE
    10	[3]	CALL       	0 0
    11	[3]	END

(from http://doris.sourceforge.net/lua/weblua.php).