AddTrigger/Ex function with Regexp and \n

Posted by Dontarion on Tue 08 Jun 2010 11:43 PM — 6 posts, 23,756 views.

USA #0
StringRoomListCur = CurrentRoom .. ". (" .. CurrentArea .. ")$\n^" .. CurrentDesc
StringRoomListOld = OldRoom .. ". (" .. OldArea .. ")$\n^" .. OldDesc
-- These rooms look like: The gates of Mhaldor. (Mhaldor)\nRoomDescription.
StringRoomListCur = string.gsub (StringRoomListCur, "[^%a%c%d]", "\\" .. "%1")
StringRoomListOld = string.gsub (StringRoomListOld, "[^%a%c%d]", "\\" .. "%1")
AddTriggerEx("", StringRoomListCur, "[WORMHOLE: " .. OldArea .. ", " .. OldRoom .. "]", 41, -1, 0, "", "", 2, 100)
AddTriggerEx("", StringRoomListOld, "[WORMHOLE: " .. CurrentArea .. ", " .. CurrentRoom .. "]", 41, -1, 0, "", "", 2, 100)

That is the code inside an alias called whm. It creates two triggers based on information stored about the previous two rooms visited (wormholes in Achaea). My only problem I keep running into is \n, \\n, to \\\\\n doesn't work. Am I missing something here because the system keeps coming back with the string is incomplete at the \n as the alias keeps putting in a literal line return mid string.

Compile error
World: Achaea
Immediate execution
[string "Alias: "]:3: unfinished string near '")'
USA #1
<ignore this post>
Amended on Wed 09 Jun 2010 12:47 AM by Twisol
USA #2
Well, I don't want it to print out the \n. I want it to make the trigger line with \n in it. What ends up happening is when I do AddTrigger/Ex it turns the \\n into a real newline and messes up the string so the AddTrigger fails.
USA #3
Try using $ in the trigger pattern instead of \n. You won't have to go through all these pains.
Amended on Wed 09 Jun 2010 12:48 AM by Twisol
Australia Forum Administrator #4
Dontarion said:

Well, I don't want it to print out the \n. I want it to make the trigger line with \n in it. What ends up happening is when I do AddTrigger/Ex it turns the \n into a real newline and messes up the string so the AddTrigger fails.


If you do "send to script" (rather than calling a script function in a script file) MUSHclient preprocesses \n as a newline (before sending to the script engine), thus you get complaints about unterminated strings. To get the \ appear inside the script itself, you need to double it (ie. \\n).

You can avoid some of this confusion by not scripting inside the trigger (or alias) but using a script file, and just putting the function name in the Script box. Alternatively, use the Lua string constants that let you imbed newlines like this:


s = [[This string will
have a newline inside it]]

USA #5

function createWormholeTrigger ()
	if CurrentRoom == "" or OldRoom == "" then
		Note("")
		Note("You need to collect both sides of the wormhole to create the triggers.")
		Note("")
	else
		CurrentRoomHold = CurrentRoom
		OldRoomHold = OldRoom
		CurrentRoom = string.gsub (CurrentRoom, "[^A-Za-z0-9 ]", "\%1")
		OldRoom = string.gsub (OldRoom, "[^A-Za-z0-9 ]", "\%1")
		CurrentArea = string.gsub (CurrentArea, "[^A-Za-z0-9 ]", "\%1")
		OldArea = string.gsub (OldArea, "[^A-Za-z0-9 ]", "\%1")
		CurrentDesc = string.gsub (CurrentDesc, "[^A-Za-z0-9 ]", "\%1")
		OldDesc = string.gsub (OldDesc, "[^A-Za-z0-9 ]", "\%1")
		StringRoomListCur = CurrentRoom .. "\. \(" .. CurrentArea .. "\)\n" .. CurrentDesc
		StringRoomListOld = OldRoom .. "\. \(" .. OldArea .. "\)\n" .. OldDesc
		--Note(StringRoomListCur)
		--Note(StringRoomListOld)
		if IsTrigger(string.gsub (CurrentRoomHold, "[^A-Za-z0-9]", "_")) then
			AddTriggerEx(string.gsub (CurrentRoomHold, "[^A-Za-z0-9]", "_"), StringRoomListCur, "[WORMHOLE\: " .. OldArea .. "\, " .. OldRoom .. "]", 41, -1, 0, "", "", 2, 100)
			SetTriggerOption(string.gsub (CurrentRoomHold, "[^A-Za-z0-9]", "_"), "multi_line", "1")
			SetTriggerOption(string.gsub (CurrentRoomHold, "[^A-Za-z0-9]", "_"), "lines_to_match", "2")
			SetTriggerOption(string.gsub (CurrentRoomHold, "[^A-Za-z0-9]", "_"), "group", "AAAA")
			--When really IsTrigger it shows up at 0.
		else
			-- here would be a trigger setup for ones with the same name to start adding numbers
			-- on the end of it.
		end
		
		if IsTrigger(string.gsub (OldRoomHold, "[^A-Za-z0-9]", "_")) then
			AddTriggerEx(string.gsub (OldRoomHold, "[^A-Za-z0-9]", "_"), StringRoomListOld, "[WORMHOLE\: " .. CurrentArea .. "\, " .. CurrentRoom .. "]", 41, -1, 0, "", "", 2, 100)
			SetTriggerOption(string.gsub (OldRoomHold, "[^A-Za-z0-9]", "_"), "multi_line", "1")
			SetTriggerOption(string.gsub (OldRoomHold, "[^A-Za-z0-9]", "_"), "lines_to_match", "2")
			SetTriggerOption(string.gsub (OldRoomHold, "[^A-Za-z0-9]", "_"), "group", "AAAA")
		else
			-- Here would be if there is already a trigger with the same name to start adding numbers
			-- on the end of it.
		end
	end
end


I decided to go with the script file because it's just easier. I edit now because I saw the option out of the corner of my eye flipping between windows.

Can't seem to make it work on areas that have spaces in their names. Working on that problem now. -- Figured out the issue - you can't have trigger names with - in it. The new code turns anything non-alphanumeric into _'s.