Passing Variables to Script from Alias

Posted by Merseberger on Mon 28 Nov 2016 10:16 PM — 3 posts, 15,586 views.

#0
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
Australia Forum Administrator #1
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.
#2
Thank you Nick.