Scripting from File question

Posted by Spellbo on Wed 24 Oct 2018 10:45 AM — 4 posts, 16,240 views.

#0
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?
Amended on Wed 24 Oct 2018 08:46 PM by Nick Gammon
USA Global Moderator #1
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?
Amended on Wed 24 Oct 2018 03:22 PM by Fiendish
Australia Forum Administrator #2
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.
#3
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.