Really basic module scope question

Post Reply
brennanyoung
Posts: 6
Joined: Mon Mar 08, 2021 3:30 pm

Really basic module scope question

Post by brennanyoung »

I am a software dev but a novice at lua and mudlet, and I am finding it extremely confusing to make use of general programming concepts (such as scope, modules, instantiation etc.) with mudlet.

I'm baffled about how these concepts may or may not apply to scripts, triggers and aliases. I've seen a thread on the forum which talks about how everything in mudlet uses the same namespace(!), but this mostly seems to be rants between devs about their preferences regarding packages or closures. Not really that helpful for me.

I can find nothing in the manual which tells me how trigger X might initialise a module in script Y. A really, really basic step.

Example: If I make a module script, such as the following (borrowed from lua-users.org/wiki/ModuleDefinition )
Code: [show] | [select all] lua
-- mymodule.lua
local M = {} -- public interface

-- private
local x = 1
local function baz() print 'test' end

function M.foo() print("foo", x) end

function M.bar()
  M.foo()
  baz()
  print "bar"
end

return M
and then (if I was not in mudlet) I could initialise the module and use it with
Code: [show] | [select all] lua
local MM = require 'mymodule'
MM.bar()
so... how might I do this from a trigger or an alias instead? What is the equivalent to "require" which would initialise a mudlet script rather than loading an external file? Do I have to use the file system? Can't I just treat a script like a module definition? Maybe I can, but how?

BTW a really basic example of this in the wiki would help a great deal.

User avatar
Vadi
Posts: 4940
Joined: Sat Mar 14, 2009 3:13 pm

Re: Really basic module scope question

Post by Vadi »

Hey! Good question.

Modules in Lua are really just tables in the global table. Since you're an experienced software dev, I'll drop down into the low level details.

Everything is actually in a global table named `_G`. So when you do `target = "bob"`, you can actually access it as `_G.target` and `_G["target"]` equally well. Of course, you should not be using `_G` in your daily operation, but there are some rare cases where it's handy as these things go.

What is a Lua module then? A Lua module can either return all it's contents (as in your example) or you can load it directly in _G. How can you achieve the same in Mudlet? Make a new script and only put functions in your namespace that you'd like exposed, and keep the rest local, for example:
Code: [show] | [select all] lua
-- your custom namespace
mysystem = mysystem or {}

-- public function
function mysystem.callme()
  print("worked!")
end

-- private function
local function privatefunction()
  print("try calling me")
end
Drop this into a script and try calling those functions from an alias/trigger. An introduction to namespaces can also be viewtopic.php?f=8&t=1211.

In general, Lua supports a lot of the general programming concepts - if you'd like, you can use them - but you're not forced to either, so that's why you don't see it or Mudlet boxing you into them.

I hope that gives you a good starting point and feel free to ask more :)

visionok
Posts: 22
Joined: Thu Mar 04, 2021 8:50 am

Re: Really basic module scope question

Post by visionok »

I was looking for a way to show the currently defined "global" variables, your mention of _G seemed to be just what I need. So I tried
Code: [show] | [select all] lua
lua _G
and
Code: [show] | [select all] lua
lua display(_G)
both resulted in mudlet going into some frenzy (using all the CPU) and needing killing. Is there any way to show global variables?

User avatar
Vadi
Posts: 4940
Joined: Sat Mar 14, 2009 3:13 pm

Re: Really basic module scope question

Post by Vadi »

Yep, either update to the latest PTB where that works okay, or for the non-programmers, there's the Variables view:
Selection_090.png

Post Reply