Timing an Event

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
Bantymom
Posts: 73
Joined: Fri Aug 27, 2010 4:32 am

Timing an Event

Post by Bantymom » Fri Nov 05, 2010 3:57 am

This is the first little script that I have written all by myself.

I know there are lots of ways to make a timer, but all of those seems to be for situations where you want to actually see the timer happening. I just want to figure out how long it takes a student to play the game. That I couldn't find (searching for "time" was too broad, searching for "timer" and "counter"took me to the kinds of timers I describe above). Looking in the Lessons place didn't help me either. So, I looked up everything I could think of in the dictionary that might help me.

It seems silly, and it took me many hours to figure it out, but I am very proud of myself.

I present it here, however, not for a pat on the back, but to ask you to please just take a look-see at it.

The conversion of the raw seconds into a time format is very chunky. It works, but I am sure it blocky and inelegant, the brute force method of an inexperienced beginner. "format," "time," "hour," "minute" searches of the dictionary, however, could provide me with nothing better. I tried to convert the seconds to time, but it kept returning a time that was 4 hours larger than it should have been (14 minutes and 24 seconds would be converted to 4:14:24 PM), and I don't need the PM.

I didn't want to do a counting timer partly because, while it would be a good programming exercise for me, it was hurting my head just thinking about it, and I was worried that other things would have to stop while it was doing its thing, but mostly because the computers it will run on are all of different ages, some of which are very slow, and I have already noticed that my laptop waits for shorter seconds than does the desktop computers in my room that the children use, and the computers in the computer lab and the Special Ed room are different as well. I worried that counting on different computers would give different results.

I thought that just getting the twice, finding the difference, and converting it to a min:sec format would be better for me (I tried just subtracting one time from the other, but, as you can imagine, that didn't work, hehe)

There are several bits of the code that are there just for illustrative purposes and so that I could see what was happening. In my game, the buttons and fields will be unnecessary, as the result will be part of a larger field.

So, is there a better way I could have done this? For example, would using convert and then deleting the first and last parts (the 4: and the PM) be better? Oh! Should I turn this into a function? *trembles in fear*

I learned a lot from doing this one myself; building on it or going in another direction will teach me even more.

Code: Select all

--Button "Start Timer"
on mouseUp
   put the seconds into Field "Start Timer"
end mouseUp

--Button "Stop"
on mouseUp
   put the seconds into Field "End"
   put field "End" - field "Start" into tRawSeconds
   if tRawSeconds > 60 then
      put the trunc of (tRawSeconds / 60) into tMinutes --lol, so proud of myself for putting this all in one line!
      put tRawSeconds - (tMinutes * 60) into  tSeconds
      if tSeconds < 10 then
         put tMinutes & ":0" & tSeconds into field "Time"
      else
         put tMinutes & ":" & tSeconds into field "Time"
      end if
   else
      if tSeconds < 10 then
         put ":0" & tRawSeconds into field "Time"
      else
         put ":" & tRawSeconds into field "Time"
      end if
   end if
   put tRawSeconds into field "Time2"
   convert field "Time2" to long time --all this is just to show what happens with convert
end mouseUp
thank you kindly,
Bantymom
Attachments
Timer Test.rev.zip
(1.18 KiB) Downloaded 262 times
Last edited by Bantymom on Sat Nov 06, 2010 6:44 am, edited 2 times in total.
2nd-grade Teacher, Poultry Fancier, Scottish Country Dancer
and Perpetual Beginner

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: Timing an Event

Post by Dixie » Fri Nov 05, 2010 10:31 am

Hi...

You want fewer lines... see the attached stack

be well

Dixie
Attachments
Timings.rev.zip
(1.48 KiB) Downloaded 241 times

Bantymom
Posts: 73
Joined: Fri Aug 27, 2010 4:32 am

Re: Timing an Event (seconds, trunc, mod, format [charLength])

Post by Bantymom » Sat Nov 06, 2010 6:06 am

Thanks very much, Dixie!

I never would have thought to look up "mod," or that changing the delimiter could be so powerful, two very important lessons at exactly the time that they made sense.

That took care of one of the if/then statements (I still needed the other to be able to insert the ":" and have it make sense). Determined to be able to display it in a time-like format in one field, and thinking there must be SOME way to format numbers, I again went back to the dictionary. Numberformat worked, but it changed the format for all my numbers, and I couldn't set it just for one field, for a word in a field, or for a variable. But then I looked just at format, and there it was! Those special formatting incantations *%charLength* things!

Code: Select all

on mouseUp
   put (the seconds - gStartTime) into fld "End"    --Dixie
   put (the seconds - gStartTime) mod 60 into tSeconds    --Dixie
   put the format("%02s",tSeconds) into tSeconds        --  <--Yes! The incantation!!!
   set itemdel to "."    --Dixie (and a major learning moment for me)
   put item 1 of (trunc (the seconds - gStartTime)/60) & ":" & tSeconds into field "Time"  --Dixie + ":"
end mouseUp
And in the game, it will be reduced even further as the first line won't be needed.

*does the wildly happy dance*

While I didn't end up doing it all on my own, I wrote the first version and was able to take Dixie's generous refinement and go on to the what might be the final version, with no more if/then statements at all, and down from the original 17 brute-force lines to only 5!

I don't even care that it took me 2 hours to figure it out the last bit. This old dinosaur learned a lot.

I am finally beginning to feel a little more empowered (and not so afraid of the dictionary or bits of code that don't look so much like real words). These are small steps, and I am grateful to everyone here who has so patiently been helping me make them.

brownies all around,
Bantymom
Attachments
Timer based on Dixie.rev.zip
(1.3 KiB) Downloaded 259 times
2nd-grade Teacher, Poultry Fancier, Scottish Country Dancer
and Perpetual Beginner

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: Timing an Event

Post by Dixie » Sat Nov 06, 2010 1:59 pm

Hi Bantymom...

Well done, I'm pleased for you that you are happy with your stack...

I have adjusted the script of the 'stop timing' button to show that you don't need to use the format function... as it seems to me you use it to put the seconds to the right of the field so that it looks nice..

The fields in the 'timings' stack have their textAlign property set to right... as it is set now, if the remaining seconds are less than 10 then that number will be preceeded by a zero... if the number of seconds remaining are greater than 9 then a zero will not be placed in the field... and the seconds will appear to the right of the field...

I just wondered if this was the cosmetic appearance that you were looking for...

go gently

Dixie
Attachments
Timings 2.rev.zip
(1.72 KiB) Downloaded 255 times

Post Reply