Difference between require() and _G.require() for asset libraries

Hey guys,

I recently encountered a bit of an inconsistency with trying to use asset libraries.

When using the orthographic library the following works fine in lua:

__dex_lib_orthographic_Camera = _G.require("orthographic.camera")

However when using the Monarch library, the following does not work:

__warbattles_monarch_Monarch = _G.require("monarch.monarch")
/main.lua:67: module 'monarch.monarch' not found

However it works if I change it to this:

__warbattles_monarch_Monarch = require("monarch.monarch")

So what exactly is the deal with _G? And why is it different for these two libraries? From what I can tell on Github they both have the same structure…

local M = {}

...

return M

Interesting. _G is the table of global functions and function tables (modules). You find the standard Lua functions such as require, math.max, tostring, io.open etc

Calling _G.require() and require() should be the same.

Ah! The problem in your case is that the Defold build pipeline doesn’t recognise _G.require() and will therefore not include the required file. If it is required from somewhere else using require() it will work though.

2 Likes

Ah, so in the case of orthographic it works because I have a camera.go object in the game which requires the camera through its script, right?

Is this something that could/should/may be fixed in the future?

Yes, our require() detection can definitely be improved.