Skip to content

How to access vector components? #1298

Answered by vegorov-rbx
Ono-Sendai asked this question in Q&A
Discussion options

You must be logged in to vote

Currently, Luau doesn't come with a full vector library/metatable setup.
Only one access method has a fast-path included in the interpreter loop for performance.

To setup basic x/y/z access, you can do the following:

static int lua_vector_index(lua_State* L)
{
    const float* v = luaL_checkvector(L, 1);
    const char* name = luaL_checkstring(L, 2);

    int ic = (name[0]) - 'x';

    if (unsigned(ic) < 3 && name[1] == '\0')
    {
        lua_pushnumber(L, v[ic]);
        return 1;
    }

    luaL_error(L, "%s is not a valid member of vector", name);
}

static int lua_vector_namecall(lua_State* L)
{
    luaL_error(L, "%s is not a valid method of vector", luaL_checkstring(L, 1));
}

void s…

Replies: 3 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by Ono-Sendai
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants