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 ➜ how to do specific things based on alias variables.

how to do specific things based on alias variables.

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


Posted by Lilbopeep   USA  (42 posts)  Bio
Date Wed 02 Sep 2009 07:49 AM (UTC)
Message
hello!

I'm still very new to lua but I thought I would try something that seemed simple, but I got stuck! drat.

For starters, I am stuck on something that's probably simple, but I don't know how to store a word in a variable -

for instance, I want to have an alias that allows me to 'shortcut' making scrolls in my game, which is a 5 step process. I could make an alias for every spell I might want to make a scroll for, but this seemed like a more interesting solution -

I thought I could do something like
scribe_scroll = tostring (%1)
Note "Scroll String is: ", scribe_scroll)

but I keep getting 'Scroll String is: nil'

However, whenever I do get that problem solved, my next step will be (psuedo)

Send ("get parchment <container_keyword>")
Send ("hold parchment")

if scribe_scroll = <value>
Send ("scribe <target spell>")
else do
if scribe_scroll = <value>
Send ("scribe <target spell>")
else do
Note ("You didn't type the right keywords, check again")
end

Send ("remove scroll")
Send ("put scroll <container_keyword>")

Something like that?


,.~`'~.,Dance Magic Dance,.~`'~.,
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #1 on Wed 02 Sep 2009 08:15 AM (UTC)
Message
First:
Template:faq=32 Please read the MUSHclient FAQ - point 32.

- Nick Gammon

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

Posted by Lilbopeep   USA  (42 posts)  Bio
Date Reply #2 on Wed 02 Sep 2009 08:57 AM (UTC)
Message
Duh. *bonk self*

RFTM, princess.


if "%1" == "ms" then
  Note ("You are trying to make a Meteor Swarm scroll")
else
if "%1" == "cl" then
  Note ("You are trying to make a Chain Lightning scroll")
else
  Note ("Invalid argument in scribe syntax")
end -- if
end -- if


Just for testing, I had it throw me in some notes and that seems to work just fine, so this is gonna work out nice.

So next, I think for each 'if' check I would set a common variable like scribe_spell to be whatever the spell actually was, then tell it to run a function with that variable? Any pointers on where to start there?


,.~`'~.,Dance Magic Dance,.~`'~.,
Top

Posted by Fadedparadox   USA  (91 posts)  Bio
Date Reply #3 on Wed 02 Sep 2009 09:22 AM (UTC)

Amended on Wed 02 Sep 2009 09:32 AM (UTC) by Fadedparadox

Message
Would be easier to combine the two if statements to one with an elseif... like so.

if "%1" == "ms" then
  Note ("You are trying to make a Meteor Swarm scroll")
elseif "%1" == "cl" then
  Note ("You are trying to make a Chain Lightning scroll")
else
  Note ("Invalid argument in scribe syntax")
end -- if


As for using a variable with all the information, a table would be perfect. Think of a lua table as a variable that holds other variables. You can make the table by setting it equal to an opening and closing curly brace, like so:

scrolls = {}


Then you can put information into it/read it with scrolls[scrollname], like so...

scrolls = {}
scrolls["ms"] = "Meteor Swarm"


print (scrolls["ms"])
-> Meteor Swarm


From there, we can easily make a table with both pieces of data.
scrolls = {}
scrolls["ms"] = "Meteor Swarm"
scrolls["cl"] = "Chain Lightning"


Then make your if statement read it instead...

if scrolls["%1"] then
  Note ("You are trying to make a " .. scrolls["%1"] .. " scroll")
else
  Note ("Invalid argument in scribe syntax")
end -- if
Top

Posted by Lilbopeep   USA  (42 posts)  Bio
Date Reply #4 on Wed 02 Sep 2009 09:31 AM (UTC)
Message
oh, yeah, that saves me a couple lines for each spell, which is good because there are quite a few.

,.~`'~.,Dance Magic Dance,.~`'~.,
Top

Posted by Fadedparadox   USA  (91 posts)  Bio
Date Reply #5 on Wed 02 Sep 2009 09:32 AM (UTC)
Message
Lilbopeep said:

oh, yeah, that saves me a couple lines for each spell, which is good because there are quite a few.

