Button Function Problem

I am having trouble making a button function. The problem that I am having is that the “Value” that I put in the function won’t change to “ChangeToThisValue” if the conditions are correct. For example if conditions are correct then Show equals 1. Aka RectanglarButton(50, 0, 50, height, Show, 1);
Not sure how to solve this any help is appreciated.

Here’s the code.

int Show;

void setup() {
  size(200, 200);
}

void draw() {
  
  if (Show == 0) {
    background(173, 129, 129);
  } else if (Show == 1) {
    background(200);
  } else if (Show == 2) {
    background(0);
  }
  
  RectanglarButton(0, 0, 50, height, Show, 0);
  RectanglarButton(50, 0, 50, height, Show, 1);
  RectanglarButton(100, 0, 50, height, Show, 2);
  
}

void RectanglarButton(float PosX, float PosY, float Width, float Height, int Value, int ChangeToThisValue) {
  
  fill(255);
  if (mouseX > PosX && mouseX < PosX + Width && mouseY > PosY && mouseY < PosY + Height) {
    if (mousePressed) {
      fill(215);
      Value = ChangeToThisValue;
    }
    fill(235);
  }
  
  rect(PosX, PosY, Width, Height);
  
}

So what I did is just change:

Value = ChangeToThisValue;

to:

Show = ChangeToThisValue; //Change from Value to Show?

I don’t really see where else you are using the variable Value and ChangeToThisValue except within your RectanglarButton class? So when you click on the button, it won’t change anything because the Show value has nothing to do with the Value variable. Also, I am not too sure on if you wanted the rectangles to change color? But by doing my suggested changed above, it ends up doing so.

Hopefully this helps, and I would be glad to help out further,

EnhancedLoop7

This works but the only problem is that if I want to change Show to something else there’s a problem. For example I want to have a another int called ColorButton and put that into the button function I can’t is there another way to fix this. That’s why I was using Value so that way I could have different int go into the Button function.

Sorry if I am a bit confused on what the actual result of your code should be… what do you want this code to do?

but the only problem is that if I want to change Show to something else there’s a problem

How so? In my first response I changed Show to ChangeToThisValue, that you set for each button. Could you please elaborate on what specifically you’d like?

I want to have a another int called ColorButton and put that into the button function I can’t

Could you also please explain what you mean by this? What I think you’re saying is that you want another int variable to hold the value of show that you pass in to your function?

That would be as simple as adding in:

int ColorButton; 

ColorButton = ChangeToThisValue;

within your class.

Again, I could be still misunderstanding your question, but am willing to continue working with you on the problem,

EnhancedLoop7

In some languages this would be easy. You would pass the address (in memory) of the variable you want to change, and then just assign a new vaule to the thing at that address.

They call it a pointer. It points at something else.

… The problem is that Processing doesn’t do pointers. It’s usually not too much of an issue, but it can be frustrating at times.

There are two approaches that you could take. The first is useful if you have a fixed number of these sort of things that you would like to be able to change. Just put them in an array, and pass the index into that array to your button function:


int[] modifyTheseValues = new int[3];

color[] colors = { color(200, 0, 0), color(0, 200, 0), color(0, 0, 200) };

void setup() {
  size(200, 200);
}

void draw() {

  background(0);

  RectanglarButton(0, 0, 50, 50, 0, 0);
  RectanglarButton(0, 50, 50, 50, 0, 1);
  RectanglarButton(0, 100, 50, 50, 0, 2);
  fill(colors[modifyTheseValues[0]]);
  ellipse(25, 175, 50, 50);

  RectanglarButton(50, 0, 50, 50, 1, 0);
  RectanglarButton(50, 50, 50, 50, 1, 1);
  RectanglarButton(50, 100, 50, 50, 1, 2);
  fill(colors[modifyTheseValues[1]]);
  ellipse(75, 175, 50, 50);

  RectanglarButton(100, 0, 50, 50, 2, 0);
  RectanglarButton(100, 50, 50, 50, 2, 1);
  RectanglarButton(100, 100, 50, 50, 2, 2);
  fill(colors[modifyTheseValues[2]]);
  ellipse(125, 175, 50, 50);
}

void RectanglarButton(float PosX, float PosY, float Width, float Height, int Value, int ChangeToThisValue) {
  fill(255);
  if (mouseX > PosX && mouseX < PosX + Width && mouseY > PosY && mouseY < PosY + Height) {
    if (mousePressed) {
      fill(215);
      modifyTheseValues[Value] = ChangeToThisValue;
    }
    fill(235);
  }
  rect(PosX, PosY, Width, Height);
}

The second approach involves classes and objects and inheritance and is a real pain to write a simple example for - so I’m not going to unless the first approach isn’t enough.

2 Likes

EnhancedLoop7

what do you want this code to do?

I am trying to make a more simpler button in less code lines that was the goal. The problem is with the way you were trying to do. Is that if you set the values in the button function then you cannot have another button that has entirely different named value change if the conditions are correct. Did this help.

TfGuy44

I think TfGuy44 hit the nail on the head almost. This would work but like I said above to EnhancedLoop7. Is that different buttons will have different Named values change for different reasons. In this case is the second way better.

AH HA! Okay, you CAN do this. The trick is that the external value that you want to change HAS to be an array.

Consider:

int[] value = new int[1];
int[] other = new int[1];


void setup() {
  size(200, 200);
  
  value[0] = 1;
  println( value[0] );
  change( value, 2 );
  println( value[0] );
  
  other[0] = 3;
  println( other[0] );
  change( other, 4 );
  println( other[0] );
  
}

void draw() {
  
}

void change( int[] changeThis, int newValue ){
  changeThis[0] = newValue;
}
1 Like

It worked, Thanks so much. What’s the difference from single integer and an array integer?

When you pass a plain old integer, you’re passing a copy of that integer’s value. So you can’t make any changes to the integer the value came from, because all you got was its current value.

When you pass it as an array, you force it to pass the address in memory of where that array starts (and not a copy of that array’s contents!). This means that you can change the original array.

O that makes sense, Thanks for the info.

Oh okay! I am glad that got figured out, and everything is working for you!

EnhancedLoop7

Yep @TfGuy44 helped my fix the tiny error. Thanks for your effort though.

1 Like