Time range in script
giovpres
15 Jul 2020, 18:16I see there are already a timer, but I'ld like to add a control based on current time (i.e. between 18.00-19.00, there are a beautiful sunset, after 19.00 the moon is born... And so on) may be useful to add a time range. It's already possible to do this or may be added in next updates? Thank you.
mrangel
15 Jul 2020, 23:33Do you mean an in-game time, or in the real world?
giovpres
16 Jul 2020, 17:33In real world, the current time of the device, so who is playing in evening, the game ambient is in evening too...
mrangel
16 Jul 2020, 18:25In that case you'd probably want to get the current time from the browser.
I'd suggest some javascript like:
$(function() {
function updateRealTime() {
ASLEvent ("UpdateRealTime", new Date().getHours())
}
updateRealTime();
setInterval (updateRealTime, 100000);
});
That sends the browser's time to the Quest backend when the game loads, and then every 100 seconds after that (so you have the current time accurate to within a couple of minutes, while causing minimal disruption to the game. Depending what you're using it for, you might want to increase or decrease the delay.
If you don't want to add a javascript file to your game, you could convert the script above to a single line and put it in your game's UI Initialisation Script (on the Advanced Scripts tab):
JS.eval("$(function(){function e(){ASLEvent('UpdateRealTime',(new Date).getHours())}e(),setInterval(e,1e5)});")
And then on the Quest side, you would have a simple function like this:
<function name="UpdateRealTime" parameters="hour">
game.realtime = hour
</function>
Then you can use game.realtime
in your scripts or in the text processor to see what time it is. It should be a single number, the number of hours since midnight. If you want minutes too, you could make the code a little bit more complex.
Does that do what you want?
giovpres
16 Jul 2020, 20:47Yes, thank you!
Sorry but I'm new in this, my last program I made was in Amos Pro on Amiga 1200 :D
Please can you explain me step by step how to apply that scripts? I attempted to put JS.eval("$(function(){function e(){ASLEvent('UpdateRealTime',(new Date).getHours())}e(),setInterval(e,1e5)});")
in game/Advenced Scripts/User interface initialisation... putting it pressing the </> button.
The function I not understand where goes, please make me an example
giovpres
19 Jul 2020, 10:45Sorry, I attempted to do your suggest without success. Now I tought it's better to get the current hour and current minute and store them in two variables at start so I can increase them by a timer, please how I must to do? I already made the timer with controls and works fine but I'ld like to start with real values.
giovpres
19 Jul 2020, 14:36I managed to setup the "player.Ora" attribute (it contains the hour only, no minutes) with current hour, but in timers script, when the attribute being increased, it starts from 0 and not from the value just set by function, I hope had explained good... please help me.
mrangel
19 Jul 2020, 17:19Sorry, I attempted to do your suggest without success. Now I tought it's better to get the current hour and current minute and store them in two variables at start so I can increase them by a timer, please how I must to do? I already made the timer with controls and works fine but I'ld like to start with real values.
OK. The javascript will be a little more complex. So the script in User Interface Initialisation will be:
JS.eval("$(function(){function e(){var d=Date.now();ASLEvent('UpdateRealTime',d.getHours()+';'+d.getMinutes())}e(),setInterval(e,6e4)});")
Are you having trouble creating a function in Quest?
Go to the 'functions' section and press 'new'. Enter the name UpdateRealTime
. If you want to change this to something language specific, you need to change it in the script above as well.
For the function parameters, you need to add one and call it time
. Then for the function's script, add the code:
time = Split (time)
game.hour = ToInt (StringListItem (time, 0))
game.minute = ToInt (StringListItem (time, 1))
This will give you two attributes, game.hour
and game.minute
, which contain the current time (to within one minute). If the computer goes to sleep or is in standby, or if the player resets their clock while the game is running, the game will have the old time for up to a minute before it is corrected.
giovpres
19 Jul 2020, 20:11Hi Mrangel, thank you for you patience, I made how you wrote, I placed the script above in User Interface Initialisation, in functions I created new and called UpdateRealTime
, added the time
parameter and in Return type I choosed Integer
, I pasted your script but, i.e., if in room description I write {game.hour} or {game.minute}, the variables are empty.
I need to have hour and minute a once only because I created a time in status; in player/attributes if I set a value, it starts from it and works fine, my problem is I need to set that attributes from real values because the timer increase minutes every 50 seconds, so the game should start with real time but with faster lapse.
mrangel
19 Jul 2020, 20:27I need to have hour and minute a once only
In that case, you have a simpler script:
JS.eval("$(function(){var d=Date.now();ASLEvent('UpdateRealTime',d.getHours()+';'+d.getMinutes())});")
In this case, you will probably want to put it in your game start script. (Putting it in UI Initialisation script would make it skip to the current time when the player loads a saved game)
giovpres
19 Jul 2020, 20:44Thank you, I made it but I continue have the game.hour and game.minutes variables empty, I test them adding {game.hour} in a room description... My bigger problem is: how I can tranfer their values to player.Ora and player.Minuto attributes to start with them?
mrangel
20 Jul 2020, 01:05Sorry, I made a careless mistake.
In the code above, change Date.now()
to new Date()
.
My bigger problem is: how I can tranfer their values to player.Ora and player.Minuto attributes to start with them?
You can just change the attribute names in the function.
giovpres
20 Jul 2020, 16:42Hi, mrangel, finally you solved my issue, thanks a lot!
I attempted to save position to see if saves the current time game but I encountered an error: Unable to save the file due to the following error: Function not found: 'case'
giovpres
20 Jul 2020, 17:02Ok, this time I solved myself, I seen another topic where you rewrote the Possessive function in Italiano.aslx file but there was still a problem with two duplicate cases, I replaced that in line 657 case ("lei la") with case ("lei lo"), it works fine now.
A last issue, if I resume the position, the time restarts from current real time, where can I move the script so that it is skipped when I resume the game?
mrangel
20 Jul 2020, 17:21A last issue, if I resume the position, the time restarts from current real time, where can I move the script so that it is skipped when I resume the game?
I already mentioned this. You need to move the JS line from the UI Initialisation script to the Start script.
giovpres
20 Jul 2020, 17:59Ok, done! Thank you!