[LUA] Include like in C

Started by
3 comments, last by Acharis 5 years ago

I have a big .lua file and I would like to split it into parts for convenience. Is there a way to make some sort of #include (like in C preprocessor?). I tried require and import but I'm getting "Attempt to call global 'require' (a nil value)".
 

Basically, I have something like the code below and I would like to split it into several files and then call some sort of "main.lua" which includes all those.


-- File 'variables.lua'
VAR_1=64; VAR_2=65;

-- File 'define1.lua'
defineSomething(VAR_1,"text");
defineSomething(VAR_2,"text");
defineSomething(VAR_1,"text");
...

-- File 'define2.lua'
defineSomething(VAR_2,"text");
defineSomething(VAR_2,"text");
defineSomething(VAR_1,"text");
...

-- File 'main.lua'

#include 'variables.lua';
#include 'define1.lua'
#include 'define1.lua'

 

Stellar Monarch (4X, turn based, released): GDN forum topic - Twitter - Facebook - YouTube

Advertisement
5 minutes ago, Acharis said:

I tried require and import but I'm getting "Attempt to call global 'require' (a nil value)".

require is the standard built-in way to gain access to other Lua files... You should probably look into why it's not working for you. Are you using modules or anything to do with global environments that could be hiding global variables?

If you want C's literal #include, you could run a standard C preprocessor over your Lua code like http://mcpp.sourceforge.net

So, that code should work, right?


-- File 'main.lua'

require 'variables.lua';
require 'define1.lua'
require 'define2.lua'

I don't think I use modules or anything fancy like that... My LUA knowledge is extremelly basic and I use LUA as a simple tool to call a few C++ functions (something like a config file).

Any clues where I should look? What theorethically could be the cause? Maybe require need some lua lib? I have two included: {"base", luaopen_base}, {"io", luaopen_io}.

 

Stellar Monarch (4X, turn based, released): GDN forum topic - Twitter - Facebook - YouTube

All right, it started to work after including *all* libraries: luaL_openlibs(L);
Any clue what library is the one that's needed for require? Since including them all is a bit of an overkill.

Stellar Monarch (4X, turn based, released): GDN forum topic - Twitter - Facebook - YouTube

This topic is closed to new replies.

Advertisement