[C-API] How to use set/getUserData() in C SDK

I am developing using MacOS with PlayDate C SDK 2.5.0 and I was trying to figure out how to use this functions. My idea was to put there a Lua table with some callbacks and then do something like this:


my_object *foo = init();
my_object->lua_userval = pd->lua->pushObject(my_object, CLASSNAME_MY_OBJECT, 1); 

// ...

int mo_set_luadata(lua_State *L) {
    LuaUDObject *lua_userval;
    my_object *mo = (my_object*)pd->lua->getArgObject(1, CLASSNAME_MY_OBJECT, &lua_userval);
   
    // second argument is table and also top of stack if I understand correctly
    pd->lua->setUserValue(lua_userval, 1);
}

static void some_callback(my_object *mo) {
    pd->lua->pushObject(my_object, CLASSNAME_MY_OBJECT, 1); 
    pd->lua->getUserValue(mo-> lua_userval, 1);
    pd->lua->callFunction("my_object.callback");
}

But it seems work incorrectly or I am doing something terribly wrong :slight_smile:

If you're trying to create a pseudo-class in C for Lua code to use, you can try playdate->lua->registerClass(), which does most of the table creation under the hood (in fact, there's no way to push a table directly in the C API).

Well, that's actually exactly the issue. I replied in another thread that I would like to push at least callback function somehow. Right now I had to make some workaround to make everything work