Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Help with in game timers

Author
Message
theagg
16
Years of Service
User Offline
Joined: 11th Feb 2008
Location:
Posted: 17th Feb 2008 15:58
Setting up a timer to trigger upon a certain event being satisfied and triggering further events based on how long the timer has been running.

I just cannot get anything like this to work using dbTimer().

eg

If { condition that triggers timer }

then start timer from zero and count up

then If ( timer reaches time T }, trigger new event.

Stop timer and reset to zero when initial 'If' condition that triggered it is no longer met. etc
Codger
21
Years of Service
User Offline
Joined: 23rd Nov 2002
Location:
Posted: 17th Feb 2008 18:01
dbTimer() simply return the system time in milliseconds. To test for a specific amount of time having passed you need an additional variable say MyTimer. Check if dbTimer()is greater than MyTimer. If the condition is met then set MtTimer = dbTimer()+ 500 (.5 seconds)

Codger

System
PIV 2.8 MZ 512 Mem
FX 5600 256 mem
Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 17th Feb 2008 18:51
Rather than use dbTimer I suggest you use clock defined in time.h as it works the same and it's a standard C function.

theagg
16
Years of Service
User Offline
Joined: 11th Feb 2008
Location:
Posted: 18th Feb 2008 02:25
Well thanks so far.

However, its still not yet really given me any clue as to how to have an event timer start from zero and count up when an event occurs and stop counting when the event finishes and rest to zero.

eg

Ball moves within set distance of object and triggers timer, which continues to count up whilst ball is within range. Ball moves out of range and timer stops, resetting to zero.

I have tried various methods, some hinted at here, using dbTimer(), Clock(), difftime () etc, and the timer refuses to count properly. It sits at zero, or it never resets to zero between events ( such that when the ball rolls away, then rolls within distance again, the timer is displaying the last time ( say 10 seconds ) plus the extra time in between.

Frustrating !

Will have another crack at it tomorrow
Codger
21
Years of Service
User Offline
Joined: 23rd Nov 2002
Location:
Posted: 18th Feb 2008 08:36
theagg

Quote: "Ball moves within set distance of object and triggers timer, which continues to count up whilst ball is within range. Ball moves out of range and timer stops, resetting to zero."


Why would you care how long an object is within an area then reset the timer?

Timers are usually used to restrict an event for example if a weapon is only allowed to fire 10 rounds per second then you would use a timer to ensure that 100 milliseconds have passed between rounds.

Codger

System
PIV 2.8 MZ 512 Mem
FX 5600 256 mem
theagg
16
Years of Service
User Offline
Joined: 11th Feb 2008
Location:
Posted: 18th Feb 2008 12:40
Several reasons as to why I might want it that way.

For example, a time counter that gives you 10 seconds to comply or face the consequences so long as you stand within said range.

Move outside the range and the displayed counter resets to zero.

The point being that whenever the area is entered, the timer starts displaying from zero. Using dbTimer() or clock() just returns the number of ticks since the program started, so whenever it is used, its value will always be higher than the last time, thus making it difficult apparently to have a timer start specifically from zero each time the event occurs.
Codger
21
Years of Service
User Offline
Joined: 23rd Nov 2002
Location:
Posted: 18th Feb 2008 16:55 Edited at: 18th Feb 2008 16:57
ok
Using Pacman as an example

When Pacman eats the energy dot set variable EnergizedStartTime = dbTimer().
In your main loop insert

Pacman = Prey
if( dbTimer()- 5000 < EnergizedStartTime) Pacman = Hunter

If you want to know how long Pacman has been energized

energizedTime = dbTimer() - EnergizedStartTime



Hope this helps

Codger

System
PIV 2.8 MZ 512 Mem
FX 5600 256 mem
theagg
16
Years of Service
User Offline
Joined: 11th Feb 2008
Location:
Posted: 18th Feb 2008 19:05
Well, although I see where the example you gave is going and can visualise whats happening, it stil doesn' quite deal with the problem I am having.

Try to visualise this.

A room contains 5 boxes spread around, each displaying an LED counter set to 0. Each set to trigger if anything gets within 10 m.

The 'Ball' rolls in and comes to a halt close to the fist three boxes, ( ie they are all within 10 m of the ball, the other two are 25 m away )

This immediately triggers the 'alarms' on the first 3 boxes, which all start counting up from zero ( it must be from zero ) displaying the count on their respective LED's. Which continue to count so long as the ball remains within 10m of them.

The ball rolls away, so the counters stop. But the ball then rolls back towards the first 3 boxes. So again, this triggers their alarms, and again, they begin counting up..from zero ) etc.
Now, I can get the counters to start displaying their counts when the ball rolls close and stop displaying them when the ball rolls away.

