Sign in to follow this  

Problem Delaying fire on weapons

This topic is 4402 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
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
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
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
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
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

This topic is 4402 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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