Keeping track of my empty vials

Posted by Marvimoonflair on Fri 30 Apr 2010 07:03 AM — 16 posts, 51,990 views.

Canada #0
Hey folks! Long time reader, first time poster! I play Achaea, Aetolia and the occasional Lusternia on MUSH and I love it! Nick, you are the MAN.

In a nutshell, I want to make an alias that would send the individual ID numbers of the vials I choose so I have a master list to refer to when I do my regular pitstop in the city!

I'm still really newb at Lua scripting, dipping my toe into if/then functions (which take a lot of time for me to get to work, if they do at all!) but I'd really love to learn about these magical things called tables :)

Ideally, I'd like to have an alias that I would type out and then append the vial ID number and then what I needed to fill it with, sending both values to a table. (i.e. addempty 123456 health) From what I understand about tables, the ID number would act as the key and "health" would be the value associated with it? (If I'm right, it'll be nice to know 2 months of tedious Access classes at school weren't useless!) And then have a second alias that would print out this info, IDs and elixir to be refilled all nice and side-by-side. Then maybe a third alias that'll remove an item from the table as I fill the vial.

I am but the lowly bottom of the totem pole compared to all your collective prowess! Teach me your ways!

~Marvi
Australia Forum Administrator #1
You are on the right track. The key is what leads to the value, so-to-speak.

eg.


vials = { }  -- create table

vials [12345] = "health"  -- key 12345 has value "health"
vials [12332] = "sadness"

-- loop through table
for k, v in pairs (vials) do
  print (k, v)   -- print each one
end -- for

vials [12345] = nil  -- delete this entry

Canada #2
Thanks Nick! It works great!!

Now might there be a way to add up how many items are on my table so I can stick a number at the end of my awesome list?
USA #3
Hallos, Marvi!

The simplest solution is probably just to count the pairs in the list:

local count = 0
for k,v in pairs(vials) do
  count = count + 1
end

Note(("You have %d vials"):format(count))


Another option is to keep a count separately and add/subtract from it when you add/remove a vial in the list. That way you don't have to count the vials every time. I don't think you'll have enough vials to make much difference in speed, though.
Canada #4
So the script is working great buuuuut...

When I close down MUSH and start everything back up again, my table is gone!!

How can I set it up so I don't have to re-input all my vials again?

PS - HI SOLUDRA! FANCY SEEING YOU HERE ;)
USA #5
Haha, hi. ;)

The usual solution is to pack up the table into a MUSHclient variable (using GetVariable()/SetVariable()), which are automatically saved between sessions. (If this code is in a plugin, it needs the save_state="y" attribute at the top too, in the <plugin> tag.)

Since MUSHclient comes with a handy serialize.lua module, this is a piece of cake.


In a plugin:
require("serialize")

vials = nil

function OnPluginInstall()
  local data = GetVariable("vials") or "{}"
  vials = assert(loadstring("return " .. data))()
end

function OnPluginSaveState()
  local data = serialize.save_simple(vials)
  SetVariable("vials", data)
end


In a script file, it's about the same, but there's an extra step involved. You need to go to Game -> Configure -> Scripting, and put function names in the "Save" and "Open" boxes at the bottom. Then you just create functions with those names, but it's just about the same example as above.
require("serialize")

vials = nil

function ScriptOnOpen()
  local data = GetVariable("vials") or "{}"
  vials = assert(loadstring("return " .. data))()
end

function ScriptOnSave()
  SetVariable("vials", serialize.save_simple(vials))
end


You don't really need to define an on-install/on-open function, because the plugin and script files are immediately executed when loaded anyways. I like to do it to make it more clear what I'm doing, though.


(edited several times)
Amended on Sat 01 May 2010 06:04 AM by Twisol
Canada #6
Sounds great!

...Now how do I pack up the table into a variable using a plugin or script? Sorry to back track a little!!
USA #7
Er, did I miss something in my examples? I showed how to take a table and put it into a MUSHclient variable. The code to do that specifically is in the OnPluginSaveState function. The code to get it back into a table is in OnPluginInstall.
Canada #8
Working through it now, I guess.

I'll come back if I really hit a wall! (Which may be upcoming soon!)

I think my problem is that I've never written a plugin before. I'm kinda going by how I see other people's lua plugins written. We'll see how it goes.

EDIT - Okay I got it going now! I was just being silly and for some reason couldn't see how to stick the script into the plugin via the Plugin Wizard (the "Script" tab, duh!)

Thanks! I'll be sure to post again should I run into any problems or want to add features that I don't now how to add.
Amended on Sun 02 May 2010 04:17 PM by Marvimoonflair
USA #9
Marvimoonflair said:
I think my problem is that I've never written a plugin before. I'm kinda going by how I see other people's lua plugins written. We'll see how it goes.


Well, I gave an example where you could use the plain script file instead of a plugin, you just have to go through that one extra step I mentioned. ;)

Glad you've got it working!
Canada #10
So far it's working pretty okay buuuut...

Why can't I see any of my aliases/triggers/variables when I bring up the Configuration menu?

I mean all my aliases and triggers are still working and things but they're not up there anymore in case I want to refer so something.

Did I make a whoopsies?

Plugin here: http://pastebin.com/TwDj86VF
USA #11
Plugins are kept entirely separate from your world. You can think of the world as its own plugin, so it has all its own triggers, aliases, etc visible.

It's a bit annoying, yeah, but there's no indication of it changing anytime soon. Besides, plugins are made to be distributable. If a plugin dumped all of its details in plain sight, and obscured your own triggers, aliases, etc., that wouldn't be very nice.
Amended on Sun 02 May 2010 05:41 PM by Twisol
USA #12
Also, based on everything in the plugin, I think you put pretty much everything in your world into the plugin, instead of putting only the vial-related stuff in. :P
Canada #13
Haha guess I'll just dissect the plugin till it's just the vial stuffs, then reload the variables and such from separate files!

I'm done attempting to code today. It's time to go outside!

Thanks!
Canada #14
Alrighty, re-done plugin here! http://pastebin.com/7mrAMAkF

As you can see I took out all the world's configurable thingies out of it to leave just the vial stuff in there but now it's back to not keeping whatever I put into the table after I close the world and MUSH down and start everything back up...again.

I'm determined to work this out till it works!!

EDIT: Could it have something to do with the 'vials = nil' line that's after the require("serialize")?? I don't really know so forgive me if it sounds completely ludicrous!
Amended on Mon 03 May 2010 06:12 AM by Marvimoonflair
USA #15
Your first problem is that there's no way to put anything in that table. Remember, plugins are entirely separate from eachother and the world. Each plugin has its own separate script space, so 'vials' in the plugin is a different variable from 'vials' in the world.

I think you'll want a pair of aliases for this:

<aliases>
  <alias
   match="^\s*setvial\s+(\d+)\s+(\w+)\s*$"
   enabled="y"
   regexp="y"
   send_to="12"
   ignore_case="y"
   sequence="100"
  >
  <send>vials["%1"] = "%2"</send>
  </alias>
  
  <alias
   match="^\s*unsetvial\s+(\d+)\s*$"
   enabled="y"
   regexp="y"
   send_to="12"
   ignore_case="y"
   sequence="100"
  >
  <send>vials["%1"] = nil</send>
  </alias>
</aliases>


Usage:
setvial 123456 health
unsetvial 123456

Add that XML to the plugin file.