Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 981795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:35:57+00:00 2026-05-16T04:35:57+00:00

I’m working on a small project trying to integrate lua with c++. My problem

  • 0

I’m working on a small project trying to integrate lua with c++.
My problem however is as follows:

I have multiple lua scripts, lets call them s1.lua s2.lua and s3.lua. Each of these has the following functions: setVars() and executeResults().

Now I am able to to call a lua file through LuaL_dofile and immediately after use setVars() and/or executeResults(). The problem here however is that after I load s2.lua I can no longer call the functions of s1.lua. This would mean I have to redo the LuaL_dofile on s1.lua to regain access to the function and by doing so I lose access to the functions in s2.lua.

Is there a way to simply load all lua files in a row, and afterwards start calling their functions at will? Something like s1->executeResults() s5->executeResults() s3->setVars() etc.

I currently already have a system in place using boost::filesystem to detect all lua files in a folder, I then save these files names in a vector and then simply iterate over the vector to load each lua file in a row.

Foregoing the filling of the vector with lua file names my plugin load function looks like this at the moment:

void Lua_plugin::load_Plugins(){
 std::vector<std::string>::const_iterator it;
 for (it=Lua_PluginList.begin(); it!=Lua_PluginList.end(); it++){
  std::cout<<"File loading: " << *it << std::endl;
  std::string filename =  *it;
  std::string filepath = scriptdir+filename;
  if (luaL_loadfile(L, filepath.c_str()) || lua_pcall(L, 0, 0, 0)) {
   std::cout << "ScriptEngine: error loading script. Error returned was: " << lua_tostring(L, -1) << std::endl;
  }
 }
}

To make it a bit more clear, all I have in the .lua’s is something like this:

-- s1.lua

setVars()
--do stuff
end

executeResults()
--dostuff
end

etc, but I would like to be able to call s1.lua’s setVars() and s2.lua’s setVars() after simply having loaded both in a row.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T04:35:57+00:00Added an answer on May 16, 2026 at 4:35 am

    This is effectively what gwell proposed using the C API:

    #include <stdio.h>
    
    #include "lua.h"
    
    static void
    executescript(lua_State *L, const char *filename, const char *function)
    {
        /* retrieve the environment from the resgistry */
        lua_getfield(L, LUA_REGISTRYINDEX, filename);
    
        /* get the desired function from the environment */
        lua_getfield(L, -1, function);
    
        return lua_call(L, 0, 0);
    }
    
    static void
    loadscript(lua_State *L, const char *filename)
    {
        /* load the lua script into memory */
        luaL_loadfile(L, filename);
    
        /* create a new function environment and store it in the registry */
        lua_createtable(L, 0, 1);
        lua_getglobal(L, "print");
        lua_setfield(L, -2, "print");
        lua_pushvalue(L, -1);
        lua_setfield(L, LUA_REGISTRYINDEX, filename);
    
        /* set the environment for the loaded script and execute it */
        lua_setfenv(L, -2);
        lua_call(L, 0, 0);
    
        /* run the script initialization function */
        executescript(L, filename, "init");
    }
    
    int
    main(int argc, char *argv[])
    {
        lua_State *L;
        int env1, env2;
    
        L = (lua_State *) luaL_newstate();
        luaL_openlibs(L);
    
        loadscript(L, "test1.lua");
        loadscript(L, "test2.lua");
    
        executescript(L, "test1.lua", "run");
        executescript(L, "test2.lua", "run");
        executescript(L, "test2.lua", "run");
        executescript(L, "test1.lua", "run");
    
        return 0;
    }
    

    Test scripts:

    -- test1.lua
    function init() output = 'test1' end
    function run() print(output) end
    
    -- test2.lua
    function init() output = 'test2' end
    function run() print(output) end
    

    Output:

    test1
    test2
    test2
    test1
    

    I omitted all error handling for brevity, but you’ll want to check the return value of luaL_loadfile and use lua_pcall instead of lua_call.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 529k
  • Answers 529k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This code does not even compile. Without warnings and strict… May 16, 2026 at 11:19 pm
  • Editorial Team
    Editorial Team added an answer As I was saying in a comment to another answer,… May 16, 2026 at 11:19 pm
  • Editorial Team
    Editorial Team added an answer if id field is an integer you do not have… May 16, 2026 at 11:18 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have just tried to save a simple *.rtf file with some websites and
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I want use html5's new tag to play a wav file (currently only supported
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.