Actual example:
I have game.inituserinterface
call JS.whereAmI()
.
function whereAmI(){
ASLEvent("WhereAmI", platform);
}
platform
will be "desktop", "webplayer", or "mobile".
Is there a slick way to time that process?
Because Quest only tracks time in seconds, I think you would need to do this in javascript.
Not sure what you're planning to use this for. But if you want Quest to know how much lag it has, you could use something like:
So maybe you could do something like:
<function name="MeasureLag" parameters="time">
if (TypeOf (time) = "int") {
msg ("The round-trip time for an ASLEvent is " + time + "ms")
}
else {
JS.measureLag();
}
</function>
with the javascript:
$(function () {
var starttime = (new Date()).getTime();
ASLEvent("MeasureLag", "");
window.measureLag = function() {
ASLEvent("MeasureLag", (new Date()).getTime() - starttime);
}
});
In this case, we're calling ASLEvent twice - once to measure how long the message takes to get there and back; and once to send the calculated result back to Quest.
If you only need to know the time on the javascript side, you would only need to use the function once. If there's a particular ASLEvent that you want to measure the time for, you could make the JS that calls it record the value of (new Date()).getTime()
before calling it, and then calculate the time elapsed when the you get a response back. Assuming, of course, that the ASLEvent you are timing sends any response to the browser.
Does that make sense? I think I've got it clear in my head, but it's hard to explain.
Awesome sauce!
Thanks much, this is exactly what I'm looking for.
I'm wanting to time between calling JS.whereAmI()
and WhereAmI
in Quest actually running.
👍👍
I'm wanting to time between calling JS.whereAmI() and WhereAmI in Quest actually running.
Hmm… sounds like you really want the round-trip time for a Quest->JS->Quest exchange… which you could only track with one-second precision (so in most cases it would tell you 0 or 1 seconds). Is the JS->Quest->JS version close enough?
I've seen a variant of this question before, from someone who wanted to set a timeout so a particular piece of code would run after the platform was known. If that's the case, I would strongly suggest just calling that code from the WhereAmI
function, or sticking it in game.changedquestplatform
.
The JS function running onload was making Quest throw crazy errors about lists while the game was loading.
I ended up with this:
<function name="MeasureLag" parameters="time">
if (time = "") {
JS.measureLag ()
}
else if (TypeOf (ToInt(time)) = "int") {
msg ("The round-trip time for an ASLEvent is " + time + "ms")
}
else {
JS.measureLag ()
}
</function>
and this:
var starttime;
setTimeout(function(){
starttime = new Date().getTime();
ASLEvent('MeasureLag', '');
}, 2000);
window.measureLag = function() {
ASLEvent('MeasureLag', (new Date()).getTime() - starttime);
}
In the desktop player, it ranges from 23ms to 81ms.
It seems that even 1ms would be too long to depend on an ASLEvent to effect anything in Quest inside of the same script which calls a function in JS to call that ASLEvent. (Did I say that in a way that made sense?)
If there's a particular ASLEvent that you want to measure the time for, you could make the JS that calls it record the value of (new Date()).getTime() before calling it, and then calculate the time elapsed when the you get a response back.
Worst case, I should be able to do this in the JS function that calls it, and add a ...
Well, hmm... I was going to say JS.addTextAndScroll(new Date()).getTime();)
, but that would add more time in between Quest and JS, haha.
I've seen a variant of this question before, from someone who wanted to set a timeout so a particular piece of code would run after the platform was known. If that's the case, I would strongly suggest just calling that code from the
WhereAmI
function, or sticking it ingame.changedquestplatform
.
Gotcha.
Now that I ran that first bit of code, I realize it doesn't matter how big of a gap it is -- it just matters that a gap exists.
If there is no way to stall a Quest script to wait for an attribute to be changed, I don't see any way to determine the player platform from within a script that needs to use JS to have ASLEvent call a Quest function which sets game.questplatform
. Using while
doesn't work, I guess because the attribute can't be set from outside of the while
block until while
has finished.
Whoo, it takes a little longer online:
The round-trip time for an ASLEvent is 330ms
https://play.textadventures.co.uk/Play.aspx?id=editor/22255bd5-2a3a-4e62-8045-faaa4c30bd13%2fTime+the+ASLEvent.aslx
If there is no way to stall a Quest script to wait for an attribute to be changed
I don't think there is… unless I'm confusing it with a different bit of code, I believe function calls from ASLEvent
are queued up to be run as soon as any currently-running scripts on the Quest side end.
If you need some code to run during initialisation but after the platform is known, then I suspect the simplest way to do that would be to have the init script:
JS.whereAmI()
and then:
<function name="WhereAmI" parameters="s">
game.questplatform = s
// init code that depends on knowing the platform goes here
</function>
Holy crap!
That's it, I think!
WhereAmI
could have an optional parameter for a callback script.
Well, the same could be done like you said before I guess: game.changedquestplatform
, but this still seems to be the best approach.