Require functions not working as they should when loaded

Posted by Trevize on Wed 06 Dec 2006 04:34 AM — 7 posts, 29,970 views.

#0
My problem is simple this. When I load an lua file with require it seem to cause some kind of a problem with script="sub". No error message shows up when loaded, but when the pattern matches and the trigger runs, it says "Trigger function "sub" not found or had a previous error.". This is -only- when I load with require. If I open my triggers in mushclient and enable the trigger "test" manually it works just like it should. If I use EnableTrigger or EnableGroup the error shows up when the pattern matches.. Can someone please explain this, its driving me nuts.. I'm trying to inplant a flexible system changing my offence depanding on what characther I'm using and not filling up the entire client with triggers and etc.
------------------------------
My main lua file loaded by the mushclient contains this and much more:
function is called and name = "assassin":
DeleteTriggerGroup("classes")
DeleteAliasGroup("classes")
DeleteTimerGroup("classes")
DeleteTemporaryAliases()
DeleteTemporaryTriggers()
classes = utils.readdir (GetInfo(57) .. "plugins\\xtcy\\classes\\*.lua") assert (classes)

for k, v in classes do
if tostring(string.gsub(k, "(%a+).lua", "%1")) == name then
require(GetInfo(57) .. "plugins\\xtcy\\classes\\" .. tostring(k))
end
end

function sub(subtext)
if subtext ~= "" then stext = subtext end
for word in string.gfind(stext, "($[#%a%d]+$[^$]+)") do
ColourTell(tostring(string.gsub(word, "$([#%a%d]+)$(.*)", "%1")), "black", tostring(string.gsub(word, "$([#%a%d]+)$(.*)", "%2")))
end
ColourTell("", "", "\n")
end

---------------------------

Assassin.lua contains this and other things.

ImportXML
[[
<triggers>
<trigger name="test" lines_to_match="2" keep_evaluating="y" match="^You prepare yourself to hypnotise your victim, (.*)\n(.*)\.$" multi_line="y" regexp="y" script="sub" send_to="12" sequence="101" group="classes">
<send>stext = "$gray$Prepares hypnosis for $white$" .. tostring("%1%2") .. "$darkgray$."</send>
</trigger>
</triggers>
]]

Ps. I've used LoadPlugin("url") before, the problem with that is that I cant use function in my main lua file with the aliases, triggers and timers which is vital for my system since its need to know balances, wielded weapons, current afflictions and so on. .ds

Many thanks to whoever can solve or atleast tell me why and what my problem is.
Amended on Wed 06 Dec 2006 04:36 AM by Trevize
Australia Forum Administrator #1
First you can simplify a couple of things:


DeleteTriggerGroup("classes")
DeleteAliasGroup("classes")
DeleteTimerGroup("classes")


... can become:


DeleteGroup("classes")


And instead of:


classes = utils.readdir (GetInfo(57) .. "plugins\\xtcy\\classes\\*.lua") assert (classes)


... this looks neater:


classes = assert (utils.readdir (GetInfo(57) .. "plugins\\xtcy\\classes\\*.lua")) 


(assert returns a non-nil value so you can simply assign it).

Quote:

function is called and name = "assassin":


As for your main problem, it is a bit unclear what you are doing exactly.

However a clue might be that the availability of a function is checked for when the trigger is added, for efficiency reasons. Thus this won't work:

  • Create a trigger that has a script function "sub" (which doesn't exist)
  • Later, create function "sub"
  • The trigger will not find "sub" now.


What you can do is make a dummy "sub" function (this can do nothing), so that MUSHclient marks it as "available". Then later on you can reassign the actual code that "sub" does, and it will still work (this is Lua-specific).
Amended on Wed 06 Dec 2006 07:39 AM by Nick Gammon
#2
Yeah I figured it might be something just like that. So I did what you say'd before I created an sub function in the lua file that is loaded with "require" and still got the problem. Only diffrence was that now (that) function was called instead of the function in my main lua file. Afer this I moved the function sub belove the function that load with "require" and then the normal function sub loads but the trigger still wont work.

I have even tried putting it as an sandbox, still wont load properly, but that way my normal sub function is called instead of the sandbox one.

The problem seem to be exactly what you stated

  • Create a trigger that has a script function "sub" (which doesn't exist)
  • Later, create function "sub"
  • The trigger will not find "sub" now.


But why the heck does'nt it find the sub function when its in a sandbow or above the ImportXml part in the lua file.

When the trigger is created and I test it with the test trigger function in mush (ctrl+shift+f12):

You prepare yourself to hypnotise your victim, Erus Smeff Valstivar, Wraith of
Eternal Ire.\0A

It shows:
You prepare yourself to hypnotise your victim, Erus Smeff Valstivar, Wraith of
Eternal Ire.
Trigger function "sub" not found or had a previous error.

If I open the triggers in mushclient, doubleclick on the one we're working on, and press ok. Close the window, and try running the test trigger again it works:
You prepare yourself to hypnotise your victim, Erus Smeff Valstivar, Wraith of
Eternal Ire.
Prepares hypnosis for Erus Smeff Valstivar, Wraith of Eternal Ire.

Out of ideas how to make it work. There's no other way of adding triggers, alises and timers from an lua och plugin file with them having access to the main functions in my main lua file?
Australia Forum Administrator #3
Quote:

There's no other way of adding triggers, alises and timers from an lua och plugin file with them having access to the main functions in my main lua file?


Ah I didn't spot this as being the exact problem.

It is part of plugin design that each plugin executes in its own script space, thus you cannot load subs into a plugin and expect them to be available in the main script file (or another plugin). For one thing, they may be in different script languages.

I gather you putting most of your scripts in the main file? Thus you shouldn't use plugins to load shared subroutines. Simply "require" them into the main script space.

Or, move the scripts, triggers, etc. into a plugin, and load that. I'm not totally clear on what you are doing, but it seems you are mixing two methods here.
#4
No I dont think thats what I mean.. I am already using require to do that... I'm tired of this and just want it to work and really really confused.. here's my entire lua file:

http://83.251.195.236/xtcy.lua

Dont try and load it, since its missing out on a few sandboxes.

Here's the file that I load with require:
http://83.251.195.236/assassin.lua

See function sub and class in xtcy.lua.. And see line 180 in assassin.lua.. that trigger is imported into the mushclient and there it tries to call the function sub which is in the main lua file "xtcy.lua"

I hope this helps to explain my problem, thank you
Australia Forum Administrator #5
I think your problem is covered in this post:

http://www.gammon.com.au/forum/bbshowpost.php?id=6369

Particularly this line:

Quote:

There is a test in the "Load World" function (which is also used for ImportXML), that it doesn't find script entry points if a plugin is doing ImportXML.


There is a suggested solution there, and this may well solve your problem.
#6
Thanks a million I read the thread and found a solution it just brought up another problem with my sub function but it aint possible to fix. Will do that in the morning..

Million thanks!!