Jump to content
  • Advertisement
Acharis

[LUA] Include like in C

Recommended Posts

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'

 

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Posted (edited)

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}.

 

Edited by Acharis

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!