(resolved) Unable to create timer in function called by Execute! :(

Posted by j0nas on Sat 20 Sep 2008 03:16 AM — 10 posts, 30,938 views.

#0
I'm using the Execute-trick to trigger plugin-callbacks in my main, non-plugin code. Unfortunately it appears that I'm unable to add any timers in a thread that has been created by an Execute-call.

A good way to illustrate my problem is:
/Execute("/AddTimer(\"look\", 0, 0, 2, \"\", timer_flag.Enabled + timer_flag.OneShot + timer_flag.Temporary, \"\")")

Is there any way to fix or work around this, other than moving all my code into a plugin?

Edit: I am using 4.27, but I am probably going to downgrade to the last version using the old spellchecker, so if that's a solution to this problem... :)
Amended on Sat 20 Sep 2008 03:19 AM by j0nas
USA #1
It's a syntax problem. :)

/Execute("/AddTimer('look', 0, 0, 2, 'look', timer_flag.Enabled + timer_flag.OneShot + timer_flag.Temporary, '')")

works.


Execute("/AddTimer('look', -- the Label
0, --hours
0, --minutes
2, --seconds
'look', -- send field. here's your problem.
timer_flag.Enabled + timer_flag.OneShot + timer_flag.Temporary, --flags
'' --function in world's script file.
)")

#2
You're right, thanks for correcting me. My issue remains, though perhaps it's not related to Execute. I'll dig deeper and see if I can find the cause.
#3
My code is a lot more complex than the provided example, and in examining my issue further I've found that I get an errorcode "eScriptNameNotLocated (30009) Script name is not in the script file" when executing AddTimer under some circumstances. I know this isn't so, the script name exists, I think there's some kind of namespace issue perhaps.

Can I ignore the check AddTimer is performing here, to force it to do what I want?
#4
To reproduce the problem I'm having, two files are necessary. A plugin, and a main script file. They look like this:


--- Plugin ---
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>
<muclient>
<plugin
   name="PluginAddTimerTest"
   author="-"
   id="788b9fdcf934fecbb21e5306"
   language="Lua"
   purpose="-"
   date_written="2008-09-19"
   requires="3.80"
   version="1.0"
>
</plugin>
<script>
<![CDATA[
Execute("/addTimerTestTrigger()")
]]>
</script>
</muclient>

-- End Plugin ---

--- Main Script ---
local totalMessage = "";
testCallbacks = {}


function partialMessage(message)
  if type(message) == "boolean" then
    if message then
      message = "true"
    else
      message = "false"
    end
  end
  
  if type(message) == "number" then
    message = "" .. message
  end
  
  if not(type(message) == "string") then
    message = "<" .. type(message) .. ">"
  end
  
  totalMessage = totalMessage .. message
end


function message(message)
  partialMessage(message)
  
  print("[script] " .. totalMessage)
  totalMessage = ""
end


function addTimerTestTrigger()
  if testCallbacks.AddTimerTest then
    for i,o in pairs(testCallbacks.AddTimerTest) do
      o()
    end
  end
end


function timedEvent()
  message("TimedEvent triggered!")
end


function addTimerTest()
  message("AddTimer returns: " .. AddTimer('', 0, 0, 2, '', timer_flag.Enabled + timer_flag.OneShot + timer_flag.Temporary, 'timedEvent'))
end
testCallbacks.AddTimerTest = testCallbacks.AddTimerTest or {}
table.insert(testCallbacks.AddTimerTest, addTimerTest)

--- End Main Script ---


The code may look a little strange, but remember that it is taken out of a larger context. To reproduce the fault, install the script file and add the plugin, then try the following input(input marked with '>', output follows):

>/addTimerTestTrigger()
[script] AddTimer returns: 0
[script] TimedEvent triggered!
/Execute("/addTimerTestTrigger()")
[script] AddTimer returns: 0
[script] TimedEvent triggered!
/ReloadPlugin("PluginAddTimerTest")
[script] AddTimer returns: 30009


Again, MUSHclient 4.27. Is there anything I can do to fix this?

Edit: Tried to format the code a little bit better, but initial tabs and spaces seem to be ignored. Maybe if I knew the forum's markups, but... :)
Amended on Sun 21 Sep 2008 01:15 AM by j0nas
#5
A similar experiment with EnableTrigger gives me 30005, again while I know that the named trigger exists within the main script. It seems clear that the Executed functions are still working with the environment from the Executing plugin, despite having moved to the main script.

This is really making it very, very hard to work with callbacks in the main code. Is there something I can do about it, other than to start developing in plugins?
Australia Forum Administrator #6
Plugins and the main script are in different script spaces. They can even be in different languages (eg. VBscript and Lua). Also triggers, aliases, timers are in special plugin areas, not in the main world area. Thus a plugin cannot enable a main world trigger, or vice-versa.

This is all part of the modular design of plugins.

I would encourage you to develop inside plugins - in most cases you can do preliminary testing in the main script, and then use the Plugin Wizard to turn that into a plugin very easily.
#7
This whole problem stems from the fact that I can't use callbacks in the main script. If I could do that I would have no need for any plugins at all, is it possible to make that happen somehow, without the plugin-reroute through Execute?
Australia Forum Administrator #8
Your example script works in version 4.37. I noted in the other thread you made that Execute now executes in global script space from version 4.29 onwards.
#9
You're right, thanks!