Hello.
I came across strange issue when I attempted to execute the following code:
[I]GarrysMod/garrysmod/lua/includes/modules/test_module.lua[/I]:
[CODE]-- 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
}
[/CODE]
[I]GarrysMod/garrysmod/lua/test.lua[/I]:
[CODE]-- 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?
[/CODE]
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.
[QUOTE=Willox;47418075]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.[/QUOTE]
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().
[QUOTE=Willox;47418075]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.[/QUOTE]
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]
[QUOTE=James xX;47418112]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().[/QUOTE]
Okay, I see module() is like:
[I]Source: [URL="http://lua-users.org/wiki/ModulesTutorial"]http://lua-users.org/wiki/ModulesTutorial[/URL][/I]
[CODE]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[/CODE]
Right? :)
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.
[I]GarrysMod/garrysmod/lua/includes/modules/test_module.lua[/I]:
[CODE]-- 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()
[/CODE]
[I]GarrysMod/garrysmod/lua/test.lua[/I]:
[CODE]-- GarrysMod/garrysmod/lua/test.lua
require( "test_module" )
local test = test_module
test.printXY()
collectgarbage()
[/CODE]
[QUOTE]] lua_run_cl include( "test.lua" )
0 0
] lua_run_cl test_module.printXY()
0 0[/QUOTE]
Greetings, OmegaExtern.
Sorry, you need to Log In to post a reply to this thread.