Vector3 table appears incorrectly as number instead of table

86 views
Skip to first unread message

Mokka Chocolata

unread,
Jul 14, 2025, 11:04:29 AMJul 14
to lu...@googlegroups.com
I'm making a custom Vector3 class, where there is a new function. I'm making this for a game (this is not Roblox, rather a completely different engine).

When I call the function, it returns the X value. I want a table with XYZ and special metafunctions to handle math.

I tried playing around with indexes, and checking that the arguments are correct (it is).

Even AI couldn't help: at one point it got stuck generating.

Anyways, I have attached a C source code as a TXT file.
I'm calling it from Lua as "print(Vector3.new(0.5, 0.5, 0.5))".

Source code.txt

Sainan

unread,
Jul 14, 2025, 11:14:56 AMJul 14
to lu...@googlegroups.com
Oh no, my AI-generated code doesn't work, and because I don't understand it, I'm in no position to debug it!

If I had to guess, change `return 2` to `return 1` to return only the last value on the stack instead of the last 2.

-- Sainan

Scott Morgan

unread,
Jul 14, 2025, 11:29:04 AMJul 14
to lu...@googlegroups.com
On 14/07/2025 15:35, Mokka Chocolata wrote:
> I'm making a custom Vector3 class, where there is a new function. I'm
> making this for a game (this is not Roblox, rather a completely
> different engine).
>
> When I call the function, it returns the X value. I want a table with
> XYZ and special metafunctions to handle math.


Looks like LuaLibs_Vec3ToLuaVector3 is returning just one table to the
stack, but LuaLibs_Vector3New is returning 2 which means that last stack
argument passed to it plus the new table.

I suspect the following will work, but shows what needs changing:

local a, b = Vector3.new(0.1, 0.2, 0.3) -- a = 0.3
print(b) -- the new vector

You can also improve things by only defining the metatable once
(probably in SetupLibs) and reusing it. Read up on luaL_newmetatable and
luaL_getmetatable.

A few other things need fixing up there to make it a good module, but
that'll get you working for now.

Scott

Tomás Guisasola

unread,
Jul 14, 2025, 11:29:14 AMJul 14
to lu...@googlegroups.com
Hi Sainan

> If I had to guess, change `return 2` to `return 1` to return only the last value on the stack instead of the last 2.

The function pushes a single value, so I think it won't make such a
difference to correct the return value to 1, although it is
recommended.

Regards,
Tomás

Tomás Guisasola

unread,
Jul 14, 2025, 11:32:08 AMJul 14
to lu...@googlegroups.com
Hi Mokka

> Anyways, I have attached a C source code as a TXT file.
> I'm calling it from Lua as "print(Vector3.new(0.5, 0.5, 0.5))".

The code lacks some functions, such as `LuaLibs_Vector3ToString`...

Regards,
Tomás

Luiz Henrique de Figueiredo

unread,
Jul 14, 2025, 12:27:06 PMJul 14
to lu...@googlegroups.com
I've dug out and dusted an old code for 3d vectors that was meant for learning.
It only handles access to fields but it is easy to extend with other operations.
See http://web.tecgraf.puc-rio.br/~lhf/ftp/lua/ar/lv3-100.tar.gz
Enjoy.
--lhf

isncg

unread,
Jul 15, 2025, 9:03:46 AMJul 15
to lua-l
Hi Mokka

In my opinion, LuaLibs_Vec3ToLuaVector3 `return 2` caused two values placed into the stack:  parameter z and the result table.

before LuaLibs_Vec3ToLuaVector3 returns, the lua stack should be like following:
[function pointer], [param x], [param y], [param z], [result table]

after the function call, lua process the `return 2` by move last two values to the place where the function pointer is, and the first return value is parameter z. 
this is done by `moveresults` in ldo.c:

firstresult = L->top.p - nres;  /* index of first result */
if (nres > wanted)  /* extra results? */
    nres = wanted;  /* don't need them */
for (i = 0; i < nres; i++)  /* move all results to correct place */
    setobjs2s(L, res + i, firstresult + i);
for (; i < wanted; i++)  /* complete wanted number of results */
    setnilvalue(s2v(res + i));
L->top.p = res + wanted;  /* top points after the last result */

I am not sure why lua can use function parameters as return values directly, maybe it assumes C API always returns the exact number?

Regards,
Yishen
Reply all
Reply to author
Forward
0 new messages