Stop Input Until reload is done!

:face_with_spiral_eyes:How do i prevent press reload key disable if is reloding and enable after the reload amout is done??

 if (Input.GetButtonDown("Reload"))
    {
        if(totalBullets <= 0)
  {
   if (clips >0)
   {
    audio.PlayOneShot(ReloadSound);    
    animation.CrossFade("reload");
    yield WaitForSeconds(ReloadTime);
    totalBullets = 10;    
    clips -=1;
   }
  
  }
 
     
    } 
    else if (Input.GetButtonDown("Reload"))
    {  
   
   
   
    }

Add a boolean that is true while reloading, and false otherwise.

Only reload when the player presses the reload button and the boolean is false.

var reloading : boolean = false;

function Update(){
if (Input.GetButtonDown("Reload")  !reloading){
	    Reload();
	}
}

function Reload(){
	if (reloading)
	return;
	
    if(totalBullets >= 0  clips > 0){
        reloading = true;
        audio.PlayOneShot(ReloadSound);    
        animation.CrossFade("reload");
        yield WaitForSeconds(ReloadTime);
        totalBullets = bulletsPerClip;
        clips--;	
        reloading = false;	
    }
}

I hate to nitpick your code, but you’re checking the status of reloading 1 too many times.

Here:

if (Input.GetButtonDown("Reload")  !reloading){

And here:

function Reload(){
	if (reloading)
	return;

You can get rid of one of them. (I would prefer to keep the second, and delete the first. But maybe thats just me)

Yeah right - little mistake (nothing will change, if he check 1 or 10 times)
 :slight_smile:

To be honest, I prefer OneManArmy3D’s approach. I certainly wouldn’t take the check out of the Reload function. Checking against a boolean is absolutely trivial for gameplay code like this - you are far, far better off optimizing for human error in these situations.

Always do the check in the function itself performing the action. If Reload() should not work while reloading (and really, it shouldn’t) - then Reload() should check to ensure the conditions are right. You won’t need the " !reloading" in the update loop in this case, but it also won’t hurt anything and will have absolutely no measurable impact on your codes performance.

Down the road you are way better off checking the same boolean twice than you are forgetting that a certain function should only be called in a certain situation. In a situation like this where the code is all next to one another and you aren’t likely to change it, it may seem like a small thing - but on a larger project it can make a huge difference. Easily one of the largest mistakes game programmers make is over-optimizing code which is by no means performance sensitive, and making that code more difficult to work with in the process. The larger the project the more important this becomes.

It’s important for proper encapsulation as well - “Reload()” should assume that whatever is calling it is an idiot and should ensure. While it’s a private member function it isn’t a huge deal, but later if you move towards a setup where you have an Input management class that calls Reload() externally, it becomes very important (something you very likely will do as your project grows).

Someone posted a great lecture by Jonathan Blow (Braid) in the gossip forum recently that talks about exactly this sort of thing:
http://www.myplick.com/view/7CRyJCWLM71/CSUA-talk

Thanks for the post full of information Kyle. It is something that many people first getting into coding need to realize (that extreme optimization shouldn’t be on the forefront of their minds) I know it hardly matters, thats why I said I was nitpicking the code.

I brought it up because there is no reason not to.

Later on in the course of development there is always the chance that the boolean will change to a very intensive function in which case it could hurt in the long run. Not likely I know, but there is always a chance :slight_smile:

This way works fine. tks a lot .

If press reload key reloading gun if still reloading and press reload key again do nothing. thas is the resolt im looking for:)

 if (Input.GetButtonDown("Reload")  !reloading)
 {
     Reload();
 }
 
}
function Reload()
{
 if (reloading)
 return;
 
 if(totalBullets <= 0)
 {
   if (clips >0)
   {
       reloading = true;
    audio.PlayOneShot(ReloadSound);    
    animation.CrossFade("reload");
    yield WaitForSeconds(ReloadTime);
    totalBullets = 10;    
    clips -=1;
    reloading = false;
   }
 
 }
 
}

Now the problem is the animations.Is pain in the ass:(
Little hard for me to manipulate animations in script.
And put working sounds in the current place.

full code:

var explo : Transform;
var FireZone : Transform;
var totalBullets = 10;
var clips = 5;
var AmmoTexture : Texture2D;
var clipsTexture : Texture2D;
var hitParticles : ParticleEmitter;
var force = 10.0;
var damage = 5.0;
var range = 100.0;
var ReloadTime = 5;
var ReloadSound : AudioClip;
var FireSound : AudioClip;
var EmptySound : AudioClip;
var reloading : boolean = false;
function Update()
{
    Fire(); 
}
function Fire ()
{ 
    var direction = transform.TransformDirection(Vector3.forward);
 var hit : RaycastHit;
 
    if (Input.GetButtonDown("Fire1"))
 {
     if(totalBullets > 0)
  {
   animation.Play("startFire");
   animation.Play("endFire"); 
   FireOneShot ();
   Instantiate(explo, FireZone.position, transform.rotation);
 
        }
  else 
  {
   //audio.PlayOneShot(EmptySound); 
   animation.CrossFade("idle");
 
  } 
 
 
    }
    /*
    if (Input.GetButtonDown("Reload"))
    {
        if(totalBullets <= 0)
  {
   if (clips >0)
   {
    audio.PlayOneShot(ReloadSound);    
    animation.CrossFade("reload");
    yield WaitForSeconds(ReloadTime);
    totalBullets = 10;    
    clips -=1;
   }
 
  }
 
 
    } 
 */
    if (Input.GetButtonDown("Reload")  !reloading)
 {
     Reload();
 }
 
}
function Reload()
{
 if (reloading)
 return;
 
 if(totalBullets <= 0)
 {
  if (clips >0)
  {
   reloading = true;
   audio.PlayOneShot(ReloadSound);    
   animation.CrossFade("reload");
   yield WaitForSeconds(ReloadTime);
   totalBullets = 10;    
   clips -=1;
   reloading = false;
  }
 
 }
 
}
function FireOneShot () {
 
 var direction = transform.TransformDirection(Vector3.forward);
 var hit : RaycastHit;
 
 // Did we hit anything?
 if (Physics.Raycast (transform.position, direction, hit, range)) {
  // Apply a force to the rigidbody we hit
  if (hit.rigidbody)
   hit.rigidbody.AddForceAtPosition(force * direction, hit.point);
 
  // Place the particle system for spawing out of place where we hit the surface!
  // And spawn a couple of particles
  if (hitParticles) {
   hitParticles.transform.position = hit.point;
   hitParticles.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
   hitParticles.Emit();   
  }
  // Send a damage message to the hit object   
  hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
 }
 audio.PlayOneShot(FireSound);
 totalBullets -=1;  
}
 
function OnGUI()
{
    GUI.Box(Rect(0,Screen.height - 60, 30, 30), AmmoTexture); 
    GUI.Box(Rect(30,Screen.height - 60, 30, 30),"" + totalBullets);
    GUI.Box(Rect(60,Screen.height - 60, 30, 30), clipsTexture);
    GUI.Box(Rect(90,Screen.height - 60, 30, 30),"" + clips);
}

you can download this “FPS Kit” and learn from it (it’s free)
 it looks that you need it