Lua module - returns as nil problem

Hello.

I came across strange issue when I attempted to execute the following code:
GarrysMod/garrysmod/lua/includes/modules/test_module.lua:


-- GarrysMod/garrysmod/lua/includes/modules/test_module.lua

-- Local/private functions & variables
local _x, _y = 0, 0

-- Global/public functions
local GetX = function()
    return _x
end

local GetY = function()
    return _y
end

local PrintXY = function()
    print( GetX(), GetY() )
end

-- Main functions table
return {
    getX = GetX,
    getY = GetY,
    printXY = PrintXY
}


GarrysMod/garrysmod/lua/test.lua:


-- GarrysMod/garrysmod/lua/test.lua

local _test = require( "test_module" )

--[[
] lua_run_cl include( "test.lua" )

[ERROR] lua/test.lua:14: attempt to index local '_test' (a nil value)
  1. unknown - lua/test.lua:14
   2. include - [C]:-1
    3. unknown - LuaCmd:1
--]]

_test.printXY()
--  After above expression is called, "0    0" should be printed into  console. But it is not, instead it complains about local _test variable  being nil.
-- WTF is wrong here?


Require doesn’t work properly in Garry’s Mod. Either create a global table or use the module() function which will create one for you.

To expand on this: In your test_module.lua file, call module( “module name” ) at the top of the file. This however will not let you access other globals, so either call module( “module name”, package.seeall ), or create a local copy of the globals you want to have access to before calling module().

Thanks for your quick response, Willox. I really couldn’t figure it out.
I’ve readed several blogs and official lua manual about writing Lua module, and I found that using module() is obsolete/deprecated in Lua 5.2… Latest version being 5.3.
Oh well, since current GMod branch still uses Lua 5.1, I guess I will go with global table, or perhaps module()?
Is there any sugar under module() in GMod?

[editline]29th March 2015[/editline]

Okay, I see module() is like:
Source: http://lua-users.org/wiki/ModulesTutorial


local print = print -- the new env will prevent you from seeing global variables

local M = {}
if setfenv then
	setfenv(1, M) -- for 5.1
else
	_ENV = M -- for 5.2
end

local function private()
    print("in private function")
end

function foo()
    print("Hello World!")
end

function bar()
    private()
    foo()
end

return M

Right? :slight_smile:

Yes, or you can use the module() function that’s located in the “Old ways of creating a module” section on that page.

Thank you guys for your help. Here is the solution to my Lua module.
GarrysMod/garrysmod/lua/includes/modules/test_module.lua:


-- GarrysMod/garrysmod/lua/includes/modules/test_module.lua

module( "test_module", package.seeall )

-- Local/private functions & variables
local _x, _y = 0, 0

-- Global/public functions
getX = function()
    return _x
end

getY = function()
    return _y
end

printXY = function()
    print( getX(), getY() )
end

collectgarbage()


GarrysMod/garrysmod/lua/test.lua:


-- GarrysMod/garrysmod/lua/test.lua

require( "test_module" )
local test = test_module

test.printXY()

collectgarbage()


Greetings, OmegaExtern.