lua-users home
lua-l archive

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


This is untested code:

----- code -----

local chunkCache = {}

function callChunk( name )
    local chunk = chunkCache[ name ]

    if( not chunk ) then
        local msg

        chunk, msg = loadfile( name )

        if( msg ) then error( msg ) end

        chunkCache[ name ] = chunk
    end

    local success

    success, msg = pcall( chunk )

    if( not success ) then error( msg ) end
end

----- end code -----

> -----Original Message-----
> From: lua-bounces@bazar2.conectiva.com.br 
> [mailto:lua-bounces@bazar2.conectiva.com.br] On Behalf Of Jose Marin
> Sent: Thursday, February 05, 2004 4:34 AM
> To: Lua list
> Subject: Calling a chunk by file name
> 
> 
> Hello.
> 
> How do I call a compiled chunk by the original file
> name?
> 
> For example, I have the files:
> 
> Enemy1.lua
>   some code..
>   
> 
> Enemy2.lua
>   some code..
> 
> 
> etc...
> 
> 
> I compile the files:
> 
> luac -o enemies.dat Enemy1.lua Enemy2.lua ...
> 
> Then, in C or Lua code, I would like to call the code
> in some archive, for example, Enemy1.lua.
> 
> I wouldn't like to create a function for each enemy,
> like this:
> 
> function Enemy1()
>   some code..
> end
>   
> 
> function Enemy2()
>   some code..
> end
> 
> 
> then call Enemy1().
> 
> 
> 
> 
> ______________________________________________________________________
> 
> Yahoo! GeoCities: 15MB de espaço grátis para criar seu web 
> site! http://br.geocities.yahoo.com/
>