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 ➜ General ➜ Scripting from File question

Scripting from File question

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


Posted by Spellbo   (2 posts)  Bio
Date Wed 24 Oct 2018 10:45 AM (UTC)

Amended on Wed 24 Oct 2018 08:46 PM (UTC) by Nick Gammon

Message
Hi,

I'm trying to create a simple function for comparing spell cost and actual mana. I want it to be accessible in any alias/trigger that I create. I have possible a problem with syntax. I am saving this in a file named "myscript.lua", I'm new to mushclient and Lua as well.


function ManaCheck(actm, scost, command)
  if actm > scost then
    Send( "command" )
    mcheck = "1"
  else
    mcheck = "0"
  end -- for if/
return mcheck
end -- for function


sending:
/ManaCheck(GetVariable("p_mp_cur") , 320 , Yes ) -- p_mp_cur is Var saved from prompt


Run-time error
World: Brutus
Immediate execution
[string "Command line"]:1: attempt to call global 'ManaCheck' (a nil value)
stack traceback:
        [string "Command line"]:1: in main chunk


My questions:

Is this syntax right ? (def not)

Where is mcheck saved or how I can access it?
Top

Posted by Fiendish   USA  (2,555 posts)  Bio   Global Moderator
Date Reply #1 on Wed 24 Oct 2018 03:20 PM (UTC)

Amended on Wed 24 Oct 2018 03:22 PM (UTC) by Fiendish

Message
Template:codetag To make your code more readable please use [code] tags as described here.


Quote:
Is this syntax right ?

No, but that's not causing the error message you showed.

You want "Yes", not Yes, and Send(command), not Send("command"), and you need to tonumber actm because GetVariable returns a string, and then you need to check if it's nil before seeing if it's greater than scost.

mcheck isn't saved anywhere, though technically since you didn't mark it local it will still be accessible to anything in the same script space.

Quote:
attempt to call global 'ManaCheck' (a nil value)

Did you tell your world to use myscript.lua as its script file?

https://github.com/fiendish/aardwolfclientpackage
Top

Posted by Nick Gammon   Australia  (23,173 posts)  Bio   Forum Administrator
Date Reply #2 on Wed 24 Oct 2018 08:49 PM (UTC)
Message
See this tutorial about scripting: http://www.gammon.com.au/forum/?id=10212

In Lua (as in other languages) you don't quote variable names. For example:


foo = 42
print ("foo")  --> prints "foo" not "42"



This is better:


foo = 42
print (foo)  --> prints "42"


MUSHclient has provision for a file with scripting functions in it, see the Scripting configuration tab. Anything you put there will be available to triggers and aliases.

- Nick Gammon

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

Posted by Spellbo   (2 posts)  Bio
Date Reply #3 on Wed 24 Oct 2018 11:03 PM (UTC)
Message
Thanks for both answers.
Problem solved. The main problem which causes an error was that an '.LUA' file has been saved somewhere where mushclient has no rights to access it. After I moved the file to user\documents everything works fine.

After I corrected syntax errors the script works great.
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.


15,321 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.