[Lua 5.4] math library not working with vector types

Notes:

  • Only happens when using Lua 5.4
  • Server artifact 4491 Windows
  • Happens both client & server-side
  • Happens on both Stable & Canary

Expected behavior:

For the Lua math library to accept vector types like with CfxLua 5.3. For example, I use math.floor on vectors to round sometimes. This should run the floor operation on each vector value and return a vector.

Current behavior:

Using Lua math operations such as math.ceil, math.floor makes Lua throw a bad argument error. bad argument #1 to 'floor' (number expected, got vector).

Repro:

-- This will throw type errors when using 5.4 but works with CfxLua 5.3
local _vector = vec(2.95234, 1.234324, 1049.623425)

print(math.ceil(_vector))
print(math.floor(_vector))
print(math.abs(_vector))

Comments:

Not sure when the math library started doing this in 5.4. I know this worked fine ~3 or so months ago since I was using this to round player positions before saving them to db.

Guess it’s another thing I let slip through the cracks without reporting (Sorry).

Not sure when the math library started doing this in 5.4.

These changes were dropped when the GLM bits were introduced. The original (and 5.3) implementation required too many changes to vanilla Lua (i.e., much of lmathlib required a complete rewrite) and I wanted to keep most of the vector/matrix bits in their own individual compilation units. The design also makes it much easier to merge upstream Lua changes.

Instead, Lua5.4 now offers the GLM library that is intended to be a complete super-set of the base Lua math library, e.g.

local glm = require('glm') -- Also possible to do: math = require('glm')
local _vector = vec(2.95234, 1.234324, 1049.623425)

print(glm.ceil(_vector))
print(glm.floor(_vector))
print(glm.abs(_vector))
2 Likes

That certainly explains it! This completely flew under my radar :sweat_smile:, but makes a lot of sense. Thanks.

Is this noted down anywhere for people to find (potentially w/ the require workaround)?