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 ➜ Newbie Scripting

Newbie Scripting

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


Posted by Katherina   (19 posts)  Bio
Date Fri 08 Feb 2008 02:03 AM (UTC)
Message
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
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #1 on Fri 08 Feb 2008 03:19 AM (UTC)
Message
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.


- Nick Gammon

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

Posted by Katherina   (19 posts)  Bio
Date Reply #2 on Fri 08 Feb 2008 03:58 AM (UTC)
Message
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)
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #3 on Fri 08 Feb 2008 05:09 AM (UTC)
Message
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.

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


13,339 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.