I don't know what a module really does or why you would use it. Could someone explain?
Printable View
I don't know what a module really does or why you would use it. Could someone explain?
It's a queue system. It's so you don't hang up a script's execution running an infinite while loop.
This way you can have all your scripts in one lua file.
So when a module is executed, it will run and finish without having its execution interrupted, compared to a script with an infinite while-loop that can have its execution interrupted whenever?
I also see some use the registerEventListener(TIMER_TICK, "functionName"), not really sure when this should be used rather than modules
I've seen in your scripts that you've been using all these different methods. In some you use the above EventListener method, in other you use a module and also
infinite loop in some scripts. What makes you use different methods? Should modules be used only if you want to concurrently execute different contexts in the same script file?
Ended up with lots of questions haha, answer them if you feel like :)
The main reason to use a module is so the module doesn't get in the way of the rest of the script.
It allows you for example to display messages in a custom channel while also listening for user input in that said channel.
registerEventListener(TIMER_TICK... was the old way of achieving this. Modules actually use this method in it's core, but there's a queue system on top of that so you can support
multiple TIMER_TICKs in a sense, as well as delay, stop, pause, and restart them.
To support old scripts that still have this method, any registerEventListener with TIMER_TICK specified will create a module, it's the same thing, but you should definitely just use a Module in the first place.
As far as my scripts, if I had time I would update them all to use modules, infinite while loops are fine if you have a dedicated .lua script for that specific action.
I'm having a problem with Module either.
In my lua file I create:
Anyone knows why doesn't work?PHP Code:
local EquipRing = Module.New('EquipRing', function(module)
end)
function onWalkerSelectLabel(label)
EquipRing:IsActive() -- Always returns false
EquipRing:Stop() -- Seems like not to work, because Module still running
end
Has anyone figured out how to make Module:Stop() work?? I've tried both Module.Stop('module name') and modulename:Stop() formats and it doesn't work...
The only thing that works is when I create the module without starting it (using false for param 'startOnCreate') I'm able to start the module using Module.Start... But stopping the module afterwards still doesn't work...
Maybe I'm just fooling myself and I can't use Module.Stop on a label??.. But then again starting a module works...
Also where should one put module code? In the beginning of the main lua file, the end, a dedicated lua file, or doesn't matter??
We seem to be having the same problem actually! @parrode too
Yeah I pretty much gave up on modules until things are clearer... For now I'll just keep using multiple lua files. It works, it's reliable, and I can easily manually kill the scripts if I want.
Honestly I'm not even sure what's the advantage of using modules (other than not having to use multiple lua files)... I thought being able to start and stop modules on labels was a great advantage but it doesn't work. :rolleyes: Yes I know I could use labels like lua code:elseif (Label == "startRingSwitcher") then RingSwitch = trueand then put if RingSwitch then at the top of the module but I feel it's not clean enough. :p
elseif (Label == "stopRingSwitcher") then RingSwitch = false
...Plus I'm not even able to make the anonymous function module format work at all lua code:--This format doesn't work (or I just have no idea where the hell to put it):
Module.New('Ring-Switcher', function()
--This format works... to some extend:
function RingSwitcher(module)
Below is a description of modules, explanation from @Spectrus
Code:A module is a function that when created, is added to the TIMER_TICK event. It is then executed every 200ms, as is the behavior of the TIMER_TICK.
To create a Module, simply call the constructor for it: Module.New(name, func). The parameter name is a string which can then be used to modify the status of the module (i.e. Module.Stop(name), etc.). The function is the function that will be called when the TIMER_TICK event occurs.
In essence, a module is as simple as this:
###############################
-- First create a function for your module.
function checkCreature()
local c = Creature.New('Demon')
if c:isAlive() and c:isOnScreen() then
print('Run!')
end
end
-- Then create a module for that function.
Module.New('creature check', checkCreature)
################################
If you would like to control your module at any time, you can start or stop it with Module.Start(name) and Module.Stop(name).