# Change fps when the button is pressed

**URL:** <https://discourse.processing.org/t/change-fps-when-the-button-is-pressed/12678>\
**Category:** Libraries\
**Created:** [July 11, 2019, 10:27pm UTC](https://discourse.processing.org/t/change-fps-when-the-button-is-pressed/12678 "2019-07-11T22:27:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![GuilhermeVolker](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/guilhermevolker/32/5657_2.png) [@GuilhermeVolker](https://discourse.processing.org/u/GuilhermeVolker)\
**Post date:** [July 11, 2019, 10:27pm UTC](https://discourse.processing.org/t/change-fps-when-the-button-is-pressed/12678/1 "2019-07-11T22:27:33Z")

</div>

I need to be able to change the fps while the program is running, is it possible? I think the code is right but it doesn’t work 🤨

```auto
import g4p_controls.*;

GImageToggleButton btn1,btn2,btn4,btn8;

public int fps = 30;

public void setup(){
  
 size(1200,700);
 frameRate(fps);
 //G4P.setCursor(ARROW);
   
  btn1 = new GImageToggleButton(this, 1050,400,"imagens/1x.png",1,1);
  btn2 = new GImageToggleButton(this, 1100,400,"imagens/2x.png",1,1);
  btn4 = new GImageToggleButton(this, 1050,450,"imagens/4x.png",1,1);
  btn8 = new GImageToggleButton(this, 1100,450,"imagens/8x.png",1,1);
  
 }

public void draw(){
  background(0);
  println(frameRate);
 }

public void handleToggleButtonEvents(GImageToggleButton button, GEvent event) { 
  //println(button.getState());
  if(button == btn1){
    println("botao 1x pressionado");
    fps = 30;
  }
  if(button == btn2){
  println("botao 2x pressionado");
  fps = 60;
  }
  if(button == btn4){
   println("botao 4x pressionado"); 
   fps = 120;
  }
  if(button == btn8){
   println("botao 8x pressionado"); 
   fps = 240;
  }
}

```

---

<div class="post-metadata">

**Author:** ![InferNova](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/infernova/32/6009_2.png) [@InferNova](https://discourse.processing.org/u/InferNova)\
**Post date:** [July 11, 2019, 10:35pm UTC](https://discourse.processing.org/t/change-fps-when-the-button-is-pressed/12678/2 "2019-07-11T22:35:36Z")

</div>

Hey There!

Yes it possible. But to change FPS in running time you need to call frame rate in draw.

---

<div class="post-metadata">

**Author:** ![GuilhermeVolker](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/guilhermevolker/32/5657_2.png) [@GuilhermeVolker](https://discourse.processing.org/u/GuilhermeVolker)\
**Post date:** [July 11, 2019, 10:40pm UTC](https://discourse.processing.org/t/change-fps-when-the-button-is-pressed/12678/3 "2019-07-11T22:40:37Z")

</div>

haha, so simple! Thanks!!

---

<div class="post-metadata">

**Author:** ![InferNova](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/infernova/32/6009_2.png) [@InferNova](https://discourse.processing.org/u/InferNova)\
**Post date:** [July 12, 2019, 12:52am UTC](https://discourse.processing.org/t/change-fps-when-the-button-is-pressed/12678/4 "2019-07-12T00:52:56Z")

</div>

Note to add to update that you should only change the frame rates when you click each button. Rather than in raw draw. This will just save some resources.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [July 12, 2019, 11:54am UTC](https://discourse.processing.org/t/change-fps-when-the-button-is-pressed/12678/5 "2019-07-12T11:54:41Z")

</div>

I set the frameRate() with each button press.

I have an NVIDIA card and with Vertical sync (On) the frame rate is limited to 60 (my monitor refresh rate). I can always use other settings if I wish; this was my setting at the time.

With Vertical sync (On):

```auto
size(1200,700); //I can set 30, 60, 120, 240

size(1200,700, P2D); //I can set 30, 60 and it is limited to 60

size(1200,700, P3D); //I can set 30, 60 and it is limited to 60

```

Have fun!
