[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  Passing Variables to Script from Alias
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

Passing Variables to Script from Alias

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


Posted by Merseberger   (4 posts)  [Biography] 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
[Go to top] top

Posted by Nick Gammon   Australia  (21,322 posts)  [Biography] 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
[Go to top] top

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


1,849 views.

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

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]