1

I'm trying to embed Lua 5.2 into my C program.

I want to let the Lua script to be able to require and load another script. How to do it?

Suppose the Lua part has such file structure:

lua_script
  - main.lua
  + utils
    - custom_loader.lua
  + globals
    - scene_globals.lua
    - scene_levels.lua

The main.lua will try to require and import functions from custom_loader.lua, and so on...

Is it possible to do this without writing a kind-of wrapper in C? Can the Lua script automagically just load everything it needs?

(Ps. I don't really need sandboxing for now, so it's okay for the script to do what it want.)

4
  • I think require is just a function that you can define as you please. Commented Jan 13, 2017 at 12:43
  • @KerrekSB so, I should just push a C function as require and do luaL_loadfile() for every files that the script want? Commented Jan 13, 2017 at 12:47
  • Yes, one possibility is to write your own searcher system that resolves require calls. But there may be simpler solutions using built-in facilities. Commented Jan 13, 2017 at 12:50
  • require('lua_script.utils.custom_loader') Commented Jan 16, 2017 at 13:52

1 Answer 1

2

As decribed by the Lua require man page, it searches for the file in a path.

This path can be defined in C. Have a look to this post : "Setting the global LUA_PATH variable from C++/C"

The require function is very pratical to load modules and libraries defined in .lua files.

Sign up to request clarification or add additional context in comments.

1 Comment

@Tito: yes I think it does.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.