Reloading Classees

edited January 2018 in How To...

I'm currently making a small game and I was wondering if anyone knows how to make it so when I change an interger it apply changes to a class then reloads the class as if the programme was just launched. I can make it change the detials of the class but these details don't take effect as the class is only ran once, when I start the programme.

Answers

  • How often does the reload you want occur?

    You could just use new but this is inelegant and slow

  • Can't you just add a reset() function to the class?

  • class Thing {
      int type;
      Thing(){
        type = 0;
      }
      void draw(){
        switch( type ){
          case 0:
            fill(255,255,0);
            rect(10,10,180,180);
            break;
          case 1:
            fill(0,0,255);
            ellipse(100,100,180,180);
            break;
          case 2:
            fill(255);
            triangle(10,10,190,10,10,190);
            break;
        }
      }
    }
    
    Thing my_thing;
    
    void setup(){
      size(200,200);
      my_thing = new Thing();
    }
    
    void draw(){
      background(0);
      my_thing.draw();
    }
    
    void mousePressed(){
      my_thing.type++;
      my_thing.type%=3;
    }
    

    Here is probably the easiest way. Give your class a type variable that determines the details of the class's implementation.

    There are other ways with inheritance, but that might be more complex than you need. Without seeing some sample code from you it is hard to tell.

  • This class runs when I boot up the programme. I have have a public boolean called night and when it is switched from true to false the variables on lines 43 and 65 are changed. Since this only loads at the start of the programme the variables don't actually take effect. I need the programme to go through all this code again for these changes to take effect.

    Code below:

    // A rectangular box
    class Box
    {
      // Instead of any of the usual variables,
      // We will store a reference to a Box2D Body
      Body body;
      Vec2 pos;
      float w, h;
      int player=0;
      Box(float x, float y, float w_, float h_, int type, int player)
      {
        this.player = player;
        w = w_;
        h = h_;
        // Build Body
        BodyDef bd = new BodyDef();
        if (type==0)
        {
          bd.type = BodyType.DYNAMIC; // Was Dynamic
        } else
        {
          bd.type = BodyType.STATIC; // Was Static
        }
        bd.position.set(box2d.coordPixelsToWorld(x, y));
    
        body = box2d.createBody(bd);
    
        // Define a polygon (this is what we use for a rectangle)
        PolygonShape sd = new PolygonShape();
        // Box2D considers the width and height of a
        // rectangle to be the distance from the
        // center to the edge (so half of what we
        // normally think of as width or height.)
        float box2dW = box2d.scalarPixelsToWorld(w/2);
        float box2dH = box2d.scalarPixelsToWorld(h/2);
        sd.setAsBox(box2dW, box2dH);
    
        // Define a fixture
        FixtureDef fd = new FixtureDef();
        fd.shape = sd;
        // Parameters that affect physics
        fd.density = 0;
        fd.friction = frict;
        fd.restitution = 0.1;
        // Attach Fixture to Body
        body.createFixture(fd);
        body.setUserData(this);
      }
    
      int getPlayer()
      {
        return player;
      }
    
      void display()
      {
        // We need the Body’s location and angle
        pos = box2d.getBodyPixelCoord(body);    
    
        //Platforms
        if(player==0)
        {
          pushMatrix();
          translate(pos.x, pos.y);
          fill(pred, pgrn, pblu);
          stroke(0);
          rectMode(CENTER);
          rect(0, 0, w, h);
          popMatrix();
        }
    
        //Player 1
        if(player==1)
        { 
          pushMatrix();
          translate(pos.x-25+x, pos.y-37.5+y);
          beginShape();
          noStroke();
          image(loadImage("Sprite1.png"), 1281,0, 0, 0);
          vertex(0, 0, right, 0);
          vertex(50, 0, left, 0);
          vertex(50, 75, left, 1);
          vertex(0, 75, right, 1);
          endShape();
          popMatrix();
        }
    
        //Player 2
        if(player==2)
        { 
          pushMatrix();
          translate(pos.x-25+x, pos.y-37.5+y);
          beginShape();
          noStroke();
          image(loadImage("Sprite2.png"), 1281,0, 0, 0);
          vertex(0, 0, right2, 0);
          vertex(50, 0, left2, 0);
          vertex(50, 75, left2, 1);
          vertex(0, 75, right2, 1);
          endShape();
          popMatrix();
        }
    
        //Player 1's Bullets
        if(player==3)
        {
          pushMatrix();
          translate(pos.x, pos.y);
          fill(120);
          stroke(0);
          rectMode(CENTER);
          rect(0, 0, w, h);
          popMatrix();
        }
    
        //Player 2's Bullets
        if(player==4)
        {
          pushMatrix();
          translate(pos.x, pos.y);
          fill(50);
          stroke(0);
          rectMode(CENTER);
          rect(0, 0, w, h);
          popMatrix();
        }
      }
    }
    
  • edited January 2018

    WHY!??! NOOOOOO! NOOOOOO!!!! NOOOO. NOO. NOOO.

    DON'T. DO. THAT.

     PImage img1, img2;
    
    // Somewhere inside setup(), do these...
    //    img1 = loadImage("Sprite1.png");
    //    img2 = loadImage("Sprite2.png");
    
    // A rectangular box
    class Box
    {
      // Instead of any of the usual variables,
      // We will store a reference to a Box2D Body
      Body body;
      Vec2 pos;
      float w, h;
      int player=0;
      Box(float x, float y, float w_, float h_, int type, int player)
      {
        this.player = player;
        w = w_;
        h = h_;
        // Build Body
        BodyDef bd = new BodyDef();
        if (type==0)
        {
          bd.type = BodyType.DYNAMIC; // Was Dynamic
        } else
        {
          bd.type = BodyType.STATIC; // Was Static
        }
        bd.position.set(box2d.coordPixelsToWorld(x, y));
    
        body = box2d.createBody(bd);
    
        // Define a polygon (this is what we use for a rectangle)
        PolygonShape sd = new PolygonShape();
        // Box2D considers the width and height of a
        // rectangle to be the distance from the
        // center to the edge (so half of what we
        // normally think of as width or height.)
        float box2dW = box2d.scalarPixelsToWorld(w/2);
        float box2dH = box2d.scalarPixelsToWorld(h/2);
        sd.setAsBox(box2dW, box2dH);
    
        // Define a fixture
        FixtureDef fd = new FixtureDef();
        fd.shape = sd;
        // Parameters that affect physics
        fd.density = 0;
        fd.friction = frict;
        fd.restitution = 0.1;
        // Attach Fixture to Body
        body.createFixture(fd);
        body.setUserData(this);
      }
    
      int getPlayer()
      {
        return player;
      }
    
      void display()
      {
        // We need the Body’s location and angle
        pos = box2d.getBodyPixelCoord(body);    
    
        //Platforms
        if(player==0)
        {
          pushMatrix();
          translate(pos.x, pos.y);
          fill(pred, pgrn, pblu);
          stroke(0);
          rectMode(CENTER);
          rect(0, 0, w, h);
          popMatrix();
        }
    
        //Player 1
        if(player==1)
        { 
          pushMatrix();
          translate(pos.x-25+x, pos.y-37.5+y);
          beginShape();
          noStroke();
          image(img1, 1281,0, 0, 0);
          vertex(0, 0, right, 0);
          vertex(50, 0, left, 0);
          vertex(50, 75, left, 1);
          vertex(0, 75, right, 1);
          endShape();
          popMatrix();
        }
    
        //Player 2
        if(player==2)
        { 
          pushMatrix();
          translate(pos.x-25+x, pos.y-37.5+y);
          beginShape();
          noStroke();
          image(img2, 1281,0, 0, 0);
          vertex(0, 0, right2, 0);
          vertex(50, 0, left2, 0);
          vertex(50, 75, left2, 1);
          vertex(0, 75, right2, 1);
          endShape();
          popMatrix();
        }
    
        //Player 1's Bullets
        if(player==3)
        {
          pushMatrix();
          translate(pos.x, pos.y);
          fill(120);
          stroke(0);
          rectMode(CENTER);
          rect(0, 0, w, h);
          popMatrix();
        }
    
        //Player 2's Bullets
        if(player==4)
        {
          pushMatrix();
          translate(pos.x, pos.y);
          fill(50);
          stroke(0);
          rectMode(CENTER);
          rect(0, 0, w, h);
          popMatrix();
        }
      }
    }
    

    CHANGE THIS FIRST. You do NOT want to be reloading your images EVERY SINGLE FRAME. Just load them ONCE. Do this inside setup()!

  • Oh sorry. I done what you said, thank you. What do I do next?

Sign In or Register to comment.