Jump to content

[SOLVED] Is there an easy way to access GLOBAL from my own Classes?

Recommended Posts

[EDIT] Found a solution to my original question.

The new environment actually is GLOBAL.

So instead of


In the modmain.lua

You'd just do


In another file (ie: printer).

No passing of GLOBAL required. Just import as

local Printer = require("util/printer")


[Original question]


So I've made my own util class in Lua called "Printer", and am requiring it from my modmain

local Printer = require("util/printer")

It's a collection of all the things I'm using to display ingame data in some way shape or form. (read: crude debug tool)

ie, approximately:

Printer.Print(...) = print(...)

Printer.CharacterSay(...) = TheNet:Say(...)

Printer.Talker(...) = ThePlayer.components.talker:Say(...) -- protected from crashing

Printer.TablePrint(...) = something similar to the Util table readers (I haven't fully digested them yet, and only found them after writing mine), but with a "max depth" built in to the recursive function, and a workaround for the print() character length limit of 4083.


For TheNet and ThePayer, I need GLOBAL however. And if I try to use it directly from inside the Printer class, it appears as a nil


So I'm currently using something like a Factory paradigm to build it in the require line

local Printer = require("util/printer"):WithGlobal(GLOBAL) 

And I've recently figured out that if I leverage Class file properly, I might be able to do something like this instead

local Printer = require("util/printer")(GLOBAL) 

I also tried to dig up to how the modmain "environment" is set. But it looks like it'd be messier than these solutions. Correct me if I'm wrong.


So I was wondering if anyone had anything better?

Am I just missing something obvious?

Edited by Bigfootmech
Link to comment
Share on other sites

GLOBAL is accessible from all mod environments, and doesn't/shouldn't be passed in through a function.
Klei scripts have their environment set directly to global.
Mods do not, in order to prevent accidental tainting of important globals.

Environment is set up in mods.lua:267 in function CreateEnvironment.

Personally, this is what I do to avoid needing GLOBAL (and to avoid dealing with strict.lua).

	local modEnv = GLOBAL.getfenv(1)
	local rawget, setmetatable = GLOBAL.rawget, GLOBAL.setmetatable
	setmetatable(modEnv, {
		__index = function(self, index)
			return rawget(GLOBAL, index)
      -- lack of __newindex means it defaults to modEnv, so we don't mess up globals.



Edited by penguin0616
  • Like 2
Link to comment
Share on other sites

Ah, this isn't exactly what I meant, but it has helped me move forward, thank you :)

I don't actually mind using GLOBAL.ThePlayer instead of ThePlayer in my modmain.

What I want to do is to use GLOBAL.ThePlayer, and GLOBAL.TheNet inside my /<mod folder>/scripts/util/printer.lua , But currently GLOBAL is a nil there.


So I thought maybe it was possible to attach an "environment" to my Printer class cleanly.

Except the only thing I can find that runs something in an environment is /scripts/util.lua:721 "RunInEnvironment" and "RunInEnvironmentSafe"

For them though, I need /scripts/mods.lua:515 kleiloadlua(modMainFileFullPath)

Which I can't find declared anywhere, so I'm assuming that it's on the C side. Which possibly means RunInEnvironment isn't what I'm looking for.


Also, I totally forgot about how metatables work. So re figuring that out now

But I think from the stuff you posted about how to get the mod environment, as well as the class file having a potential for setting __index should be enough to keep me busy for a bit.

I actually went down a coding rabbit hole to make a slightly prettier version of my printer, so I can see what a default Class looks like exactly. In a reader-friendly manner.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...