Calling main script functions from a plugin

Posted by Ked on Wed 10 Mar 2004 03:32 PM — 4 posts, 14,583 views.

Russia #0
This topic was brought up recently: how can a function in the main script file be called from a plugin script? I've wondered about it before, but back then I decided to just expose whatever data the outside scripts may need from the plugin through MC variables. But this approach doesn't work very well if, say, you want to use the OnPluginPartialLine callback in your main script. A solution is hypothetical (though it does work for me in several other problems) but I am pretty sure it'll work:

1.Make an alias in your world file to match on something more or less unique (I prefer using GUID's or Mushclient's GetUniqueID callback). Set that alias to call a routine in your main script.

2.Whenever OnPluginPartialLine fires in your plugin, simply do World.Execute with a string that will be matched by the alias above; using alias's wildcards you can pass any needed data at the same time also.

As I said, I haven't tried that exact thing myself, but I don't see anything that would prevent it from working.
Australia Forum Administrator #1
Yes, that sounds like a brilliantly inventive way of achieving it, and would execute immediately too. Good idea.
USA #2
Hmm.. An even better idea:

<aliases>
  <alias
   name="Sur_Send"
   script="Surname"
   match="<worldID>\:([[:digit:][:alpha:]_]) (.*)"
   enabled="y"
   regexp="y"
   send_to="12"
  >
  <send>call %1 %2</send>
  </alias>
</aliases>

Just replace the <worldID> part above with the unique ID for the world you are using it in, then the plugin can:

world.execute World.GetAlphaOption("id") & ":Some_Name " & Some_value

Since %1 and %2 directly substitute their contents, the script engine won't know the difference between a call directly coded into the script and your dynamically created one. In fact..... Since such a call inside a plugin should execute within the plugins script space, not the main one, this also solves the nasty issue of being unable to call your own custom sub, with custom parameters. The plugins alias will simply call its own sub that has the name you gave it.

Guess the CallPlugin command, that never worked all that well anyway, just became redundant. lol Really good hack Ked. I bow to your greatness and will bang my head against a wall for an hour in punishment for not figuring it out myself. ;)
Amended on Wed 10 Mar 2004 11:55 PM by Shadowfyr
Russia #3
Oooo, I'd like to see that :)