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 ➜ Passing Variables to Script from Alias

Passing Variables to Script from Alias

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


Posted by Merseberger   (4 posts)  Bio
Date Mon 28 Nov 2016 10:16 PM (UTC)
Message
I am learning Lua and wanted to try and use the GMCP data to play around. My first function was going to grab the stats of my character and send them to a designated channel. I wrote an alias called ReportStats that takes one variable. It uses a regular expression and looks like this:


^ReportStats (.*?)$


The alias calls the function ReportStats from my script file. The problem I'm running into is twofold. First, through some testing when I try and pass the variable from the alias to the function it is showing up as null when I print it. In the alias I just have ReportStats(%1).
Is that wrong? The function is below.


function ReportStats (chan)
	res, gmcptext = CallPlugin("3e7dedbe37e44942dd46d264","gmcpval","char.stats")
	luastmt = "charstats = " .. gmcptext
	assert (loadstring (luastmt or "")) ()
	
	if chan == nil then
		chan = 'spouse'
	end
	
	myStr = tonumber(charstats.str)
	myDex = tonumber(charstats.dex)
	myCon = tonumber(charstats.con)
	myWis = tonumber(charstats.wis)
	myInt = tonumber(charstats.int)
	myLuck = tonumber(charstats.luck)
	myDr = tonumber(charstats.dr)
	myHr = tonumber(charstats.hr)
	myChan = tostring(chan)
	
	send (myChan .. " Str: " .. myStr .. " Dex: " .. myDex .. " Con: " .. myCon .. " Wis: " .. myWis .. " Int: " .. myInt .. " Luck: " .. myLuck .. " DR: " .. myDr .. " HR: " .. myHr)
end


The second issue is that when I forced the variable chan to be 'spouse', I am getting that send and execute are nil global variables. The error is below:


Run-time error
World: Aardwolf
Immediate execution
[string "Script file"]:30: attempt to call global 'send' (a nil value)
stack traceback:
        [string "Script file"]:30: in function 'ReportStats'
        [string "Alias: "]:3: in main chunk
Error context in script:
  26 :  myDr = tonumber(charstats.dr)
  27 :  myHr = tonumber(charstats.hr)
  28 :  myChan = tostring(chan)
  29 :  
  30*:  send (myChan .. " Str: " .. myStr .. " Dex: " .. myDex .. " Con: " .. myCon .. " Wis: " .. myWis .. " Int: " .. myInt .. " Luck: " .. myLuck .. " DR: " .. myDr .. " HR: " .. myHr)
  31 : end


So to recap, the first problem is figuring out how to get the alias to pass the variable successfully to the script. The second problem is how to send the information to the channel. FYI, if I substitute print for send in the above function, it does work.

M
Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #1 on Tue 29 Nov 2016 06:29 AM (UTC)
Message
Quote:

In the alias I just have ReportStats(%1).


Assuming the wildcard is a string you need to quote it, because it is literally inserted. Ie.


 ReportStats("%1")


Otherwise if you don't, and if you are sending to (say) "group" then it would be:


ReportStats (group)


Here group is treated as a variable, not a literal. That's why you quote it.




Quote:

I am getting that send and execute are nil global variables.


That's right. Lua is case-sensitive. You should use Send and Execute. Check the help file or online documentation for the capitalization. If you use the "function complete" feature it can complete a function name for you.

- Nick Gammon

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

Posted by Merseberger   (4 posts)  Bio
Date Reply #2 on Wed 30 Nov 2016 01:46 AM (UTC)
Message
Thank you Nick.
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.


12,452 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.