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
➜ Lua
➜ Adding to saved table
It is now over 60 days since the last post. This thread is closed.
Refresh page
Posted by
| Rene
(46 posts) Bio
|
Date
| Mon 06 Nov 2017 11:36 PM (UTC) Amended on Tue 07 Nov 2017 12:46 AM (UTC) by Nick Gammon
|
Message
| I am trying to make my plugin dynamic so I can easily add more types of scrolls to the scroll table, and then next time the plugin will have that type added. However I found if it was saving and loading the table, any changes I made in the file didn't affect it and it would load the table again without those additions.
What I did to get around that was have it save the table on another name, and then run comparisons between the tables and then correct for it. I was curious about anyone's comments if there is a better way to do this.
function OnPluginInstall ()
--[[
scrolls = {
{"jpct", "jogloran's portal of cheaper travel"},
{"ccc", "chrenedict's corporeal covering"},
{"tpa", "transcendent pneumatic alleviator"}
}
]]--
assert (loadstring (GetVariable ("scrolls_saved") or "")) ()
if scrolls_saved ~= nil then
for i, v in ipairs (scrolls) do
if scrolls_saved[i] ~= nil and scrolls[i][1] == scrolls_saved[i][1] then
scrolls[i] = scrolls_saved[i]
else
for k, v in ipairs (scrolls_saved) do
if scrolls[i][1] == scrolls_saved[k][1] then
scrolls[i] = scrolls_saved[k]
end
end
end
end --for
end
DebugNote("Loading...")
end -- function OnPluginInstall
-- on saving state, convert Lua table back into string variable
-- save_simple is for simple tables that do not have cycles (self-reference)
-- or refer to other tables
function OnPluginSaveState ()
DebugNote("SaveState:Saving...")
SetVariable ("scrolls_saved",
"scrolls_saved = " .. serialize.save_simple (scrolls))
end -- function OnPluginSaveState
Now if I want to run the plugin again without it having the part of ccc I can just change the table to be:
scrolls = {
{"jpct", "jogloran's portal of cheaper travel"},
{"tpa", "transcendent pneumatic alleviator"}
}
And the plugin will no longer have the ccc reference but will have the values right for the ones left.
Comments please.
[EDIT] Changed from quote tags to code tags. | Top |
|
Posted by
| Nick Gammon
Australia (23,133 posts) Bio
Forum Administrator |
Date
| Reply #1 on Tue 07 Nov 2017 12:49 AM (UTC) |
Message
| I don't see why you have to go through this rigmarole.
Just save as "scrolls" and load it back. Can you post the code that doesn't work please? |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Rene
(46 posts) Bio
|
Date
| Reply #2 on Tue 07 Nov 2017 01:08 AM (UTC) Amended on Tue 07 Nov 2017 03:52 AM (UTC) by Nick Gammon
|
Message
|
function OnPluginInstall ()
scrolls = {
{"jpct", "jogloran's portal of cheaper travel"},
{"ccc", "chrenedict's corporeal covering"},
{"tpa", "transcendent pneumatic alleviator"}
}
assert (loadstring (GetVariable ("scrolls") or "")) ()
end -- function OnPluginInstall
function OnPluginSaveState ()
SetVariable ("scrolls",
"scrolls = " .. serialize.save_simple (scrolls))
end -- function OnPluginSaveState
I think I explained, if I do it like this, even if I later change the table to be:
scrolls = {
{"jpct", "jogloran's portal of cheaper travel"},
{"tpa", "transcendent pneumatic alleviator"}
}
it will still load the first way since the saved table includes the {"ccc", "chrenedict's corporeal covering"},
line, and I want it to lose that line if I edit it out later or add it back in to allow customization according to which ones I want. | Top |
|
Posted by
| Nick Gammon
Australia (23,133 posts) Bio
Forum Administrator |
Date
| Reply #3 on Tue 07 Nov 2017 03:53 AM (UTC) |
Message
|
 |
