Need help understanding timers and counters.

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
User avatar
Grimwold
Posts: 511
Joined: Thu Sep 13, 2012 11:45 pm
Location: A Dungeon somewhere in the UK

Need help understanding timers and counters.

Post by Grimwold »

I want to use a timer for a puzzle, so started out by trying a very basic script to get the idea of timers and counters... but I can't even get that to work, so wondered if someone could advise...

I created counter "time_01" initial 0
I created timer "button_timer" interval 1 and running at start.

I added connectors from button_timer to time_01 to increment
I also added a connector from button_timer to the following script

Code: Select all

function printTime()
  hudPrint(tostring(time_01))
end
When the timer activates it prints the value of the counter to the hud (so I can see that the timer is running and the counter is going up). However the output I get is "table: 0x05e4f8b0"

I tried without the tostring() function and it crashed saying bad argument, string expected


ultimately what I want to do is have 2 buttons... when either is pressed the script checks whether the timer is running.. if it's not, start the time and decrement a door counter. If it is running, check if it's over the alloted time: yes, reset everything. No, decrement the door counter, opening the door.
User avatar
Montis
Posts: 340
Joined: Sun Apr 15, 2012 1:25 am
Location: Grimrock II 2nd playthrough (hard/oldschool)

Re: Need help understanding timers and counters.

Post by Montis »

http://www.grimrock.net/modding/scripting-reference/
You can look up scripting references for timers and counter there.

For your script, the correct usage would be:

Code: Select all

function printTime()
  hudPrint(time_01:getValue())
end

Things to note about timers and counters:
Timers reset automatically after the interval and keep running, resetting, running, etc.
Counters will get an "activate" event when they reach 0, but don't reset automatically. To do that you can connect them to themselves with the event activate and the action reset. A counter will get a "deactivate" event when set to any other state than 0. And finally a counter should get an "any" event when switching from any number to 0 or from 0 to any number. (Not 100% certain about the latter, but will try as soon as I can.)

Some additional info on timers and counters is also here in the last block:
http://www.grimrock.net/modding/inspect ... onnectors/
When destiny calls, the chosen have no choice.

My completed dungeon (LoG1): Hypercube
User avatar
Grimwold
Posts: 511
Joined: Thu Sep 13, 2012 11:45 pm
Location: A Dungeon somewhere in the UK

Re: Need help understanding timers and counters.

Post by Grimwold »

Awesome! Thanks for the quick reply. Somehow I missed the getValue hook when reading the reference.

I had to make one change to your script to get it to work, as hudPrint was expecting a string.

Code: Select all

function printTime()
  hudPrint(tostring(time_01:getValue()))
end
User avatar
Komag
Posts: 3658
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

Re: Need help understanding timers and counters.

Post by Komag »

Grimwold wrote:ultimately what I want to do is have 2 buttons... when either is pressed the script checks whether the timer is running.. if it's not, start the time and decrement a door counter. If it is running, check if it's over the alloted time: yes, reset everything. No, decrement the door counter, opening the door.
If I understand right, you are going to have the buttons on either side of the door? I guess that doesn't really matter. So basically the second button is just another button to do the same thing, right?

And let me try to understand what your real goal is:
WHEN BUTTON PUSHED:
- if timer is NOT running, start timer, and decrement door counter
- if timer IS running, see if it's over allotted time
if it is NOT over allotted time, decrement the door counter, opening the door
if it IS over allotted time, reset everything

so I'm not quite sure how that will work. Are you saying they have to push the button once (which starts the timer and decrements the door counter), then hurry and push it again before the allotted time is up (to decrement the door counter again) to open the door?
Finished Dungeons - complete mods to play
User avatar
Grimwold
Posts: 511
Joined: Thu Sep 13, 2012 11:45 pm
Location: A Dungeon somewhere in the UK

Re: Need help understanding timers and counters.

Post by Grimwold »

