#How do I use a variable, which is not yet created as a variable required to create this variable?

7 messages · Page 1 of 1 (latest)

dawn egret
#

I'm currently working on GUIScreens in minecraft modding and I am trying to detect when a certain button is pressed. I could do that with the click event and the mouseX and mouseY but there is apparently a onPress event, which is required in order to create a button. For a method to fulfill the requirements for onPress it has to have a button as a parameter, so it seems. However when I do just that, I can't use the button that I'm creating, since it's not a valid variable yet and could be null... How am I supposed to do this?
```java
private void addButtons() {
// Has to be executed when one Entry layout is changes
for(int i = 0; i < entries.size(); i++) {
int y = i * 12 + 20;
int x = 40;
if(i > 5) {
x = 80;
}

        if (entries.get(i).getCurrentLayout().equals("default")) {
            Button button = new Button(x, y, 143, 28, Component.literal("Entry" + i), buttonPressed(button));
            entries.get(i).addButton(button);
            this.addWidget(button);
        }


    }
}


protected void buttonPressed(Button button) {

}
livid wingBOT
#

This post has been reserved for your question.

Hey @dawn egret! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

dusk wagon
#

you don't

#

whatever you're doing right now, you're doing it wrong

#

what's the constructor of Button you want to call

dawn egret
#

I've figured it out. I apparently just misunderstood the concept of lambdas. This whole concept would work but instead of buttonPressed(button) I should use this::buttonPressed