What I cannot get it to do is to reset their individual counters to zero each time. Such that when the ball rolls back, instead of starting from zero again, the counters just display the ongoing increasing seconds elapsed since the first encounter.

It's the resetting to zero for all boxes no longer in contact that is proving difficult
Codger
21
Years of Service
User Offline
Joined: 23rd Nov 2002
Location:
Posted: 18th Feb 2008 21:24
theagg
Try this to see if it does most of what you want




Codger

System
PIV 2.8 MZ 512 Mem
FX 5600 256 mem
theagg
16
Years of Service
User Offline
Joined: 11th Feb 2008
Location:
Posted: 18th Feb 2008 22:04
Thanks, I will check through that and test it once my head has cooled down from all the stressing !

In the meantime, I think I may have solved it via a different method ( using arrays to store times ). eg

I can test in comparison to your snippet.

the relevant bit.



I can't believe it ended up being that simple and the time taken to get to such a simplistic solution !

Well, hopefully it continues to work anyway.
jason p sage
17
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 19th Feb 2008 01:10
I have something for you.

My silly but effective Clock Class:
jgc_clock.h


jgc_clock.cpp



Now for a timer class
jgc_timer.h


jgc_timer.cpp


1st Create the JGC_CLOCK class before your main loop.

JGC_CLOCK *Clock = new JGC_CLOCK();

2nd - Call Clock->Update(); Each loop

Now you have a working DarkGDK clock to work your timing stuff off of.



Next - Set up a timer with the timer class. Note this Timer - If triggered just tells you YUP - I triggered - at that moment it sets its self up for another "round" of the specified delay.

Pseudo Sample:


theagg
16
Years of Service
User Offline
Joined: 11th Feb 2008
Location:
Posted: 19th Feb 2008 16:05
thanks and interesting.

Being a newbie to C++ I think i need to read up more on pointers etc before I fully understand what "this->" is referring to in the code though.

The fun of learning.
jason p sage
17
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 19th Feb 2008 16:47
Your welcome.

I recommend: http://www.cplusplus.com/doc/tutorial/

Basically, a CLASS can be thought of as a user defined type - or a group of variables (and/or functions).

By declaring as a pointer (with the asterik) -


I'm telling compiler to JUST reserve a SINGLE SPOT we will put a number in - that number is a memory address. (e.g. pointer)

When I tell the compiler to make my class with this syntax:


Three things happen.
1: Memory is allocated automatically for the class to reside in.
2: The Address of this newly allocated memory is stored in our Pointer
3: The function with no name - (the constructor)

is run. Note - when it does stuff to variables in the class - they only effect that INSTANCE of the Class. One Timer? One Instance!

When you do that -> mumbo jump - you are telling the compiler to - "Use My Class - but its really Located Somewhere in memory - this is a pointer to it"



for example means - "Call the Function 'DelayPassed' with the value of the DarkGDK dbTimer function result.... for the CLASS INSTANCE pointed to by 'MyTimer' "

It seems tricky at first admittedly - but classes are cool.

Login to post a reply

Server time is: 2024-10-08 11:36:09
Your offset time is: 2024-10-08 11:36:09