ID:175930
 
Well I want to create a timer.
I've made a timer but it loops its self an infinate number of times causing the game to crash.
Here's what I've got
mob
var/htime = 50
New()
..()
spawn while (1)
src.countdown()
proc/countdown()
if(htime > 0)
sleep(1)
htime --
sleep(2)
countdown()
if (htime == 0)
htime = 50
usr.Hunger -= 1
countdown()
Which works but after a while it causes an error (an infinate loop). So does anyone know how to make a timer that wont crash the game?
var/htime = 50

world
New()
spawn() timer()

proc
timer()
htime -= 1
spawn(10) timer()


Siientx
mob
var/htime = 50
New()
..()
src.countdown()
proc/countdown()
while(src)
for(htime > 0)
htime --
sleep(2)
for(htime == 0)
htime = 50
src.Hunger --
sleep(2)

This sould work i think.
PS: Use src in a proc, not usr (thats what they always say)
In response to Siientx
lol, thats a bit shoryter way to do it :P
You have two looping structures operating on the same proc:

Yuugi wrote:
New()
..()
spawn while (1)
src.countdown()

This runs a new instance of src.countdown() every tick after the mob is created.

Then each instance of src.countdown() calls another each time it executes. (Two instances if htime = 1 when the proc executes.)

The first tick, one countdown executes. The second tick, another countdown starts for 2 total. The 3rd and 4th ticks each start a new countdown proc for a total of 4. Then the first countdown proc finishes it's sleep cycle in the 4th tick and starts a new one, so you have countdown running 5 times on the 4th tick. After a few seconds of this, your program will be trying to crunch hundreds of countdown procs for every mob in the game.


Change your New() proc so that it only excutes one instance of countdown()
New()
..()
spawn(1) src.countdown()

and change your countdown proc so that it loops only once every 3 ticks, regardless of the internal workings:

proc/countdown()
if(htime > 0)
htime--
else
htime = 50
usr.Hunger -= 1

spawn(3) countdown()
Ok thanks people. It works now.