Problems with width from class within class

Hello, I’m having problems with getting the correct value for screenwidth using width from a class within a class.
I modified the example code that I found on this forum to illustrate the problem.

If i call width from the setup it gives the correct value of 1280, but when i call it from the Class room it always gives 100.
Is this a known bug or am I getting something wrong?

House myHouse = new House();
int myblock; 
void setup() {
 
  size (1280, 720);
 //*** THIS GIVES CORRECT VALUE FOR WIDTH (1280)***
  myblock = width;
  
  myHouse.rooms[0].name="";
  myHouse.rooms[0].wifi = true;
  myHouse.rooms[0].isDefined = true;
 
  myHouse.rooms[1].name="Bedroom";
  myHouse.rooms[1].wifi = false;
  myHouse.rooms[1].isDefined = true;
  myHouse.rooms[1].colour=color(5, 0, 255);   
 
  myHouse.rooms[2].name="Child 1";
  myHouse.rooms[2].wifi = false;
  myHouse.rooms[2].isDefined = true;
  myHouse.rooms[2].windows=5;
  myHouse.rooms[2].colour=color(5, 210, 255);   
 
  background(9);
}//function
 
void draw() {
  background(9);
  myHouse.display();
  
}//function
 
// ==========================================
 
class House {  
 
  // default values for a house
  color colour=color(0, 255, 0);  
  int size=310;  
  int houseNumber=1;  
  String street="Correct blocksize from setup";
  String name="House of family Stone";
 
  // array of rooms in the house
  Room[] rooms = new Room [22];
 
  // constructor
  House () {
    // pre init rooms 
    for (int i =0; i< rooms.length; i++) {
      rooms[i] = new Room();
    }
  }//constructor
 
  // method
  void display() {
    // display house
    fill(colour);
    text(myblock+"\n"+street+" "+houseNumber, 44, 44);
 
    float verticalDistanceRooms = 60; 
 
    int i=0;
    for (Room currentRoom : rooms) {
 
      if (currentRoom.isDefined) {
        currentRoom.display(244, i*verticalDistanceRooms+88 );
 
        stroke(255);
        // horizontal line
        line (66, i*verticalDistanceRooms+88, 
          210, i*verticalDistanceRooms+88); 
        // vertical line
        line (66, 0*verticalDistanceRooms+88-20, 
          66, i*verticalDistanceRooms+88); 
 
        i++;
      }
    }//for
  }//method
  //
}//class 
 
// =====================================================
 
class Room {  
 
  // default values for a room 
  color colour=color(255, 0, 0);   
  int windows=2;  
  String name="";
 
  boolean wifi=false;
  boolean isDefined=false;
  
  //*** THIS GIVES WRONG VALUE FOR WIDTH (always 100)***
  int block = width;
  
  
  // no constructor
 
  // method 
  void display(float x, float y) {
    // display room
 
    // check wifi 
    String hasWifiText = "Wrong blocksize from Class room"; // default text
    if (wifi) {
      hasWifiText = "Wrong blocksize from Class room"; // text for room that has wifi
    } 
 
    // display room using color, name and wifi text etc.
    fill(colour);
    text(block
      +"\n"
      +hasWifiText
      , 
      x, y);
  }
  //
} //class
//

The problem is caused because you are creating the Room objects before the sketch width is set in setup

Change the first line to

House myHouse;

then create the house in setup

void setup() {
 
  size (1280, 720);       // this will set the width and height variable
  myHouse = new House();  // Now crerate the house once width is set correctly!
  //*** THIS GIVES CORRECT VALUE FOR WIDTH (1280)***
  myblock = width;
  
  myHouse.rooms[0].name="";
  myHouse.rooms[0].wifi = true;
  myHouse.rooms[0].isDefined = true;
  ...
1 Like

Works. Thank you very much. That was causing me headache.

1 Like