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 8226435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:46:04+00:00 2026-06-07T15:46:04+00:00

im trying to call a lua function from C++ where function is in subtable

  • 0

im trying to call a lua function from C++ where function is in subtable of global table. Im using lua version 5.2.* compiled from source.

Lua function

function globaltable.subtable.hello()
 -- do stuff here
end

C++ code

lua_getglobal(L, "globaltable");
lua_getfield(L, -1, "subtable");
lua_getfield(L, -1, "hello");
if(!lua_isfunction(L,-1)) return;
    lua_pushnumber(L, x);
    lua_pushnumber(L, y);
    lua_call(L, 2, 0);

However im unable to call it, i always get an error

PANIC: unprotected error in call to Lua API (attempt to index a nil value)

On line #3: lua_getfield(L, -1, “hello”);

What am I missing?

Side question: I would love to know also how to call function deeper than this – like globaltable.subtable.subsubtable.hello() etc.

Thank you!


This is what im using to create the globaltable:

int lib_id;
lua_createtable(L, 0, 0);
lib_id = lua_gettop(L);
luaL_newmetatable(L, "globaltable");
lua_setmetatable(L, lib_id);
lua_setglobal(L, "globaltable");

how do i create the globaltable.subtable?

  • 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-06-07T15:46:07+00:00Added an answer on June 7, 2026 at 3:46 pm

    function is a keyword in Lua, I am guessing on how did you manage to compile the code:

    -- test.lua
    globaltable = { subtable = {} }
    function globaltable.subtable.function()
    end
    

    When this is run:

    $ lua test.lua
    lua: test.lua:2: '<name>' expected near 'function'
    

    Maybe you changed the identifiers for this online presentation, but check that on line 2 "subtable" really exists in the globaltable, because on line 3, the top of stack is already nil.

    Update:

    To create multiple levels of tables, you can use this approach:

    lua_createtable(L,0,0); // the globaltable
    lua_createtable(L,0,0); // the subtable
    lua_pushcfunction(L, somefunction);
    lua_setfield(L, -2, "somefunction"); // set subtable.somefunction
    lua_setfield(L, -2, "subtable");     // set globaltable.subtable
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to call a function present in one class from another class by
I am trying to call c# function from javascript the code i have tried
I'm trying to wrap a class from a library I'm using in Lua. Specifially,
While trying to call an EJB made using NETbeans (using jdk1.7) from a client
I am trying to call the GetConsoleScreenBufferInfoEx function from a console application. If it
In the following I am trying call action script function from js function. What
Am trying to call a JS script from a function library in another script.
I am trying to call a function in another .php file from this one
Hi im trying call this function from an element, but the element id is
I'm trying to run an executable using Lua's os.execute() function. If I do something

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.