Assigning function to a variable

Hello everybody! I’m a beginner at processing (but I have some experience with p5js and PJs).
I’m trying to find a way to assign a function to a variable. Essentially, I want to be able to set a function to a separate variable, and whenever that function is called, it calls my new function (which was defined by that variable)

Here’s the code I’m working with:

class button{
 float posx,posy,widt,heig;
 int buttonColor = color(0,0,0);
 boolean customFunc; //I've tried void, int, and bool. Nothing wants to work.
 button(float x, float y, float wid, float hei, int bColor, boolean func){
  posx = x;
  posy = y;
  widt = wid;
  heig = hei;
  buttonColor = bColor;
  customFunc = func; //HERE IS ANOTHER THINGY 
 }
 
 void clickDet(){
  if(mouseX >= posx && mouseX <= posx+widt && mouseY >= posy && mouseY <= posy+heig){
    if(mouseButton1Click==1){
     println("CLICKED ME!");
   //HERE
    }
  }
 }

I can’t find a way to do this easily. I know I could use an array/list, but that wouldn’t be very efficient in the long run. I also don’t want to really assign each button a custom ID (but I probably will anyway for debugging).

I’ve made code for buttons using the JavaScript ports, but I can’t get anything to work here. Please help me out here guys!
EDIT: Here’s the code for my JavaScript buttons:

var button = function(config){
    this.x = config.x || 25;
    this.y = config.y || 25;
    this.w = config.w || 40;
    this.h = config.h || 25;
    this.textblack = config.tb || false;
    this.textlabel = config.label || "button";
    this.activate = config.activate || function(){}; //THIS IS WHAT I'M TRYING TO GET TO WORK IN PROCESSING
    this.chosenfont = config.tfont || "arial";
    this.textsiz = config.tsize || 10;
    
};
button.prototype.clickdet = function(){
    return mouseX >= this.x && mouseX <= (this.x + this.w) && mouseY >= this.y && mouseY <= (this.y + this.h) && mouse.clicked.m1; 
};
button.prototype.draw = function() {
     if(this.clickdet()){
         buttonlog.push("PRESSED BUTTON" + this.textlabel);
         this.activate(); //THIS IS WHAT I WANT TO DO IN PROCESSING 
     }
     rect(this.x,this.y,this.w,this.h);
     if(this.textblack === true){
      fill(0);
     } else{fill(255);}
     textFont(this.chosenfont,this.textsiz);
     text(this.textlabel,this.x+this.w/6,this.y+this.h/2.5);
}; 

Hi Mattyjak,

Sounds like maybe you want a supplier?

What is the function that you are passing in and where are you calling it?

1 Like

Consider switch\case:

:)

Not quite what I’m looking for, although this could be useful. I’m trying to pass a function that will be changed, as I am creating a button object/class to be used throughout my project.

In essence I’m trying to create a button object that can be called like this:

button startButton = new button(x,y,width,height,color,function);

and when clicked, it runs the ‘function’ argument. (In essence, do the javascript thing of storing a function inside of a variable)

I know that I could use that as my method for running the functions inside of each button object that I create, but it seems like a lot of extra work and I want to optimize it.

As opposed to writing the function to a switch statement, then assigning an ID to the button, I want to be able to assign a function to the button using the built-in argument passer.

Basically I want to be able to use a function as a variable. (And one that does not return anything {at least usually})

I guess it’s not the java way to do it and you need to extend a class :frowning:

although the names are similar, how you code in java and javascript is quite different especially around types.

1 Like

OK, I found a way that works, but I didn’t want to use it. I take advantage of the switch statement’s speed at finding addresses inside of an index. By using this, I can call it each frame without (hopefully) affected performance too badly.

int buttonFunctionIdManager = 0;
void button_Management(){
  switch(buttonFunctionIdManager){
   case 0: //DO NOTHIGN
   break;
   case 1: //DO SOMETHING NOT RESERVED
         print("WE FIRED IT");
         bFix();
   break;
   
   
   default: 
           println("[13]ERROR SOMEONE DID A BAD FUNCTION CALL: "+str(buttonFunctionIdManager));
           bFix();
   break;
  }
}
button startGameButton = new button(25,25,25,25,3,color(250,0,255),color(0,255,255),"EYOOO");

I would have preferred to just assign a function to each button in the constructor, but yeah I can’t seem to figure that out.
This will require me to assign each function into the switch statement and to add the bFix(); function to reset it after every call. So in others words, more work is needed for the same amount of reward.

1 Like

This is called a delegate: delegates are used to pass methods as arguments to other methods. Java doesn’t support this.


What you can do instead is use an interface; assign the button an instance of the interface, then call the methods against the interface.

For example, the ControlP5 library supports adding a callBackListener to a UI element. When the UI element is clicked/changes value, it calls the relevant method implemented in the user’s callback listener. Perhaps you’re looking to do something similar (call some method when the button is pressed for example).

myButton.addCallback(new CallbackListener() {
					@Override
					public void controlEvent(CallbackEvent input) {
                         myCode()...
					}
				});
2 Likes

I’ll try and see if I can implement that instead of my really bad switch method. Thank you for the help!