Problem with setup() loading order, instantiate an object with 'width' and 'height'

Hi,
How can I reliably provide width and height to the constructor while creating an object instance.
I am constructing a “device object” from a class which is automatically loaded before setup() is executed. In the device constructor I am using width and height as a reference to the device about the screen size. Since setup() is not yet processed at this point, width and height read as 100.0. So this doesn’t work.

I can’t use globals in setup. What would be a normal solution for this problem.

Here some example code:
In my root file:

void setup() {
  size(1600, 1200);
}
Device myDevice = new Device(width, height);

in my device class (different tab/file)

class Device{
  int objWidth;
  int objHeight;
  int screenWidth;
  int screenHeight;
 
 Device(int sW, int sH){
   objWidth = 300;
   objHeight = 60;
   screenWidth = sW;
   screenHeight = sH;
   position.y = screenHeight / 2;
   position.x = 0;
 }
}

If not clear enough, please let ne know

Cheers,
Stowoda

1 Like
Device myDevice;

void setup() {
  size(1600, 1200);
  myDevice = new Device(width, height);
}
2 Likes

Ah, alright, thank you! I had the type Device inside the setup().
Chapeau!

If you want to use global vars to set width and height use

settings(){
size(int1, int2);
}

Outside of setup.

Thank you for this insight. This is very helpful.