Skip to main content
gamedev.net
🔒 Locked

Lua modularity

Started by jeroenb Apr 12, 2005 at 8:00 AM 1 replies 1.6k views
Original Post
jeroenb
jeroenb
I was wondering if it is possible with Lua to create modules. What I mean is: I want to create a script library with functions which then can be included in other lua files. For instance I want to create a script with all kinds of AI functions which I can call from all kinds of creature scripts. Unfortunately I can't find any documentation about this. Can anyone help me with this?
Crafter 2D: the open source 2D game framework ?Github: https://github.com/crafter2d/crafter2d
Twitter: [twitter]crafter_2d[/twitter]
Simian Man
Simian Man
I am no expert on Lua but you can have one file say common.lua that contains a lot of functions in it. Then, when you're initializing your program, you can register them all with your lua_state* as follows:

lua_State* vm = lua_open( );
lua_dofile( vm, "common.lua" );

Now any other functions or files executed in reference to this lua_State* should have all of the functions defined in common.lua defined.

Hope this helps.
jeroenb
jeroenb
Yes, it helps a bit. Though it is not exactly what I want to achive. I want to let a user create his/her own library of functions. But I think that I found it. By adding: require "filename" to the top of the lua script script, you can include other lua files and thus create something like modularity :-).

Thanks anyway!
Crafter 2D: the open source 2D game framework ?Github: https://github.com/crafter2d/crafter2d
Twitter: [twitter]crafter_2d[/twitter]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.