0

I have a C function mesh_create_default(float *verts, uint32_t sizeof_verts):

void *mesh_create_default(float *verts, uint32_t sizeof_verts) {
    static void *mesh;
    mesh = mesh_create();
    mesh_data(mesh, verts, sizeof_verts);

    ...

    return &mesh;
}

and a function for lua to use:

int Lmesh_create_default(lua_State *L) {
    lua_pushinteger(L, (uint64_t)mesh_create_default(lua_touserdata(L, 1), lua_tointeger(L, 2)));
    return 0;
}

I am passing the result of mesh as a a 64 bit integer (perhaps not the best solution, but it works fine). My problem is with the verts argument. I don't know how I can pass a lua array into C. I have tried researching this, but I haven't found anything that solves this problem.

Say I have this lua code (just an example of what I want to do, not actual working code):

Verts = { -- an array of vertices, perhaps it needs to be somehow converted into a float * (I would prefer to convert it on the C side though, and keep my lua scripts as clean as possible)
     0.0,  1.0, 0.0,
     1.0, -1.0, 0.0,
    -1.0, -1.0, 0.0,
}

function Ready()
    Mesh = mesh_create_default(Verts, 4 * 3 * 3) -- or 8? Lua uses doubles (I think) but my code
                                                 -- requires floats, so that could be a bit of a
                                                 -- problem...
end

I apologise if this is an easily solvable question. My knowledge of lua is not that good (thus why I decided to use it for my game engine, to learn it).

EDIT: I need the entire array because I don't know how large the verts array is. The other option is to just get the size of the array as well too though (although less ideal and probably slower)

EDIT 2: Actually I do have the size of the array -- the sizeof_verts variable. Because of this, I think that this question is a duplicate of the question posted by @larsks (Read Lua table from C).

Snail5008
  • 59
  • 1
  • 9
  • 1
    Also see https://www.lua.org/pil/27.1.html which says basically the same thing. – larsks Jan 07 '23 at 02:57
  • Lua items are always passed using the Lua stack. – user253751 Jan 07 '23 at 03:06
  • @user253751 yeah, my problem is interpreting that data that is passed onto the stack. I am testing the above comments right now :) – Snail5008 Jan 07 '23 at 03:07
  • @larsks I think that this might be the solution, however when I try running `printf("%d\n", lua_rawgeti(L, 1, n));`, I get 3 no matter what value I put for `n`. Am I just not understanding how the lua_rawgeti function works? Does it push a number onto the stack or return it or what? – Snail5008 Jan 07 '23 at 03:14
  • Oh, I got it working! at least getting individual values. I was trying to print the result of lua_tonumber before, I was just not printing a newline and the segfault after it overwrote the output. Whether this also works for the entire array though, I will try now. Thanks for your help so far! EDIT: I need it to work for the whole array because I don't know the size of the verts array – Snail5008 Jan 07 '23 at 03:18

0 Answers0