lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

On Jun 11, 2014, at 11:52 AM, Lars Ruoff <> wrote:

Thanks Roland and all,

i have already a solution on the C(++) side. As i said, it's basically
struct VECTOR {
with some methods around it for utility functions.

Would it be better to expose those functions to Lua or use a Lua-side library?
The Lua scripts would need to get some vectors in tables, then do some calculation with it and return a bunch of other vectors.

And concerning my other initial question, would it feel "right" for a Lua coder to handle a 2D vector as a Lua table with elements "x" and "y"?

This comes down to how much efficiency you need, and if you want the vectors to be opaque or transparent to Lua code

If you want Lua to act as a custodian of opaque vectors that are operated on via a C library, then userdata is your friend. You can, of course, expose the C API through a metatable and then have a nice OO style vector interface to your C library. This is pretty fast and compact, but perhaps cumbersome if you want to make the vectors more transparent to Lua.

If you go the table route, be aware that Lua tables do have more overhead than a userdata. Also, it’s typically somewhat faster (and more compact) to store the X,Y parts in the array part of the table (so, use v = {1, 2} instead of v = { x = 1, y = 2 } ), effectively treating them more like a tuple.

If you have a LOT of vectors to work with in arrays, storing them in column form is much more efficient (that is, two arrays, one of X values, one of Y values). For a million vectors, this approach uses two tables instead of one million, and is MUCH more efficient in memory usage.