I want both buttons on the same side of the door and they have to push each button within the time limit.. so I somehow need to add in a check to see which button was pushed, otherwise as you say they could push the same button twice (though that might also be a valid puzzle!)
User avatar
Komag
Posts: 3658
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

Re: Need help understanding timers and counters.

Post by Komag »

Ah, I see, so:
WHEN EITHER BUTTON IS PUSHED THE FIRST TIME:
- start timer, decrement door counter

IF SAME BUTTON PUSHED AGAIN:
- if timer has completed running (thus resetting everything), start timer, decrement door counter
- if timer is still running from the first push, do nothing

IF OTHER BUTTON IS PUSHED:
- if timer has completed running (thus resetting everything), start timer, decrement door counter
- if timer is still running from the first push, decrement door counter a second time, thus opening the door
-------------------------
basically, they have to press one of the buttons, then make it over and press the other one before time runs out, right?

I'll work on that...
Finished Dungeons - complete mods to play
User avatar
Grimwold
Posts: 511
Joined: Thu Sep 13, 2012 11:45 pm
Location: A Dungeon somewhere in the UK

Re: Need help understanding timers and counters.

Post by Grimwold »

Komag wrote: basically, they have to press one of the buttons, then make it over and press the other one before time runs out, right?

I'll work on that...
That's exactly it!

Now that I understand counter values and timers a bit better I'll keep working too and compare notes if you come up with something.
User avatar
Komag
Posts: 3658
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

Re: Need help understanding timers and counters.

Post by Komag »

Okay, I have it working, but I'm CERTAIN it could be much cleaner with smarter scripting, as my method uses two timers, three counters, and a script

- create two buttons and a door (named hurrybutton1, hurrybutton2, and hurrydoor)
- create two timers (one near each button)(named hurrytimer1 and hurrytimer2)
- create three counters (two for the buttons named hurrycounter1 and hurrycounter2, and one for the door named hurrycounterdoor)
- create a script nearby (named hurryscript)

hurryscript:

Code: Select all

function hurry1()
     hurrycounter1:setValue(0)
end
function hurry2()
     hurrycounter2:setValue(0)
end
(this allows spamming of the buttons to have no ill effect, as the counters will just keep getting set to 0)

- set your timers to give enough time to traverse the room
- set hurrycounter1 and 2 to "1" and hurrycounterdoor to "2"

CONNECTIONS:
- connect each button to its corresponding timer with "activate"
- connect each button to hurryscript, with corresponding "hurry1" and "hurry2"
- connect hurrytimer1 to all three counters with "reset", and connect it to itself with "deactivate"
- connect hurrytimer2 to all three counters with "reset", and connect it to itself with "deactivate"
- connect the two counters to hurrycounterdoor with activate - decrement
- connect hurrycounterdoor to hurrydoor with activate - open

When a button is pushed, its timer starts and its counter is temporarily set to 0 (which in turn decrements the door counter by 1). This lasts for a few seconds, and if the other button isn't pushed soon enough then the first timer will reset all the counters. But if the other button IS pushed soon enough, then its counter is also set to 0, which decrements the door counter down to 0, opening the door permanently.

I can't make it break, so it seems to work pretty foolproof for me so far
Finished Dungeons - complete mods to play
User avatar
Grimwold
Posts: 511
Joined: Thu Sep 13, 2012 11:45 pm
Location: A Dungeon somewhere in the UK

Re: Need help understanding timers and counters.

Post by Grimwold »

Awesome. Thanks for working on this. I'll give yours a try.

I did come up with a solution that uses 1 timer, 4 counters and 2 virtually identical scripts one for each button. However if you spam one button it does break,
User avatar
Grimwold
Posts: 511
Joined: Thu Sep 13, 2012 11:45 pm
Location: A Dungeon somewhere in the UK

Re: Need help understanding timers and counters.

Post by Grimwold »

Your setup works really well, especially using the timers to reset. I'll abandon mine... I had scripts with a lot of if statements making it very messy
Post Reply