Sign in to follow this  
waterboy4800

Problem Delaying fire on weapons

Recommended Posts

waterboy4800    122
Making a basic game and trying to delay weapon fires based on there fire rate, cant seem to get it going Here is my code Main Game Loop

//Handle Mouse Inputs
	if (g_pMouse->IsDown ( 0 ))
		g_player.shoot(fElapsedTime);

Player Class
// Player Methods
void CPlayer::shoot(double ftime)
{
	m_weapons.front().fire(ftime);
}

Weapon Class (m_fireRate == 2000 miliseconds)
void CWeapon::fire(double ftime)
{	
	if ( (m_delayFire -= ftime) <= 0)
	{
		if (--m_ammo == 0)		
			reload(ftime);		
		m_delayFire = ftime + m_fireRate;
	}	
}

With this code the player fires as fast as the frame rate will allow them, delay seems to do nothing. ANy Ideas or better suggestions to get what im looking for? Thanks

Share this post


Link to post
Share on other sites
doynax    850
Hm.. That code seems sane to me.
Are you sure that the timer is working and have you tried tracing through the code in a debugger?
Also I would advice against using absolute floating point time values. However a precision problem should be easy to detect in this case.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Front returns first weapon on the vector, only testing with one so far. Seemed to have removed code and delay seems to be working for the time being.

fElapsedTime is delta since last frame.

Ill get back to you if it doesnt seem to work anymore.

if you dont think floating point is good what do you suggest to replace it with?

thanks

Share this post


Link to post
Share on other sites
Bliz23    188
First off, next time you post a question like this post it in game porgramming please. With that out of the way, you sure your not reseting the m_weapons class after each frame? If your not, you should actually have a function that is called each frame that updates the weapon. When it updates it, it should add on the time delta to another time that is the time since you last fired a weapon (lets call it m_LastFireTime). Then when you fire weapon, you do not need to pass time delta. But in the CWeapon::Fire() you should see if the m_LastFireTime is larger than m_Firerate. If it is, then you should fire the weapon respectively. If that doesnt work, then show me more code and it should help. If you dont wanna do it, its ok, but i think if you keep it the same way, if you fire, then 10 seconds later you try to fire later it will have a short skip until it fires, or when you keep clicking, it will keep firing depending on how fast you click, which both seems inacurate. SO hope this helps:)

Share this post


Link to post
Share on other sites
lubby    290
That code is almost driving me insane :) I would do it something like this


if (mouseDown == true)
{
player.Shoot();
}


Player::Shoot()
{
float currentTime = GetCurrentTime();
if (currentTime - m_Weapons.front().GetLastTimeFired() >= m_Weapons.front().GetFireDelay())
{
m_Weapons.front().fire(currentTime );
}

Weapon::Fire(float atWhatTime)
{
m_LastTimeFired = atWhatTime;
// fire me now....
}



Share this post


Link to post
Share on other sites
ToohrVyk    1595
I would probably have done:


if( mouseDown && gun.available ) { gun.shoot( ); }

Gun::shoot( ) {
assert( available );
new bullet( );
available = false;
Scheduler.schedule( new makeGunAvailable( this ), NOW + reloadTime );
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this