# Need Help Understanding How to Reset a Timer

**URL:** https://forum.castle-engine.io/t/need-help-understanding-how-to-reset-a-timer/1219
**Category:** 3D
**Created:** [June 3, 2024, 11:14am UTC](https://forum.castle-engine.io/t/need-help-understanding-how-to-reset-a-timer/1219 "2024-06-03T11:14:45Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![JPF12141999](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/jpf12141999/32/1218_2.png) [@JPF12141999](https://forum.castle-engine.io/u/JPF12141999)
#### Post date: [June 3, 2024, 11:14am UTC](https://forum.castle-engine.io/t/need-help-understanding-how-to-reset-a-timer/1219/1 "2024-06-03T11:14:45Z")

</div>

Hi everyone,

This is still for my SpongeBob game project, and I used the TCastleTimer object type from the physics\_2d\_game\_sopwith project; my question is the project I am using doesn’t seem to have a way to reset the timer if you need to, and my project needs to reset a timer, in particular it needs to reset the timer to indicate that the player is in pain every time they get hit so that the wincing face lasts 2 seconds exactly every time, so that way it doesn’t accumulate and then you get weirdness about it happening after 2 seconds one time, then 4 seconds another, then 1 second another, and so on.

Does anyone know the correct code for this?

I looked in the API page for the TCastleTimer and it doesn’t seem to have a Reset method, and neither do its parent classes the User Interface and Component.

Here is the link to my Github project for reference:

> **[GitHub - JPF12141999/Toy-Story-2-Ripoff-Game: Ripoff of Toy Story 2: Buzz...](https://github.com/JPF12141999/Toy-Story-2-Ripoff-Game)**
>
> Ripoff of Toy Story 2: Buzz Lightyear to the Rescue, which takes place in the SpongeBob Universe instead. - JPF12141999/Toy-Story-2-Ripoff-Game

---

<div class="post-metadata">

### Author: ![michalis](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/michalis/32/3_2.png) [@michalis](https://forum.castle-engine.io/u/michalis)
#### Post date: [June 4, 2024, 9:13pm UTC](https://forum.castle-engine.io/t/need-help-understanding-how-to-reset-a-timer/1219/2 "2024-06-04T21:13:06Z")

</div>

From your description it not clear how do you expect the “reset” of timer to work.

And looking at your code ( [gameviewplay.pas](https://github.com/JPF12141999/Toy-Story-2-Ripoff-Game/blob/main/code/gameviewplay.pas) ), you do not use [TCastleTimer](https://castle-engine.io/apidoc/html/CastleControls.TCastleTimer.html) for what it was designed – which is to run some event _regularly_, e.g. every 2 seconds. Instead, what you do (and seem to want) is to run even _once_ after 2 seconds, that’s it. Your current approach to this is somewhat complicated and buggy, i.e. you recreate the timer each time from `TViewPlay.SandyWince` (possibly making a memory leak, and double call to `EventBacktoNeutral`, since you don’t free previous timer).

I could advise how to use `TCastleTimer` without above bugs, but it would be unnecessarily complicated. You just should not really use `TCastleTimer` for what you want 🙂

The solutions:

1. If you want to do something when 2 seconds passed, just “count the time” yourself. It is trivial.

2. If you want to do something when e.g. some animation ended, or some sound finished, there are also specialized callbacks for this. For example

---

<div class="post-metadata">

### Author: ![JPF12141999](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/jpf12141999/32/1218_2.png) [@JPF12141999](https://forum.castle-engine.io/u/JPF12141999)
#### Post date: [June 4, 2024, 9:35pm UTC](https://forum.castle-engine.io/t/need-help-understanding-how-to-reset-a-timer/1219/3 "2024-06-04T21:35:10Z")

</div>

Okay, I wanted it to do something after 2 seconds have passed, but be able to start the timer over again from blank so the seconds don’t add up from previous times it was called, when the trigger happens (namely, when you touch something that hurts you);

The “TimeDoSomething” method is exactly what I wanted for something like this; I am really sorry if it was unclear what “reset a timer” meant in this context.
