Defined GMod function to local

I was just wondering why that some scripts do this. This part is copied from the GMod hook module:

local gmod                        = gmod
local pairs                        = pairs
local isfunction        = isfunction
local isstring                = isstring
local IsValid                = IsValid

I just cant see the point of calling already made functions into local. Does it take less memory to process them? Pure curiosity.

It’s part of an optimisation that doesn’t need to happen anymore thanks to LuaJIT.

Thanks :slight_smile: Learning more and more everyday :slight_smile:

While you are correct that that’s the usual reason for doing this, it’s not in this case. The reason the hook module does this is because it uses the

module function which sets the environment to the table it creates. Localizing the functions before you call

module is one way around it, another is using

package.seeall, though I’d suggest against using

module at all in new code.

I’ve never touched module in gmod or Lua so I had no idea about that. Why do you advise against it?

module is deprecated and not supposed to be used.

also luajit does not optimize that to the extent people say they are.

LuaJIT only optimizes if its trace detects a function is a fastfunc when called, which means luajit still has to find the function from all the tables it is in if it’s global or in a table.


[editline]26th August 2017[/editline]

While I agree that a source would be nice, Garry’s mod should not be used in any way as an example of something done right or how to do anything or anything up to date.

It was deprecated in Lua 5.2, you can see that here. You can see many of the critiques in detail here.

TLDR: The global side effects it causes alongside the “magic” it does is seen as bad practice, likewise what it wraps up can easily be done without it.

Very good. However if module is deprecated and of gmod was to migrate to the new way of making modules, how would it work? Would it just do like _G.hook = include"modules/hook.lua? If so then it defeats the purpose if I’m not mistaken. If not then every time you want to use the hook module you have to include it yourself which if I’m not mistaken will create its own local hooks table which wouldn’t really work.

A lot of deprecated functions are still used in Garry’s Mod – it would require major refactoring to change and no one is up to the challenge :v:

I would do it if I knew how it would work (see my above post).

You would just add them like regular libraries: no Garry’s Mod included modules work as separate instances

Ye but using hook as an example again, currently it exists in the global table right? so _G.hook is what you use when you do hook.Add and stuff. If you migrate hook to use the new way, then you either have to assign it to _G yourself which defeats the purpose of replacing it IMO or you have to do local hook = include("modules/hook.lua") every time you want to use hook. If you do it that way, how can the gamemode or engine or whatever execute all hooks that exist?

No, the functions would just be added to the global table. Ex.

hook = {}

function hook.Add(...)


Then in GMod’s init


Thats easy af why wouldnt anyone be up to it?

It’s probably possible with a regex script, but otherwise, someone would have to convert them by hand. It’s also under the guise that it would break compatibility with addons that manually require the modules, and so the PR might never be merged.

[editline]26th August 2017[/editline]

Or at least until another major API change occurs.

Woah woah woah, I thought part of the point was giving things namespace, according to this modules should not add to the global namespace, and should be returned from the scripts like normal.

local hook = {}

hook.Add = function()


return hook

local hook = include("modules/hook.lua")


It’d be great if Garry’s Mod did it like that, but it’d break every script ever made, specifically anything to do with hooks, commands, chat, and anything that uses global modules

[editline]27th August 2017[/editline]

I can’t be bothered typing include

It would be included globally – that’s how the GMod handles its libraries. They’re not instances.