Waiting for a specific amount of time

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
ultran00b
Posts: 35
Joined: Tue Oct 30, 2007 3:30 pm

Waiting for a specific amount of time

Post by ultran00b »

Hi guys, yet another n00b question!

In my code, I have a weapon that shoots whenever you press the LMB. Problem is, it shoots about 700 times per second (which is my frame rate). I need it to shoot, wait for a specific amount of time, and then shoot again. I looked all over the forum, and couldn't find a thing. Yes, I used the almighty search function. Any ideas? They are all appreciated!

Ultran00b
Use the debugger, young Skywalker...
Saiyantwin
Posts: 7
Joined: Tue Oct 30, 2007 8:55 pm

Post by Saiyantwin »

Praetor
Posts: 42
Joined: Wed Jun 20, 2007 2:31 am

Post by Praetor »

When you shoot get the time, then when processing the input for the weapon firing, check that time against the current time and find the difference, if enough time has passed then allow the firing.

Example (this is off the top of my head so it would likely need modification to actually work):

Code: Select all

//variable declarations
u32 lastFiredTime = 0;




//When the weapon is fired (in the event reciever I assume)
if (device->getTimer()->getTime() - lastFiredTime >= X) //replace X with the time in milliseconds you want between 
//shots
{

lastFiredTime = device->getTimer()->getTime();

//your code for firing the gun

}
EDIT: oh too late.... hope this helps anyway..
"Surely we don’t need to waste resources on pathfinding; they just need to walk along the shortest route from one place to another." - EA Producer
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

^^^
Winner.

Ignore the link in the first reply, it's not really relevant to your problem.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
Weng
Posts: 97
Joined: Tue Oct 03, 2006 4:23 pm
Location: Singapore

Post by Weng »

For my case, I use the time() method.

e.g

Code: Select all

#include <time.h>

time_t start;
time_t end;

time(&start);
time(&end);

if(start - end > 5)
  //do some shooting action
Note that the smallest unit of the time function is 1 second. Hope it helps :)
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

That's not a good idea, because Irrlicht provides a timer concept which can also handle game pausing etc. And it's more accurate and better portable. Basically no weaknesses compared to simple concepts like time(). So avoid platform (or even default lib) routines if Irrlicht does provide things for you.
ultran00b
Posts: 35
Joined: Tue Oct 30, 2007 3:30 pm

Post by ultran00b »

Thanks Praetor, your code worked "right out of the box"! Thanks man! I really wanted to avoid any platform dependency, so your code was perfect!

Ultran00b
Use the debugger, young Skywalker...
Post Reply