Register forum user name Search FAQ

Gammon Forum

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 ➜ Keeping track of my empty vials

Keeping track of my empty vials

It is now over 60 days since the last post. This thread is closed.     Refresh page


Pages: 1 2  

Posted by Marvimoonflair   Canada  (8 posts)  Bio
Date Fri 30 Apr 2010 07:03 AM (UTC)
Message
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
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #1 on Fri 30 Apr 2010 08:33 AM (UTC)
Message
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


- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Marvimoonflair   Canada  (8 posts)  Bio
Date Reply #2 on Fri 30 Apr 2010 07:41 PM (UTC)
Message
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?
Top

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #3 on Fri 30 Apr 2010 08:32 PM (UTC)
Message
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.

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
Top

Posted by Marvimoonflair   Canada  (8 posts)  Bio
Date Reply #4 on Sat 01 May 2010 05:11 AM (UTC)
Message
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 ;)
Top

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #5 on Sat 01 May 2010 05:54 AM (UTC)

Amended on Sat 01 May 2010 06:04 AM (UTC) by Twisol

Message
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)

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
Top

Posted by Marvimoonflair   Canada  (8 posts)  Bio
Date Reply #6 on Sat 01 May 2010 08:51 PM (UTC)
Message
Sounds great!

...Now how do I pack up the table into a variable using a plugin or script? Sorry to back track a little!!
Top

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #7 on Sat 01 May 2010 09:00 PM (UTC)
Message
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.

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
Top

Posted by Marvimoonflair   Canada  (8 posts)  Bio
Date Reply #8 on Sun 02 May 2010 02:18 PM (UTC)

Amended on Sun 02 May 2010 04:17 PM (UTC) by Marvimoonflair

Message
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.
Top

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #9 on Sun 02 May 2010 04:51 PM (UTC)
Message
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!

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
Top

Posted by Marvimoonflair   Canada  (8 posts)  Bio
Date Reply #10 on Sun 02 May 2010 05:26 PM (UTC)
Message
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
Top

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #11 on Sun 02 May 2010 05:40 PM (UTC)

Amended on Sun 02 May 2010 05:41 PM (UTC) by Twisol

Message
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.

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
Top

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #12 on Sun 02 May 2010 05:42 PM (UTC)
Message
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

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
Top

Posted by Marvimoonflair   Canada  (8 posts)  Bio
Date Reply #13 on Sun 02 May 2010 06:45 PM (UTC)
Message
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!
Top

Posted by Marvimoonflair   Canada  (8 posts)  Bio
Date Reply #14 on Mon 03 May 2010 06:00 AM (UTC)

Amended on Mon 03 May 2010 06:12 AM (UTC) by Marvimoonflair

Message
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!
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.


35,520 views.

This is page 1, subject is 2 pages long: 1 2  [Next page]

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.