To make your code more readable please use [code] tags as described here.
|
Please use code tags, not quote tags. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Nick Gammon
Australia (23,133 posts) Bio
Forum Administrator |
Date
| Reply #4 on Tue 07 Nov 2017 04:21 AM (UTC) Amended on Tue 07 Nov 2017 08:52 AM (UTC) by Nick Gammon
|
Message
| I suspect the problem is in the code you haven't shown. I made up a test plugin which works as expected:
 |
To save and install the test_save_state plugin do this:
- Copy between the lines below (to the Clipboard)
- Open a text editor (such as Notepad) and paste the plugin into it
- Save to disk on your PC, preferably in your plugins directory, as test_save_state.xml
- Go to the MUSHclient File menu -> Plugins
- Click "Add"
- Choose the file test_save_state.xml (which you just saved in step 3) as a plugin
- Click "Close"
|
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>
<muclient>
<plugin
name="test_save_state"
author="Nick Gammon"
id="8982fd6b363edbc7ca7f2469"
language="Lua"
purpose="See: http://www.gammon.com.au/forum/?id=14095 "
save_state="y"
date_written="2017-11-07 14:56:56"
requires="5.05"
version="1.0"
>
</plugin>
<!-- Aliases -->
<aliases>
<alias
match="bar *"
enabled="y"
send_to="12"
sequence="100"
>
<send>
item = table.remove (scrolls, %1)
if item then
print ("Removed:")
tprint (item)
else
print ("Nothing removed")
end -- if
</send>
</alias>
<alias
match="foo *=*"
enabled="y"
send_to="12"
sequence="100"
>
<send>
table.insert (scrolls, { "%1", "%2" } )
Note ("Inserted %1, %2 into scrolls") </send>
</alias>
<alias
match="showscrolls"
enabled="y"
send_to="12"
sequence="100"
>
<send>
print ("Scrolls:")
tprint (scrolls)</send>
</alias>
</aliases>
<!-- Script -->
<script>
<![CDATA[
require "tprint"
require "serialize"
function OnPluginInstall ()
scrolls = {
{"jpct", "jogloran's portal of cheaper travel"},
{"ccc", "chrenedict's corporeal covering"},
{"tpa", "transcendent pneumatic alleviator"}
}
assert (loadstring (GetVariable ("scrolls") or "")) ()
end -- function OnPluginInstall
function OnPluginSaveState ()
SetVariable ("scrolls",
"scrolls = " .. serialize.save_simple (scrolls))
end -- function OnPluginSaveState
]]>
</script>
</muclient>
It initially sets the table to the three items like you had. However you can use "foo" to add items, eg.
And you can use "showscrolls" to show the current table in the plugin:
You can also use "bar" to remove items, eg.
That removes the first item in the table.
Now if you reload the plugin (forcing it to go through the save and reload procedure) then the new table is still there.
Make sure you have this in the plugin header:
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Nick Gammon
Australia (23,133 posts) Bio
Forum Administrator |
Date
| Reply #5 on Tue 07 Nov 2017 04:29 AM (UTC) Amended on Tue 07 Nov 2017 04:31 AM (UTC) by Nick Gammon
|
Message
| And as a design note, you might be better off having a single table, rather than a table of tables, eg.
scrolls = {
jpct = "jogloran's portal of cheaper travel",
ccc = "chrenedict's corporeal covering",
tpa = "transcendent pneumatic alleviator",
}
That lets you index directly into the table, rather than having to scan it to find a match for a scroll. eg.
scrolls = {
jpct = "jogloran's portal of cheaper travel",
ccc = "chrenedict's corporeal covering",
tpa = "transcendent pneumatic alleviator",
}
print (scrolls ["ccc"]) --> chrenedict's corporeal covering
Then you can add new items easily:
scrolls ['heal'] = "greater heal"
And remove items:
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Rene
(46 posts) Bio
|
Date
| Reply #6 on Tue 07 Nov 2017 06:30 AM (UTC) |
Message
| Yes, but lets say I no longer want ccc on my table, if I remove it from within the plugin it will continue loading as it is in the saved table. My point was to allow me to edit it out of the plugin or add in another similar table in the plugin that will then cause it to no longer have that table in the saved table. | Top |
|
Posted by
| Nick Gammon
Australia (23,133 posts) Bio
Forum Administrator |
Date
| Reply #7 on Tue 07 Nov 2017 08:50 AM (UTC) |
Message
| No it won't, because next time the plugin saves the new table will overwrite the old one. Please post a complete plugin that proves what you are saying. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Nick Gammon
Australia (23,133 posts) Bio
Forum Administrator |
Date
| Reply #8 on Tue 07 Nov 2017 08:52 AM (UTC) |
Message
| Do you have:
in the plugin header? I've made the relevant line bold in reply #4. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Rene
(46 posts) Bio
|
Date
| Reply #9 on Tue 07 Nov 2017 07:41 PM (UTC) |
Message
| The plugin you posted is good enough for my point. I will try to explain better.
I want the plugin to be able to be edited from the .xml file, and instead change it to
scrolls = {
{"tma", "too much attention"},
{"iinw", "it is not working"}
}
it will not actually change anything as the table will load the way it was set up initially.
I know you can add\remove from it within the code, but I want to be able to edit the file and add\subtract as many as I want from there, and what I wrote was doing that.
Also on your point of having a table of tables, I want to be able to change them easily so I never reference them by name so no matter what I change it will continue to work.
Thanks. | Top |
|
Posted by
| Nick Gammon
Australia (23,133 posts) Bio
Forum Administrator |
Date
| Reply #10 on Tue 07 Nov 2017 08:10 PM (UTC) Amended on Tue 07 Nov 2017 08:24 PM (UTC) by Nick Gammon
|
Message
| Editing the XML file, and saving/restoring the variable seem to me to be mutually exclusive. If you are going to edit the XML file (something I wouldn't recommend) then don't bother saving/restoring the scrolls table.
However it is much easier to leave the XML file alone (thus it can be shared with different characters) and store all the data in the variables. The "save state" file is unique per plugin/world combination.
I would start with nothing in the scrolls table in the initial XML - and then just add new ones as required (or delete them) - letting the save-state file hold the current position.
An alternative is to use a Sqlite3 database instead. They aren't particularly hard to use, see: http://www.gammon.com.au/sql
If you edit the XML you would need to force the plugin to be reloaded. Just changing it on disk would have no effect until next time you loaded the plugin. Editing the XML just seems to me to be a difficult, error-prone, and hard-to-use technique. What happens if there is a crash half-way through rewriting the XML file? Then you have lost the entire plugin. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Rene
(46 posts) Bio
|
Date
| Reply #11 on Tue 07 Nov 2017 08:57 PM (UTC) |
Message
| Well, I want it to ship for friends and stuff set up with what I'd expect them to want.
Either way, the code I wrote seems to do what I wanted, was wondering if anyone had a better way.
Always keep backups.
Also on a side, is it possible to lessen the password requirements? It's overly secure and makes it too hard for me to remember the password myself, and what kind of information is in my account anyways?
Thanks. | Top |
|
Posted by
| Nick Gammon
Australia (23,133 posts) Bio
Forum Administrator |
Date
| Reply #12 on Tue 07 Nov 2017 09:01 PM (UTC) |
Message
|
Rene said:
Well, I want it to ship for friends and stuff set up with what I'd expect them to want.
The way you had it would do that (and in my example plugin). The initial table, in the plugin, is the default value. Once you start making changes then the changed table becomes the "real" one. You don't need to change the plugin.
Quote:
Also on a side, is it possible to lessen the password requirements? It's overly secure and makes it too hard for me to remember the password myself, and what kind of information is in my account anyways?
Thanks.
You aren't the first to complain! However you only have to enter it once. Use a password manager, and edit your profile to keep yourself logged in for a year at a time (see "Cookie Expires" field. I have mine set to 365 days). |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | 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.
32,662 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top