GCP Error on Calling Plug

Hi,

I’m learning to use GCP and have followed this source so far. Here is the config for my Xbox controller:

When I try to run the same line of code from the tutorial I get the error message “RuntimeException: Error on calling plug: abtn”

gpad.getButton("Abtn").plug(this, "abtn", ControlIO.ON_RELEASE);
void abtn() {
  getUserInput();
  println("A Button Pressed");
}

Here is the rest of the code I have used. Please excuse all the svg files, i’m going to tidy it up when I have this bit sorted.

import net.java.games.input.*;
import org.gamecontrolplus.*;
import org.gamecontrolplus.gui.*;

ControlIO control;
ControlDevice gpad;
float px, py;

PShape img;//full body
PShape h; //head
PShape u; //upper body
PShape l; //lower body
PShape lh; //left hand
PShape rh; //right hand
PShape lf; //left foot
PShape rf; //left foot
PShape e; //eyes
PShape m; //mouth1

void setup() {
  size(1080, 1920);
  // Initialise the ControlIO
  control = ControlIO.getInstance(this);
  // Find a device that matches the configuration file
  gpad = control.getMatchedDevice("Character_Creator");
  if (gpad == null) {
    println("No suitable device configured");  
    System.exit(-1); // End the program NOW!
  }
  img = loadShape("Female-01.svg");
  u = loadShape("FemaleTorso.svg");
  l = loadShape("FemaleLegs.svg");
  h = loadShape("Head.svg");
  lh = loadShape("LeftHand.svg");
  rh = loadShape("RightHand.svg");
  lf = loadShape("LeftFoot.svg");
  rf = loadShape("RightFoot.svg");
  e = loadShape("Eyes-01.svg");
  m = loadShape("Mouth-01.svg");

  gpad.getButton("Abtn").plug(this, "abtn", ControlIO.ON_RELEASE);
}

void getUserInput() {
  px = map(gpad.getSlider("X").getValue(), -1, 1, 0, width);
  py = map(gpad.getSlider("Y").getValue(), -1, 1, 0, height);
}

void abtn() {
  getUserInput();
  println("A Button Pressed");
}

void draw() {
  background(255);
  //img.enableStyle(); //disables the original style of the image
  //shape(img, 350, 350); //draws image at specified coordinates
  shape(h, 374, 378);
  shape(u, 374, 652);
  shape(l, 435, 1020);
  shape(lh, 390, 860);
  shape(rh, 728, 667);
  shape(lf, 390, 1332);
  shape(rf, 600, 1332);
  shape(e, 462, 526);
  shape(m, 462, 590);
  //img.disableStyle(); //disables the original style of the image
  //fill(0); //styles the image with a new colour
}

1 Like

Tagging @quark as he might be able to assist.

Kf

Impossible for me to check the configuration file format because it is a tab-separated-value file and an image does not distinguish between tabs and spaces. Did you use the configuration example to create the file?

I can spot some errors for instance
px = map(gpad.getSlider("X").getValue(), -1, 1, 0, width);

you don’t have an input called X it is called X Axis so the statement should be
px = map(gpad.getSlider("X Axis").getValue(), -1, 1, 0, width);

Same for py

Since this error occurs within the abtn method it might be causing the error message you get. I suspect that there was a lot more red error text in the console panel :wink:

I can’t test your code because I don’t have the images.

For future reference when posting problems on this forum

  1. don’t use images of text or code
  2. avoid code dependent on resources images, sounds, files unavailable to the reader
  3. create simple sketches that demonstrate the problem that can be executed by the reader
    where possible.
  4. if there are error message make sure you post the entire message

Hope this solves the problem :smile:

2 Likes

Thank you, it was the px and py error! I’ll keep the rest in mind for next time :slight_smile:

1 Like

Is there anywhere else I can find more info about coding in the hat switch? I’m having trouble figuring out how to implement it

The hat switch is a simple digital joystick with 4 switches, up, down, left and right. The sketch below demonstrates how you might get the hat inputs. You will need to create your own configuration file.
Configuration file

Hat Example
HAT	Joystick hat	2	HAT	cooliehat: pov	0	1.0	0.0

Sketch code

import net.java.games.input.*;
import org.gamecontrolplus.*;
import org.gamecontrolplus.gui.*;

ControlIO control;
Configuration config;
ControlDevice device;
ControlHat hat;

// Variables to hold hat inputs
boolean hat_left, hat_right, hat_up, hat_down;
float hat_x, hat_y;

// Variables for hat drawing only
float hx = 90, hy = 90;
float hrad = 70, hbrad = 0.7*hrad, hbsize= 0.15 *hrad;

void setup() {
  size(200, 280);
  // Initialise the ControlIO
  control = ControlIO.getInstance(this);
  // Find a device that matches the configuration file
  device = control.getMatchedDevice("hat-config");
  if (device == null) {
    println("No suitable device configured");
    System.exit(-1); // End the program NOW!
  }
  hat = device.getHat("HAT");
  textSize(16);
}

void draw() {
  getUserInput();
  background(220);
  // ===========================================
  // Hat face
  stroke(40, 40, 200);
  strokeWeight(1.5);
  fill(200, 200, 250);
  ellipse(hx, hy, 2*hrad, 2*hrad);
  // Hat direction buttons
  stroke(0);
  strokeWeight(0.9);
  fill(button_color(hat_left));
  ellipse(hx - hbrad, hy, hbsize * 2, hbsize * 2);
  fill(button_color(hat_right));
  ellipse(hx + hbrad, hy, hbsize * 2, hbsize * 2);
  fill(button_color(hat_up));
  ellipse(hx, hy - hbrad, hbsize * 2, hbsize * 2);
  fill(button_color(hat_down));
  ellipse(hx, hy + hbrad, hbsize * 2, hbsize * 2);
  // Hat direction values
  fill(0);
  text("X: " + hat_x, 20, 200, 200, 20);
  text("Y: " + hat_y, 20, 230, 200, 20);
}

int button_color(boolean pressed) {
  return pressed ? 0xffee2222 : 0xff771111;
}

// Poll for user input called from the draw() method.
public void getUserInput() {
  hat_left = hat.left();
  hat_right = hat.right();
  hat_up = hat.up();
  hat_down = hat.down();
  hat_x = hat.getX();
  hat_y = hat.getY();
}
2 Likes

Brilliant, thank you so much!

One more thing, sorry! Where can I find how to move the hat switch up and down to highlight the shapes? Thank you so much for your help so far, as you can tell I am new to this and it’s rather confusing haha

What shapes? I am not sure what you are asking for.

Sorry I meant my original PShape svg files. I would like up/down and left/right to work separately if this is possible? So the up/down is used to move up and down the body to focus on individual parts and left/right is used to cycle through hair styles for example when the head shape is in focus. I hope this makes sense and I really appreciate your time

The getUserInput method reads the current state of the hat and sets the variables

// Variables to hold hat inputs
boolean hat_left, hat_right, hat_up, hat_down;
float hat_x, hat_y;

you can test each of these separately and perform some action based on their value. There are some physical restrictions for instance if either hat_left or hat_right is true then the other must be false, it is impossible for them to both be true at the same time.

So you would some way to track the body part that has focus and the current style for that part then you can do something like this.

void draw() {
  getUserInput();
  processInput();
  background(220);
  // Draw the body
}

void processInput() {
  if (hat_up) {
    prev_part();
  }
  if (hat_down) {
    next_part();
  }
  if (hat_left) {
    prev_style();
  }
  if (hat_right) {
    next_style();
  }
}

The role of the four functions is obvious, you need to provide the code to do them :smile: