Wait and then do some action.

edited June 2016 in Questions about Code

Hello everyone. I'm making a FPS game and I need help. I have a gun with magazine and I want to do some delay between reloading. I mean I have count of bullet I'm shooting, then I want to reload my gun so a press 'r' key on keyboard. I have this for reload but i want do some delay:

void keyPressed(KeyEvent e) { if (key == 'r') { if (p.magazine!=p.bullets) { // Here I would like , for example, a 2 seconds delay. ( after 2 second reload the gun. ) p.magazine= p.bullets; // This is for reload. } } } When i try just delay(2000);. It stops the whole game, but i don't want stop game but just wait 2 seconds and then reload without noLoop(); Thanks for you advice.

Tagged:

Answers

  • _vk_vk
    edited June 2016

    Delay... don't use it. I mean... Almost. There still some cases where one can need it. But is not this case.

    It DOES stop the execution when called.

    Use millis() to make your own timer.

    There are plenty of examples here in the forum, just search timer/timing/millis. (you can use google to search the forum as the search function here is not so good. Just do: whatIwantToSearch site:processing.org)

    the basic idea is

    • store millis() value when the timer is to be started;

    • keep subtracting updated* mills() value with stored one

    • keep comparing the result with a pre-defined delay of your choice

    • when result is equal or bigger than delay do your stuff

    • reset the timer by reassigning new updated mills() into stored var

      * millis keep updating itself while program is running

  • Use millis() to make your own timer.

    or use one of the three libraries that exist that do timer-based stuff.

  • edited June 2016

    I tried what you adviced me. I used millis. First I make 2 variables

    int time;
    int wait = 2000;
    

    "Time" is for store the current time. In method "setup" I used time = millis(); And in void keyPressed I added this:

    if (key == 'r') {
        if (p.magazine!=p.bullets) { 
          if (millis() - time >= wait) {
            p.magazine= p.bullets;
            time = millis();
          }
        }
      }
    

    The problem is I don't have this part of code in "Draw" method, so it's calling only when i press the 'r' key. So it's like. I want to reload so I press the key and I reload ( without having to wait two seconds ) then when I shoot and try reload again ( after 1 sec ) It's doesn't working because I have to wait 2 seconds. After 2 second I can reload again. It's not what I actually want.

  • You could put the if loop in your main draw loop. If key R pressed, time = millis() will reset the timer, and the draw loop will look to see if 2 seconds have elapsed before reloading - not sure but you might need a boolean in there to say that reload has been called, then reset once the reload code has run.

  • What is reload ?

    Do you need a reload after each shot or rather when your magazine is empty ?

  • edited June 2016

    For Chrisir.

    I have variable "magazine" =>Here is actual status of bullets in magazine. When game starts magazine has max count of bullets(15) and when I shoot I do magazine--; When is magazin empty ( magazine's value is 0 ) I can't shoot. So I want do reloat like in every other games, when u press 'r' you reload your gun magazine = max count of bullet ( 15 ). In code:

    void keyPressed(KeyEvent e) { if (key == 'r') { // Here I want some delay. I mean when I press r wait 2 seconds and then reload the gun. p.magazine= 15; } This is working now. But I want do some delay for example: I have 15 bullets. I shoot 5 times so I have 10 bullets and now I press 'r' so I wait 2 sec ( between reloading I will put some sound ) and then I finish reload so I have again max count of bullet ( 15 ) If you want I can send you the entire/whole code. I'm sorry for my English. I'm from Czech Republic and I'm still learning.

  • edited June 2016 Answer ✓
        boolean reload;
        long reloadStartTime;
        int wait = 2000;
    
    
    
        draw(){        // this in your draw loop
    
        if (reload) {
          if (millis() > (reloadStartTime + wait)) { // more than 2 secs or whatever wait is set to
            p.magazine= 15;
            reload = false; 
          }
    
        }
    
    
        void keyPressed(KeyEvent e) {
        if (key == 'r') {    
                reload = true; // gets main loop waiting for reload
                reloadStartTime = millis();
            }
        }
    
  • edited June 2016 Answer ✓

    fuzzySi told you how it's done.

    you mean:

    • You can't shoot while reload takes place for 2 seconds.

    Set a boolean isLockedBecauseReloading to true and if (isLockedBecauseReloading) you can't shoot.

    After 2 secs set isLockedBecauseReloading = false;

    before setup() say boolean isLockedBecauseReloading = false;

  • 3D FPS without shooting (won't run in browser and in version 3)

    http://www.openprocessing.org/sketch/25255

    shoot without reload (won't run in browser and in version 3)

    http://www.openprocessing.org/sketch/77863

  • For Chrisir

    Dude you are awesome! You helped me again. Thanks you very much. ^:)^

  • My pleasure.

Sign In or Register to comment.