Gammon Forum
Entire forum
MUSHclient
General
Plugin execute a script on main lua file
Plugin execute a script on main lua file
|
It is now over 60 days since the last post. This thread is closed.
  Refresh page
Posted by
| Rdiniz
Brazil (21 posts) bio
|
Date
| Sat 13 Oct 2018 07:59 PM (UTC) |
Message
| Hi all,
I want to have a plugin executing a simple script on the main .lua file as if it was inside that file.
The function is actually being executed, but the value returned is not the same as if the function is called from the same lua file.
How can I acomplish this?
Thank you | top |
|
Posted by
| Fiendish
USA (2,514 posts) bio
Global Moderator |
Date
| Reply #1 on Sat 13 Oct 2018 09:14 PM (UTC) |
Message
| If the function is being executed, then it's working. To know why it's returning something other than what you expect, I think we need to see your script file. |
https://github.com/fiendish/aardwolfclientpackage | top |
|
Posted by
| Rdiniz
Brazil (21 posts) bio
|
Date
| Reply #2 on Sat 13 Oct 2018 09:27 PM (UTC) |
Message
| It's a pretty simple script, ill try to make it short:
I have this on the beginning of the main .lua file:
Then something like this on a login:
function onLogin(class)
if class == "ROGUE" then canStab = true end
end
I also have a simple getter for this value:
function getCanStab()
return canStab
end
If I try this
from prompt, i get the correct true return. The problem is that when I call getCanStab() from a plugin, its returning the default value (false). | top |
|
Posted by
| Nick Gammon
Australia (22,986 posts) bio
Forum Administrator |
Date
| Reply #3 on Sat 13 Oct 2018 11:00 PM (UTC) |
Message
| You are better off using a MUSHclient variable, and not a Lua variable (ie. use SetVariable). Then in the plugin use GetPluginVariable with an empty plugin ID to find the main variable value.
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | top |
|
Posted by
| Rdiniz
Brazil (21 posts) bio
|
Date
| Reply #4 on Sat 13 Oct 2018 11:09 PM (UTC) |
Message
| Hi Nick,
when you say I'm better off using global variables you mean there isnt another way? I have over 100 variables, making all of them global would be a lot ot work... | top |
|
Posted by
| Fiendish
USA (2,514 posts) bio
Global Moderator |
Date
| Reply #5 on Sun 14 Oct 2018 03:14 AM (UTC) Amended on Sun 14 Oct 2018 03:16 AM (UTC) by Fiendish
|
Message
| What is calling your onLogin function? I think the problem you're encountering is that plugins and the world don't share the same script space. Just because onLogin is being called by the world doesn't mean that canStab is being set to true for the plugin. Nick's suggestion of using GetPluginVariable with an empty ID to get the world's stored variables is one way around that. |
https://github.com/fiendish/aardwolfclientpackage | top |
|
Posted by
| Rdiniz
Brazil (21 posts) bio
|
Date
| Reply #6 on Sun 14 Oct 2018 04:56 AM (UTC) |
Message
| Oh, I thought the script had an unique space... that's what is happening then. The onLogin function is being executed from a regular trigger on my world, not from the plugin.
Thank you guys, gonna try the global variables... | top |
|
Posted by
| Nick Gammon
Australia (22,986 posts) bio
Forum Administrator |
Date
| Reply #7 on Sun 14 Oct 2018 05:14 AM (UTC) |
Message
| Each script (in each plugin) has a unique script space, plus one for the main script file. They might even be different languages (eg. VBscript, Lua, JScript).
However there is a module that lets you easily access MUSHclient variables from Lua:
https://github.com/nickgammon/mushclient/blob/master/lua/var.lua
It should be part of the MUSHclient download. See http://www.gammon.com.au/modules for more information (scroll down to the var.lua part). |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | top |
|
Posted by
| Nick Gammon
Australia (22,986 posts) bio
Forum Administrator |
Date
| Reply #8 on Sun 14 Oct 2018 05:17 AM (UTC) |
Message
|
Rdiniz said:
when you say I'm better off using global variables you mean there isnt another way? I have over 100 variables, making all of them global would be a lot ot work...
Your original question mentioned one variable. There could be other ways, why are you trying to share 100 variables between a plugin and global script space? Maybe that can be redesigned. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | top |
|
Posted by
| Nick Gammon
Australia (22,986 posts) bio
Forum Administrator |
Date
| Reply #9 on Sun 14 Oct 2018 05:28 AM (UTC) |
Message
| There are ways for the main script file (or main triggers and aliases) to communicate with plugins. One is CallPlugin.
This lets you pass information down to plugins.
Another is to "Execute" an alias, which the plugin then captures.
For example:
TellMyPlugin canStab = false
That could match an alias in your plugin (TellMyPlugin *) which might execute the rest of the line as Lua code. That could be dangerous, but it is an example of what you might do . |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | top |
|
Posted by
| Nick Gammon
Australia (22,986 posts) bio
Forum Administrator |
Date
| Reply #10 on Sun 14 Oct 2018 05:30 AM (UTC) |
Message
| Having separate script spaces is supposed to be a feature. Putting aside the fact that the plugin might be using a different language than the main script file, it stops you accidentally using some variable (eg. "counter") in different plugins and not realizing they are interacting with each other. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | top |
|
Posted by
| Rdiniz
Brazil (21 posts) bio
|
Date
| Reply #11 on Sun 14 Oct 2018 03:34 PM (UTC) |
Message
|
Thanks for the info, I didn't know that module. That will sure help in other situations...
Nick Gammon said:
Your original question mentioned one variable. There could be other ways, why are you trying to share 100 variables between a plugin and global script space? Maybe that can be redesigned.
I tried to make it a little simpler than it really was. The specific function I was working on uses 5 'global' variables, which isn't bad to expose as global. The 100 I mentioned are all variables I use on script file, but I'm sure I don't need all of them exposed.
Nick Gammon said:
There are ways for the main script file (or main triggers and aliases) to communicate with plugins. One is CallPlugin.
This lets you pass information down to plugins.
(function=CallPlugin)
Another is to "Execute" an alias, which the plugin then captures.
For example:
TellMyPlugin canStab = false
That could match an alias in your plugin (TellMyPlugin *) which might execute the rest of the line as Lua code. That could be dangerous, but it is an example of what you might do .
So many possibilities. I liked and tried this execute as an alias and it worked perfectly. Just a small issue, and I'm not sure it was just a feeling, I noticed a little delay on the execution of the alias, but nothing too bad.
Thanks again for all the info you two. | top |
|
Posted by
| Nick Gammon
Australia (22,986 posts) bio
Forum Administrator |
Date
| Reply #12 on Mon 15 Oct 2018 05:58 AM (UTC) |
Message
| CallPlugin would probably be faster (I think Fiendish uses it a lot). However the alias idea should not be noticeably slow. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | top |
|
Posted by
| Rdiniz
Brazil (21 posts) bio
|
Date
| Reply #13 on Mon 15 Oct 2018 03:21 PM (UTC) |
Message
| I used the alias approach to make the other way, the plugin calling a function on the main script file, using the main script space, which is what I wanted on the first place. | top |
|
The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).
To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.
25,841 views.
It is now over 60 days since the last post. This thread is closed.
  Refresh page
top
Quick links:
MUSHclient.
MUSHclient help.
Forum shortcuts.
Posting templates.
Lua modules.
Lua documentation.
Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.