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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:09:16+00:00 2026-06-17T02:09:16+00:00

In Lua Code Test = {} function Test:new() local obj = {} setmetatable(obj, self)

  • 0

In Lua Code

Test = {}
function Test:new()
  local obj = {}
  setmetatable(obj, self)
  self.__index = self
  return obj
end
local a = Test:new()
a.ID = "abc123"
callCfunc(a)

In C Code

int callCfunc(lua_State* l)
{
  SetLuaState(l);
  void* lua_obj = lua_topointer(l, 1);            //I hope get lua's a variable
  processObj(lua_obj);
  ...
  return 0;
}

int processObj(void *lua_obj)
{
  lua_State* l = GetLuaState();
  lua_pushlightuserdata(l, lua_obj);              //access lua table obj
  int top = lua_gettop(l);
  lua_getfield(l, top, "ID");                     //ERROR: attempt to index a userdata value
  std::string id = lua_tostring(l, -1);           //I hoe get the value "abc123"
  ...
  return 0;
}

I get the ERROR: attempt to index a userdata value
How to access lua’s object from lua_topointer() ?

Storing a lua object in C, then calling it from C.

  • 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-17T02:09:17+00:00Added an answer on June 17, 2026 at 2:09 am

    You shouldn’t use lua_topointer as you can’t convert it back to lua object, store your object in the registry and pass it’s registry index:

    int callCfunc(lua_State* L)
    {
        lua_pushvalue(L, 1);//push arg #1 onto the stack
        int r = luaL_ref(L, LUA_REGISTRYINDEX);//stores reference to your object(and pops it from the stask)
        processObj(r);
        luaL_unref(L, LUA_REGISTRYINDEX, r); // removes object reference from the registry
        ...
    
    
    int processObj(int lua_obj_ref)
    {
        lua_State* L = GetLuaState();
        lua_rawgeti(L, LUA_REGISTRYINDEX, lua_obj_ref);//retrieves your object from registry (to the stack top)
        ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Lua Code Test = {} function Test:new() local obj = {} setmetatable(obj, self)
I am experimenting with the following lua code: function test() return 1, 2 end
In the following lua code: function interp(s, tab) return (s:gsub('($%b{})', function(w) return tab[w:sub(3, -2)]
Consider the Lua code below: local util = {} function util:foo(p) print (p or
I implemented Simple Lua class in C. Usage of class: require(test) function foo() local
I have the following code in Lua: ABC: test (X) The test function is
Within my code, I need to push an int onto Lua's stack. I know
#1 Lua: local test = Test(); #2 C: //creating lua's test luaL_newmetatable(L, someTable); lua_userdata
Consider the following Lua code: local var1, var2; Is var2 a local variable here?
How to return C++ object to lua? My C++ code is following: class MyClass

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.