how to do specific things based on alias variables.

Posted by Lilbopeep on Wed 02 Sep 2009 07:49 AM — 12 posts, 46,816 views.

USA #0
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?

Australia Forum Administrator #1
First:
Template:faq=32
Please read the MUSHclient FAQ - point 32.
USA #2
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?

USA #3
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
Amended on Wed 02 Sep 2009 09:32 AM by Fadedparadox
USA #4
oh, yeah, that saves me a couple lines for each spell, which is good because there are quite a few.
USA #5
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.
USA #6
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!
USA #7
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).
USA #8
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?

USA #9
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.
USA #10
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.
Australia Forum Administrator #11
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.