Difference between revisions of "Using Timers"

From SCI Wiki
Jump to navigationJump to search
(Created page with "'''By Cloudee1''' While there is a whole timer class, using a doit method it is possible to count cycles so we don't often need to use timers. If you want a clock that counts w...")
 
m (1 revision)

Revision as of 17:55, 5 August 2013

By Cloudee1

While there is a whole timer class, using a doit method it is possible to count cycles so we don't often need to use timers.

If you want a clock that counts while you travel through multiple rooms then you will need to make global variables. In the main script, at the top where all of the other variable = somethings are listed add in however many countdown variables you need. It is very important to compile all scripts after making any main script edits!! This is a close approximation to my round timer for boxing in fabulous Las Vegas.

Code:
countdownminutes = 10 // the count
countdownseconds = 59 // the count
milsec = 5 // cycles per second
countmilsec = 5 // the count


If you only need this in a single room, and the ego can't leave the room then you could place these in a (local ... ) declaration underneath the uses stuff at the top, if the ego leaves and comes back the timer is reset to it's original value this way, using global variables then they will not forget what numbers they were on.

Now in order to see the countdown, you are going to need to display these values of numbers to the screen. the countmilsec variable is used to count the actual cycles so we won't display that. Since this is my own script and there were several different times to write the same bit of info to the screen, I made these statements into procedures. To use these, you will simply need to place this whole bit below everything else, outside of all of the instances.


Code:
(procedure public (Writemin)                                  // display Minutes
	(var textstring[50])
    Format(@textstring "%2d" countdownminutes)
    Display(@textstring dsCOORD  144 8  dsCOLOR 00 dsFONT 4 dsBACKGROUND 15 dsWIDTH 10)
)

(procedure public (Writesec)                                  // display Seconds
	(var textstring[50])
    (if(< countdownseconds 0)   = countdownseconds 0) // resets seconds if it dips negative

    Format(@textstring "%2d" countdownseconds)
    Display(@textstring dsCOORD 156 8 dsCOLOR 00 dsFONT 4 dsBACKGROUND 15 dsWIDTH 12)
)


Now whenever we need to update the times displayed it can be done by simply using Writemin() and Writesec(). Now would be a good time to add those into the rooms init instance where all of your rooms views and actors are inited, so the screen will start off with all of the necessary values displayed.


Code:
 Writemin()                     // update minutes displayed
 Writesec()


So I suppose now it is time make the part that does the counting.

Code:
  (method (doit)
  (super:doit())

    (if(== countmilsec 0)
      (if(== countdownseconds 0)
        (if(== countdownminutes 0)  // all three timers reach 0

           //  ... countdown at 0, now what


        )
        (else   // milsec and sec at 0 but not min
        = countdownminutes --countdownminutes  // so subtract one minute
        = countdownseconds 59  // changes the 0 to 60 to keep timer counting down
        Writemin()  // update minutes displayed
        Writesec()
        = countmilsec milsec // resets milsec to cycle delay
        )
      )
      (else  // milisec at 0 but not seconds
      = countdownseconds --countdownseconds  // so subtract one second
      Writesec()  // update seconds displayed
      = countmilsec milsec     // resets milsec to cycle delay
      )
    )


    = countmilsec --countmilsec  // subtract one from milisec each cycle

) // end method


And there you have a clock timer. It's not perfect in that seconds aren't always displayed as two digits. 00 - 09 are displayed as 0-9. But it counts down. Whenever you want to add more time, all you have to do is something like "= countdownminutes (+ countdownminutes 7)" or you could reset it regardless of what value it is on by doing "= countdownminutes 2"

As for number 2, you can simply add in the views init statement in the same place you told ego to get the first item. It would need an instance just like everything else. But other than that you are just moving it from the init method to the handle event method since it is being triggered by a user input instead of automatically inited.


Code:
(if(Said('get'/'rock'))
  Print("ok")
  Print("Hey, there's a strange plant under the rock")
  (send gEgo:get(INV_ROCK))
  (rock:hide())
  (plant:setPri(13)ignoreActors()init())
  // = countdownminutes (+ countdownminutes 7)
) // end said rock


the rock would disappear and the plant appear.