Built-in Lua loading modules programmatically (C ++)

I embed Lua in a C ++ application.

I have some modules (currently simple .lua scripts) that I want to download programmatically when the engine starts, so when the engine starts, the module (s) is available / accessible for scripts without including them in "

To do this, I need to be able programmatically (i.e. the end of C ++) to ask the engine to load modules as part of the initialization (or shortly after).

Does anyone know how I can do this?

+3
source share
2 answers

The easiest way is to add and edit a copy linit.cfor your project.

+4
source

, : ++ Lua, Lua, !

// funky = require ("funky")
//
lua_getfield (L, LUA_GLOBALSINDEX, "require"); // function
lua_pushstring (L, "funky");     // arg 0: module name
err = lua_pcall (L, 1, 1, 0);
// store funky module table in global var
lua_setfield (L, LUA_GLOBALSINDEX, "funky");

// ... later maybe handle a non-zero value of "err"
// (I actually use a helper function instead of lua_pcall
// that throws a C++ exception in the case of an error)

, , ...:)

+3

Source: https://habr.com/ru/post/1781980/


All Articles