Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are
spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the
password reset link.
Due to spam on this forum, all posts now need moderator approval.
Entire forum
➜ MUSHclient
➜ General
➜ Changing the state of an alias
Changing the state of an alias
|
It is now over 60 days since the last post. This thread is closed.
Refresh page
Posted by
| Dakine
(22 posts) Bio
|
Date
| Tue 14 Apr 2015 05:51 PM (UTC) Amended on Tue 14 Apr 2015 05:52 PM (UTC) by Dakine
|
Message
| Hi,
MUD: Aaardwolf
Very new to Lua and apologies if this is obvious. I may not be searching for the right terms!
I would like to change certain aliases on reaching a certain level.
As an example, I have a primary spell alias which I then put into one of the numberpad keys.
Example Alias:
<alias
name="mageprimcast"
match="^csc(?:[ ]+(.*))?$"
enabled="y"
regexp="y"
send_to="10"
sequence="100"
>
<send>c 346 %1</send>
</alias>
Currently I have to manually change this alias on obtaining a more powerful spell. This gets a bit tedious after awhile.
I think I can use a trigger like this:
<trigger
enabled="y"
match="you reach level {*)."
send_to="10"
sequence="100"
>
<send>if %1 == 46 then
SetAliasOption ("mageprimcast", "send", "c 347 %1")
end --if
</send>
</trigger>
However, I've not been successful with the above trigger during testing.
These are contained in a plugin. Two questions.
1) Is this the correct method of changing the send info of an alias? Is there a better way?
2) How do I maintain this new value when I close the and reload the mushclient?
thanks
Dak | Top |
|
Posted by
| Nick Gammon
Australia (23,162 posts) Bio
Forum Administrator |
Date
| Reply #1 on Tue 14 Apr 2015 08:06 PM (UTC) Amended on Tue 14 Apr 2015 08:07 PM (UTC) by Nick Gammon
|
Message
|
if %1 == 46 then
SetAliasOption ("mageprimcast", "send", "c 347 %1")
Your method is pretty good, however the %1 will be 46, not %1 any more. Try this:
if %1 == 46 then
SetAliasOption ("mageprimcast", "send", "c 347 %%1")
The %%1 becomes %1 after the alias is expanded.
Quote:
2) How do I maintain this new value when I close the and reload the mushclient?
You can't directly change the aliases in a plugin, because plugins are read-only (they can be shared between multiple world files).
The simplest way is probably to "remember" alias changes in a table, serialize that table, and then on plugin load re-apply all the changes.
See: http://www.gammon.com.au/forum/?id=4960
For example:
if %1 == 46 then
SetAliasOption ("mageprimcast", "send", "c 347 %%1")
aliasChanges = aliasChanges or { } -- ensure table exists
aliasChanges ["mageprimcast"] = "c 347 %%1" -- new value for this one
end
In reply #6 of the link above ( http://www.gammon.com.au/forum/?id=4960&reply=6#reply6 ) you can see how to change your plugin to save that table (change the variable name). eg.
require "serialize" -- needed to serialize table to string
aliasChanges = {} -- ensure table exists, if not loaded from variable
function OnPluginSaveState ()
SetVariable ("aliasChanges",
"aliasChanges = " .. serialize.save_simple (aliasChanges))
end -- function OnPluginSaveState
Then in the plugin install put the aliases back:
function OnPluginInstall ()
assert (loadstring (GetVariable ("aliasChanges") or "")) ()
-- put aliases back to remember the changes
for k, v in pairs (aliasChanges) do
SetAliasOption (k, "send", v) -- change this alias "k" (key) to send message "v" (value)
end -- for
end -- function OnPluginInstall
Make sure the plugin is set to "save state". ie. at the start of the plugin:
<plugin
name="What_Ever"
...
save_state="y"
...
>
Re-reading your post it isn't clear how the 346 becomes 347. Should that be another argument to the alias, or is that a typo? |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Dakine
(22 posts) Bio
|
Date
| Reply #2 on Tue 14 Apr 2015 08:43 PM (UTC) |
Message
| Hi Nick,
Thanks for such a quick response and comprehensive reply.
It's a lot to take in but I think I understand the concept. At least as an overview and can see how it would provide the solution. It's something to work towards.
The c 347 wasn't a typo. The c 346 was the current (spell) value of the send of the alias, which I manually go in and edit. The c 347 was the next (spell) value I would edit. Very clunky!
Thanks | Top |
|
Posted by
| Nick Gammon
Australia (23,162 posts) Bio
Forum Administrator |
Date
| Reply #3 on Wed 15 Apr 2015 06:42 AM (UTC) |
Message
| In that case the "alias that changes an alias" should probably also change the spell number. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Dakine
(22 posts) Bio
|
Date
| Reply #4 on Thu 16 Apr 2015 06:29 PM (UTC) Amended on Thu 16 Apr 2015 08:10 PM (UTC) by Dakine
|
Message
| I'm making progress..It's simpler than I first thought it was after reviewing the code.
However, I have a question if the following still applies.
local t = {}
setfenv (assert (loadstring (aliasChanges)), t) ()
access it by:
See: http://www.gammon.com.au/forum/?id=4960&reply=7#reply7
I'm always one for protecting ones own code and would like to use this if it is still relevant.
My question: does the OnPluginSaveState code below need change by adding the t?
function OnPluginSaveState ()
SetVariable ("aliasChanges",
"aliasChanges = " .. serialize.save_simple (t.aliasChanges))
end -- function OnPluginSaveState
I kept moving where the t should be until I was slightly confused.
addendum - I try to keep code quite clean and palm off code to other .lua files. On further testing I'm unable to update the unserialized table values from these .lua files only from the exampleplugin.xml file.
Is there a way to do this or only from the exampleplugin.xml file? I thought perhaps it was the order in which the requires were loaded, but not found a solution yet.
Thanks | Top |
|
Posted by
| Nick Gammon
Australia (23,162 posts) Bio
Forum Administrator |
Date
| Reply #5 on Thu 16 Apr 2015 07:52 PM (UTC) |
Message
| Let's see. If you do a normal serialize, you get what amounts to assignment statements like this:
mobs = {} -- create mobs table
mobs.kobold = { name = 'Foo' }
mobs.worm = { name = 'Bar' }
require "serialize"
print ((serialize.save ("mobs")))
Output:
mobs = {}
mobs.kobold = {}
mobs.kobold.name = "Foo"
mobs.worm = {}
mobs.worm.name = "Bar"
So when you do a "loadstring" it is executing those statements, repopulating your mobs table (in global namespace).
If you want to leave the global namespace "pure" you could serialize into a table like this:
mobs = {} -- create mobs table
mobs.kobold = { name = 'Foo' }
mobs.worm = { name = 'Bar' }
saved_mobs = serialize.save ("mobs")
local mud_stuff = {}
setfenv (assert (loadstring (saved_mobs)), mud_stuff) ()
require "tprint"
tprint (mud_stuff)
Output:
"mobs":
"kobold":
"name"="Foo"
"worm":
"name"="Bar"
This means that your recovered table is not in mud_stuff.mobs, not just mobs. This might be confusing because you are now a level down from when you did the serializing.
However if you use serialize.save_simple then you can save from a lower-level table like this:
mud_stuff = {}
mud_stuff.mobs = {} -- create mobs table
mud_stuff.mobs.kobold = { name = 'Foo' }
mud_stuff.mobs.worm = { name = 'Bar' }
saved_mobs = serialize.save_simple (mud_stuff.mobs)
print ("Saved mobs ...")
print (saved_mobs)
local mud_stuff = {}
setfenv (assert (loadstring ("mobs = " .. saved_mobs)), mud_stuff) ()
print ("Reloaded mobs ...")
require "tprint"
tprint (mud_stuff)
Output:
Saved mobs ...
{
kobold = {
name = "Foo",
},
worm = {
name = "Bar",
},
}
Reloaded mobs ...
"mobs":
"kobold":
"name"="Foo"
"worm":
"name"="Bar"
serialize.save_simple doesn't know the name of the "root" table (it only has its address not its name) so it isn't in the saved data. That's why we have to put the variable assignment into the loadstring function call.
If you are just writing for yourself you probably don't need this extra level of complexity, because presumably you trust your own code. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Dakine
(22 posts) Bio
|
Date
| Reply #6 on Thu 16 Apr 2015 10:31 PM (UTC) Amended on Fri 17 Apr 2015 02:57 PM (UTC) by Dakine
|
Message
| I've got the beginnings of my code working thanks to your replies.
As you mention, it's probably best to keep it simple to start with. At least while learning the fundamentals.
Dak | 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.
22,034 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top