disableTimer("name")

Post Reply
Caled
Posts: 403
Joined: Thu Apr 09, 2009 4:45 am

disableTimer("name")

Post by Caled »

I don't understand how to make this work.

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE MudletPackage>
<MudletPackage version="1.0">
    <TimerPackage>
        <Timer isActive="no" isFolder="no" isTempTimer="no">
            <name>Charge</name>
            <script>echo("\ncharge ended\n")
disableTimer("Charge")</script>
            <command></command>
            <time>00:00:02.500</time>
        </Timer>
    </TimerPackage>
</MudletPackage>
If I enable this timer manually, by clicking on "Activate" in the script editor, it should display "Charge" once, and then disable itself. It does not, it keeps going forever.

Ubuntu, maybe the latest git (I still don't know how to check if it is or not, but the version is beta 13 july 8.

Caled
Posts: 403
Joined: Thu Apr 09, 2009 4:45 am

Re: disableTimer("name")

Post by Caled »

Code: Select all

charging=0

function startCharge()
	charging=1
	func = "endCharge("timeout")"
	tempTimer(2.5, func)
end

function endCharge(s)
	if s == "complete" then
		echo("\nCharge over\n")
	elseif s == "timeout" then
		echo("\nCharge timeout\n")
	end
	charging=0
end
Trying to write a temp timer solution, but like that the timer won't run the endCharge function.

Code: Select all

function startCharge()
	charging=1
	func = "endCharge(\"" .. "timeout" .. "\")"
	tempTimer(2.5, func)
end
Like that it will, but I have no idea why the \"" and "\" parts are important. I copied that from another person's scripting, and would really like to know what it means.

Ryan
Posts: 10
Joined: Mon Jun 22, 2009 1:15 am

Re: disableTimer("name")

Post by Ryan »

Re: first post--yeah, it looks like a bug to me. Disabling non-temp timers works just fine outside the timer's script, but doesn't seem to work inside the script, even if it's inside a function called from that script. The easiest workaround is probably just to have your Charge timer create a tempTimer that fires in 0 seconds that calls a doCharge() function.

Re: second post--you can't use double quotes inside double quotes without escaping them. So when you write code like: func = "endCharge("timeout")", what's really happening is it's parsing "endCharge(" as a string, timeout as a variable whose value is nil, and then ")" as a string. Then it doesn't know how to combine those things, so it raises an error and quits executing. Do something like: func = "endCharge('timeout')" or func = [[endCharge("timeout")]] instead, since you can safely use " characters inside string literals that are delimited by ' or [[...]]. (Also, you can safely use newlines in [[...]], so the string appears over multiple lines in your script, and will store those newlines in the string, without ever having to put \n in there.)

And that's the reason the hideous code: \"" .. "timeout" .. "\")" works. Just like how \n is a newline, \" represents a " character that doesn't get parsed as the beginning or end of a string. So "endCharge(\"" .. "timeout" .. "\")" gets parsed as the string [[endCharge("]], the string [[timeout]], and the string [[")]], which all then get concatenated together as [[endCharge("timeout")]], which is exactly what you want. But it's just easier just to use the [[...]] syntax to begin with and not bother with \".

Edit: actually, I don't know why the other person did it that way to begin with. Even using \", they could have just done "endCharge(\"timeout\")", which is still ugly, but only three-beer ugly, not tripping-over-the-sidewalk ugly.

Caled
Posts: 403
Joined: Thu Apr 09, 2009 4:45 am

Re: disableTimer("name")

Post by Caled »

Thanks for that reply :) cleared a lot up for me.

Post Reply