Object Creation in a Program

Hello All,

I have a more generic code theory question. I have a program I am writing that is a companion program for a board game. As the board game develops, more pieces are added to the board. The board initially has no pieces on it and by the end of the first few turns will have 8 pieces. That number will go up as the game progresses. In my current design those pieces are individual objects within the companion program that have their own attributes. Those attributes will be tracked for various in-game mechanics (total player points, distribution of player resources, etc).

When you start my program, you open on an empty board and then customize it for the game you want to review. All objects that will be placed are placed after the initial program setup() runs.

How do I deal with objects that will be created at a time after the initial setup() runs? Is it possible to create new objects without initializing them in the setup() function and if so how is this done?

I have two solutions that I think work but they seem a little crude.

  1. I could create all of the objects that could potentially be in the game, make them “empty”, and then just fill in each object with the needed attributes when it is called. This would I think solve the problem but create a lot of objects at the initial running of the program that simply aren’t ever needed. I don’t have the exact answer, but I think you would only ever realistically see about half of those objects put in play before the game ended.

  2. The spots are also objects in my current design, but are always in the same location every single time the game is played and will be in the same location every time the program runs. Rather than create objects for these pieces, I can create a boolean for each of the spots those pieces would go on. If there is a game piece on that spot, the boolean is true and the attributes of the space are tracked through the spot on the board itself and not as individual objects.

Is there a more standard way to go about solving this problem?

Thank you all for the help.

If you know how many objects you will ever have you could use an array, if the size is undefined and may grow to any size use lists or arraylist.

Arrays and arraylists are containers which store objects. Arrays are static have to be initialised with a size whereas lists and arraylists are dynamic meaning you can add objects after initialisation.

https://www.youtube.com/results?search_query=Coding+train+arraylist

https://www.youtube.com/results?search_query=Coding+train+arrays

1 Like

You can and should create the objects during the game when they are placed on the board

Thank you for the reply. I’ve actually used ArrayLists before but hadn’t thought of it for this application. Much appreciated!

I figured as much. From a resource allocation standpoint it doesn’t make sense to need all of the objects to be setup at the very beginning. My question is how, or what tutorial goes into detail about this topic?

I am only familiar with standard object creation. Create the slot for the object, initialize in setup, manipulate it in draw. I am not sure how to initialize in draw.

honestly, it’s no big deal.

Here is an example where you can add boxes throughout the Sketch by clicking the mouse.

New objects are added to an ArrayList in the function mousePressed

// the boxes 
ArrayList<Cube> cubes = new ArrayList();

// -------------------------------------------------------------------------------------

void setup() {
  size( 1200, 900, P3D );

  //text 
  textMode(SHAPE); 

  Cube np=new Cube();
  np.pos=new PVector(65, 112, 33);
  cubes.add(np); 

  np=new Cube();
  np.pos=new PVector(265, 212, -233);
  cubes.add(np); 

  np=new Cube();
  np.pos=new PVector(665, 112, -233);
  cubes.add(np);
}

void draw() {
  background(0); 
  lights(); 

  // plates 
  for (Cube currentCube : cubes) {
    currentCube.display();
  }//for
}

void mousePressed() {
  Cube np=new Cube();
  np.pos=new PVector(mouseX, mouseY, 33);
  cubes.add(np);
}

//==================================================================

class Cube {

  // one cube 

  PVector pos; 
  float xs, ys; 

  void display() {
    // show rect/plate
    pushMatrix();
    fill(255, 2, 2);
    stroke(0);
    translate(pos.x, pos.y, pos.z);

    box ( 20, 21, 20);

    popMatrix();
  }
  //
}//class
//

Thank you kindly.

The ArrayList part of this is what I’ve been missing. I have been so accustomed to pointing information at the object after it has been initialized I couldn’t think how to do it otherwise. ArrayList is a big help thank you

2 Likes