Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the value from a table within a table with Lua?

Tags:

c++

c

lua

I'm writing a material system for a game engine that I'm working where a Lua script is basically used as a config file for the material.

I'm storing values in a table, but for vector values (vec2, vec3, etc.) I am embedding a table inside the main table to hold the multiple values, like so:

material = {
    color = {0.2, 0.3, 1}
}

I want to get the individual values of color, and this is what I've tried to get the first value:

lua_getglobal(L, "material");
if (!lua_istable(L, -1)) {return;};

lua_pushstring(L, "color");
lua_gettable(L, -2);
if (lua_istable(L, -1)) {
    lua_rawgeti(L, -1, 0);
    printf("%f\n", lua_tonumber(L, -1));
}
lua_pop(L, 1);

But it only ever prints 0.0, no matter the first value in the color table. What am I doing wrong?

like image 210
veridis_quo_t Avatar asked Dec 06 '25 18:12

veridis_quo_t


1 Answers

The first index should be 1 and not 0.

lua_rawgeti(L, -1, 1);
like image 186
Robert Avatar answered Dec 08 '25 08:12

Robert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!