I edited my post to answer your question.
Top

Posted by Lilbopeep   USA  (42 posts)  Bio
Date Reply #6 on Wed 02 Sep 2009 09:54 AM (UTC)
Message
Wonderful -


scrolls = {}
scrolls["ms"] = "meteor swarm"
scrolls["cl"] = "chain lightning"

  if scrolls["%1"] then
     Note ("You are trying to make a '" .. scrolls["%1"] .. "' scroll")
	  Send ("get parchment hole")
	  Send ("hold parchment")
	  Send ("scribe '" .. scrolls["%1"] .. "'")
          Send ("remove scroll")
          Send ("put scroll cloak")
  else
     Note ("Invalid argument in scribe syntax")
  end -- if


Since I already have a table, I am wondering if on false, there is a way to print the list of variables and the spells they can be scribed through the notes?

This is been so much fun!

,.~`'~.,Dance Magic Dance,.~`'~.,
Top

Posted by Fadedparadox   USA  (91 posts)  Bio
Date Reply #7 on Wed 02 Sep 2009 10:10 AM (UTC)
Message
Lilbopeep said:

Since I already have a table, I am wondering if on false, there is a way to print the list of variables and the spells they can be scribed through the notes?

This is been so much fun!

Sure, just use a for statement with pairs. It looks like the following:
for key, value in pairs (table) do
  --script here
end -- for

What that does it goes through the table after pairs and in parentheses, and assigns each key/value pair to the variables separated by commas before the 'pairs' statement, then does what's between 'do' and 'end' for each of those pairs. Here's an example that should make that make more sense.


for shortname, spell in pairs (scrolls) do
  Note (shortname .. ": " .. spell)
end -- for


That will, for each key/value in 'scrolls', set the key to shortname and value to spell, then print them out with ": " between them (minus quotes of course).
Top

Posted by Lilbopeep   USA  (42 posts)  Bio
Date Reply #8 on Wed 02 Sep 2009 11:07 AM (UTC)
Message
Can't really thank you enough for the hand-holding, I'm such a newb.

I think this is the next logical step though, I've (just) start reading up on sqlite and thought that it might be more handy to have an actual database saved to disk with all these values, so I could manually add and remove them the shortname with the spellname?


,.~`'~.,Dance Magic Dance,.~`'~.,
Top

Posted by Lilbopeep   USA  (42 posts)  Bio
Date Reply #9 on Wed 02 Sep 2009 11:46 AM (UTC)
Message
so far, if I am understanding this right, my task would be:

create the database file if it doesn't exist
create the scribe table if it doesn't exist

add an alias that would allow me to add a new shortname and spell name to the database

change the current alias to 'search' for the shortname, plug in the spell name to the script, and then make the additional change to the false to print off all the available commands

Who would have thought for little sentences would make my brain hurt so much.

,.~`'~.,Dance Magic Dance,.~`'~.,
Top

Posted by Fadedparadox   USA  (91 posts)  Bio
Date Reply #10 on Wed 02 Sep 2009 10:09 PM (UTC)
Message
Lilbopeep said:
Can't really thank you enough for the hand-holding, I'm such a newb.

It's my pleasure. We all were one at once.

Lilbopeep said:
I think this is the next logical step though, I've (just) start reading up on sqlite and thought that it might be more handy to have an actual database saved to disk with all these values, so I could manually add and remove them the shortname with the spellname?

Hmm, honestly I wouldn't create a database for something so small. Lua tables should more than suffice. You can put them in your script file and edit easier in there than you could in a database. If you don't know how to use the script file, just ask.
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #11 on Thu 03 Sep 2009 09:28 PM (UTC)
Message
Quote:

scrolls = {}
scrolls["ms"] = "meteor swarm"
scrolls["cl"] = "chain lightning"



Or, more simply:


scrolls = {
  ms = "meteor swarm",
  cl = "chain lightning",

  -- put more here

  }  -- end of table


As for the database, I agree with Fadedparadox. It is probably simpler to just serialize a Lua table.

Template:post=4960 Please see the forum thread: http://gammon.com.au/forum/?id=4960.

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


33,256 views.

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.