Is there a way to set an "internal" timer?

So I’m making a game where a character shoots.

And I need a delay between bullet shots. However, I don’t want to use delay(x) since that will affect the entire game. Can I set a delay inside my void ShootBullet() function so that I can control the fire rate without affecting the entire game?

Or is there another way to control the rate of fire? THanks

That is a common question and you are right, delay() is commonly not a good approach. The strategy is creating some sort of state in your code. See example below. You can use this concept with OOP (if you are familiar) to encapsulate this state making it easier to manage. The concept below does not include OOP but it is the generic concept.

Kf

final int DELAYTIME_MS=1500; //in msecs
final color WHITE=color(255);
final color RED=color(250, 15, 15);

final color ACTIVE = WHITE;
final color BLOCKED=RED;

int nextTime;
boolean blocking;
color bgcolor;


void setup() {
  size(400, 400);
  strokeWeight(3);
  textAlign(CENTER, CENTER);
  textSize(18);

  //Init values
  nextTime=0;
  blocking=false;
  bgcolor=ACTIVE;
}

void draw() {
  background(bgcolor); 
  translate(width/2, height/2);    

  checkIfBlockingStateExpired();
  
  fill(RED);
  text("Press any key\nBlocks for "+nf(DELAYTIME_MS/1000.0, 0, 2)+" seconds", 0, 0);
}

void keyPressed() {

  ////Enable next line if you want to blocking be activated only if NOT in blocking state
  //if(blocking) return;

  triggerBlockMode();
  bgcolor=BLOCKED;
}

void   checkIfBlockingStateExpired() {
  if (millis()>nextTime) {
    blocking=false;
    bgcolor=ACTIVE;
  }
}

void triggerBlockMode() {
  blocking=true;  
  nextTime=millis()+DELAYTIME_MS;
}