lua-users home
lua-l archive

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


Fabio Cecin:
>In Lua code (Lua 4.0), I've tried to do the following:
>  function ent_wall()
>      -- do stuff...
>   end
>
>   function entity_spawn(name)
>      args = {}
>      call( "ent_" .. name, args) -- ERROR
>   end
>
>   entity_spawn("wall")
>
> result is:
>     "Error: attempt to call a string value."
>
> Of course, what this should have accomplished is:
>     call(ent_wall, args)
>
> So, how do I get a "handle" to function "funcname", having it's (string)
> name? Like:
>    handle = get funcion by name ("ent_wall")
> call(handle, args)  -- call global function "ent_wall"

Well, you could just try looking it up directly in the global table. So:
    handle = globals()["ent_" .. name]

or perhaps by doing an evaluation, such as:
    handle = dostring("return " .. "ent_" .. name)

or (if you wish to stay compatible with Lua5 which uses "_G" instead of
"globals()" and doesn't seem to have "dostring()"):
    handle = loadstring("return " .. "ent_" .. name)()

*cheers*
Peter Hill.