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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:32:48+00:00 2026-06-12T12:32:48+00:00

I have a custom userdata in lua. When I execute tostring on an instance

  • 0

I have a custom userdata in lua. When I execute tostring on an instance of the userdata (inside Lua), it always returns a string like "userdata: 0xaddress". I would like it to return the name of the userdata ("Point: 0xaddress"), meaning that I want to override the tostring to include a case for my userdata. Does anyone know if this can be done?

#define check_point(L) \
    (Point**)luaL_checkudata(L,1,"Point")

static int 
luaw_point_getx(lua_State* L)
{
    Point** point_ud = check_point(L);
    lua_pushinteger(L, (*point_ud)->x);
    return 1;
}

static int 
luaw_point_gety(lua_State* L)
{
    Point** point_ud = check_point(L);
    lua_pushinteger(L, (*point_ud)->y);
    return 1;
}

void 
luaw_point_push(lua_State* L, Point* point)
{
    Point** point_ud = (Point**)lua_newuserdata(L, sizeof(Point*));
    *point_ud = point;
    luaL_getmetatable(L, "Point");
    lua_setmetatable(L, -2);
}

static const struct luaL_Reg luaw_point_m [] = {
    {"x", luaw_point_getx},
    {"y", luaw_point_gety},
    {NULL, NULL}
};

int 
luaopen_wpoint(lua_State* L)
{
    luaL_newmetatable(L, "WEAVE.Point");
    lua_pushvalue(L, -1);
    lua_setfield(L, -2, "__index");
    luaL_register(L, NULL, luaw_point_m);
    return 1;
}
  • 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-12T12:32:49+00:00Added an answer on June 12, 2026 at 12:32 pm

    You have to provide a __tostring metamethod, in the similar way you wrote the __index.

    // [...]
    int luaw_point_index(lua_State* L)
    {
        lua_pushfstring(L, "Point: %p", lua_topointer(L, 1));
        return 1;
    }
    
    int luaopen_wpoint(lua_State* L)
    {
        luaL_newmetatable(L, "WEAVE.Point");
        lua_pushvalue(L, -1);
        lua_setfield(L, -2, "__index");
        lua_pushcfunction(L, luaw_point_index);
        lua_setfield(L, -2, "__tostring");
        luaL_register(L, NULL, luaw_point_m);
        return 1;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have custom validation rule: public function customRule($check) { } Inside this rule I
I have a query that selects data into a custom type- UserData curData =
I have a custom form... I'd like to auto save the author (authenticated user)
I have a custom registration BrowserView where you have to extend some userdata (z3c.form
I have custom SPlist with some fields that I'd like to bind to a
I have a custom modelbinder, its check the authentication cookie and return the value.
I have this code for serializing my custom collection of UserData Objects. However the
I have custom UITableViewCell . It contains UITextLabel . When I press this cell
I have custom classes that I currently instantiate within App.xaml as resources. I want
i have custom cell with 2 buttons(the function of these buttons is just to

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.