Newbie Scripting

Posted by Katherina on Fri 08 Feb 2008 02:03 AM — 4 posts, 17,487 views.

#0
Hi,
Sorry to bother again. I'm still pretty new to coding in mushclient at all and even newer to lua. What I am trying to do with what help files and examples I have found is write a script that will set potential afflictions.
Basically the trigger gets what the potential affliction is it will give me a symptom and I have in the trigger what it is. This world again has an extremely complex combat system so I need to optimize my code. I want to know if someone call look over what I have before I change everything to find out it's wrong or doesn't work.

In my script file there is this function:
function myafflictions (affl)

if potaffl1 == nil then
SetVariable ("potaffl1", affl)
return
else
if potaffl2 == nil then
SetVariable ("potaffl2", affl)
return
else
if potaffl3 == nil then
SetVariable ("potaffl3", affl)
return
else
if potaffl4 == nil then
SetVariable ("potaffl4", affl)
return
else
if potaffl5 == nil then
SetVariable ("potaffl5", affl)
return
else
if potaffl6 == nil then
SetVariable ("potaffl6", affl)
return
end -- if
end -- if
end -- if
end -- if
end -- if
end -- if

end -- function myafflictions

It's purpose is to put the affliction name into which ever variable is empty then exit.

Then my prompt trigger which this isn't fully coded yet has these lines:

if GetVariable ("illusion") == "0" and
GetVariable ("potaff1") ~= "coating" then
SetVariable ('GetVariable("potaff1")', 1)
end -- if

The trigger itself is this :
* flays the hard waxy coating from you.

in the box below:
local affl = "coating"

if GetVariable ("fighting") == "0" then
SetVariable ("fighting", "1")
Note "Fightmode ON"
end -- if

myafflictions (affl)

Send to Script
Australia Forum Administrator #1
My first reaction is that you can simplify the "if" by using elseif, like this:


if potaffl1 == nil then
  SetVariable ("potaffl1", affl)
  return
elseif potaffl2 == nil then
  SetVariable ("potaffl2", affl)
  return
elseif potaffl3 == nil then
  SetVariable ("potaffl3", affl)
  return
elseif potaffl4 == nil then
  SetVariable ("potaffl4", affl)
  return
elseif potaffl5 == nil then
  SetVariable ("potaffl5", affl)
  return
elseif potaffl6 == nil then
  SetVariable ("potaffl6", affl)
  return
end -- if


By using elseif you save a whole lot of "end"s at the end, which makes it a bit easier to read.

My next reaction is that you seem to be mixing Lua variables with MUSHclient variables. Is that intentional?

In other words, do you really mean?


if GetVariable ("potaffl1") == nil then
  SetVariable ("potaffl1", affl)
  return
else
...


or maybe:


if potaffl1 == nil then
  potaffl1 = affl
  return
else
...


It seems a bit strange to combine them, unless you have a good reason.

#2
After much hacking I figured it out before I saw this post but it isn't as efficient as what you put here.
Yes I'm setting the mush variable on purpose because that is what the curing alias uses to determine if something needs to be cured or not.

For example if the mushalias coating = 0 then it will do the steps necissary to cure it and set it to 1. In this case it is a defence. Most should default to 0 if not afflicted and 1 to afflicted.

Now that I've figured out how easy it is to make a function I am thinking of making functions for my various cures.
One for herbs, one for salves and so on.

Is it possible to call a function from a trigger and have it return a value to that trigger?

I'm presuming it would be return(value)
Australia Forum Administrator #3
You mean, in "send to script" and have a function which does some common stuff? Yes, it could return one or more values.

eg.


function somefunction (a, b)
  return a + b, a * b
end -- somefunction 


That actually returns 2 values, so you could do this:


x, y = somefunction (2, 4)
print (x, y)  --> 6 8


Check the Lua documentation for more details about function calls.