Attempt to index global 'bit32' (a nil value)

I am attempting to read individual bits from a string of binary data. Everything works up until the point I call anything in the bit32 library. Calling this library produces the following error:

 [Error] (Script) - lua scripts/def file classes/defsymbol.lua:20: attempt to index global 'bit32' (a nil value)
---------- START STACK TRACE ----------

I am aware that this bitwise operators library was originally shipped in Lua 5.2, backported to 5.1, and removed in 5.3 (replaced with <<, &, | and the like). Since this came up as an autocomplete option in the Lua Editor (it turned what appears to be purple when I completed the method call), and the documentation points to the 5.1 reference, I am guessing that Lumberyard is using a version of Lua 5.1 that should have this library backported into it.

My question is as follows:

  • Is this library actually available in Lumberyard’s version of Lua?
    • If so, how do I access it? Is this a problem with my LUA_PATH/LUA_CPATH environment variables, or some other configuration thing?
      The only workaround I can think of so far is to make the operations myself. I already checked the 5.3 operators, and those symbols aren’t recognized.

The offending line of code that I used to test:

a = bit32.bor(1,4)

Hi @Roosevelt.Boyland,

Lumberyard does indeed use Lua as of LY 1.12. The bitwise operators are in 3rdParty\Lua\\Lua\lbitlib.c however it looks like the default script context is not loading that library like the legacy script context did. In order to fix it you need to update dev\Code\Framework\AzCore\AzCore\Script\ScriptContext.cpp

At the top around line 29 add

LUALIB_API void lua_bitlibopen (lua_State* L);

Then around line 3616 after the luaL_openlibs(m_lua) line add this


After you recompile you can access the bitwise functions in lua like this (no need to require anything, the functions are in the global table)

local a = bxor(4)

However, I recommend taking a step back and considering not doing any binary manipulation in Lua, and instead writing a c++ component or library to do this for you, or expose your own c++ method to Lua.

@petrocket Thanks for the information! After fiddling with trying to do this in pure Lua (and failing), I decided to move the related code to a C++ Gem. Hopefully this helps anyone who is trying to do this purely in Lua.