Launching code during setup() without typing code into setup() [something like eventlistener('setup

ensens
edited February 2017 in How To...

Hello!

I am new here so first i want to introduce myself. I work as an art & animation teacher in the Netherlands and i am medior practitioner with code. I know a lot about PHP/SQL, javascript and i had some projects with processing using the kinect.

My question: Is it possible to launch some code while the setup() is called, but without actualy typing that code in the setup(){} - for example: when setup() is triggered, can you use something like program.addeventlistener( onSetup, do this );

In my sketchfolder (main) are 2 files: main.pde and myclass.pde. In the main.pde we find the basic functions setup() and draw()

In the myclass.pde i have a class:

class myclass{ //constructor. myclass(){ println("myclass initialized..."); } }

Normaly, to activate/initiate the class, i have to go to main.pde setup()

myclass MC; void setup(){ myclass MC = new myclass(); } .... this works fine...

But now i want to know if it is possible to initiate the class when setup() is triggered, but without adding the [new myclass()] within the setup() function... something like this:

myclass MC; myproject.addeventlistner( "on setup", myclass MC = new myclass() );

class myclass{ //constructor. myclass(){ println("myclass initialized..."); } }

is this even possible??

I am working with processing 3.2.3

Thanks in advance.

Emiel

Answers

  • Honestly, there would be no use of that.

    initiate the class when setup() is triggered

    You've got your concepts mixed up - one doesn't initiate a (non - static) class, one only initiates instances of the class, i.e. variables.

  • And even if this was possible, I wouldn't recommend it as it goes against security.

  • edited January 2017

    @ens --

    i want to know if it is possible to initiate the class when setup() is triggered, but without adding the [new myclass()] within the setup() function

    Can you say something about why this is a requirement for you?

    to activate/initiate the class

    This isn't how Object Oriented Programming works. Classes are not "active" or "inactive." Each time you use new you are creating a new object -- an instance that uses the class definition as a template.

    MyClass mc1 = new MyClass(); // first object
    MyClass mc2 = new MyClass(); // second object
    

    For more details, see:

    In that context, your question doesn't make sense -- it looks like you are asking:

    1. "How can I create an object during setup without creating an object during setup?"
    2. "How an I run code in setup without running code in setup?"

    But again, what are you really trying to do? Rather than saying "no, your particular idea won't work" we can be much more helpful if you instead explain what you are trying to accomplish.

  • To give you better idea of how program flow occurs.
    When you press the run button-

    • Processing IDE preprocesses and compiles your code (don't bother how) if you've made any changes since the last compile.
    • The preprocesser puts your entire code into a main class with the name same as your sketch. This includes all other tabs and the classes defined by you in them. There are subtle changes made, but again don't bother.
    • Your main class will be given a "main" method (public static void main). Let's not bother about all that happens inside this, but it's this method that actually gets run when you start the sketch. This will call setup (don't bother when) and after it's over, it will loop draw over and over again (again, there are some complications).
    • So any code you put outside setup and draw (and some other functions) will never even be called unless you decide to.
  • That means, you atleast need to instantiate an instance of your class somewhere before it can do anything on its own.

  • ensens
    edited January 2017

    Thanks for responding!

    The reason i asked is because i want to make a pde file (for example a short animation of a logo on the screen) that i can just drag and drop into other projects and it would work instantly - without having to add code to the setup and draw from the other projects.

    Therefor I was wondering if there was a way (similar to the addeventlistener from actionscript) to execute some code when an event (like setup or draw?) occurs.

    If i could add in my logo.pde something like this:

    logo mylogo;
    addeventlistener("onSetup", logo mylogo=new logo() );
    addeventlistener("onDraw", mylogo.run() );
    class logo{ ... }

    As you can tell i am not fully aware of all the OO principles and the processing system architecture. And from what i understand from Lord of the Galaxy, what i want is not even possible.

  • Maybe it is possible...

    You would have to override the main method to do everything that it actually should be doing, while also instantiating and drawing the "logo" at the same time.

    This is only possible since all the tabs in Processing IDE get merged together into one class.

  • if you instantiate the logo as a global variable then it'll call the constructor before calling the setup()

    Logo logo = new Logo();
    
    class Logo {
      Logo() {
        println("Hello");
      }
    }
    
    void setup() {
      println("World");
    }
    

    because, as has been pointed out, every tab gets combined into a processing class. i don't think you'll be able to do anything to the canvas before setup is called starts though. try it, i guess.

  • This is what I tried and it worked. I put the following code under a pde file and I ran it in processing.

    Kf

    import processing.core.*; 
    import processing.data.*; 
    import processing.event.*; 
    import processing.opengl.*; 
    
    import java.util.HashMap; 
    import java.util.ArrayList; 
    import java.io.File; 
    import java.io.BufferedReader; 
    import java.io.PrintWriter; 
    import java.io.InputStream; 
    import java.io.OutputStream; 
    import java.io.IOException; 
    
    public class testJAVAfile extends PApplet {
    
    
      Logo logo = new Logo();
    
      public class Logo {
        Logo() {          
          println("Hello " + millis());
          delay(4000);
        }
      }
    
      public void setup() {
        println("World " + millis());
      }
    
    
      static public void main(String[] passedArgs) {
        String[] appletArgs = new String[] { "testJAVAfile" };
        if (passedArgs != null) {
          PApplet.main(concat(appletArgs, passedArgs));
        } else {
          PApplet.main(appletArgs);
        }
      }
    }
    

    and the output returns:

    Hello 2
    World 4153

  • edited January 2017

    Another approach might be to put the logo code in a library and include it using a single import statement.

    Libraries support void pre() which executes just before draw() and can affect drawing (and post() after). However I think pre would be on frame 0, and the screen wouldn't actually update until the end of the full (main sketch) draw loop -- so prepending a static logo or an animated logo might still need to be accommodated by the main sketch -- for example, by it having the main sketch put the contents of draw() inside an if with a global flag that your logo code could turn off, then on again to start the main sketch after n loops. But I haven't tried this.

    If a time delay isn't important and if it doesn't matter that your logo display would cover up the first x seconds of you already-running main sketch then you could easily run the logo on top of the main sketch for a limited time by using post for n frames and blacking out the sketch contents each frame before displaying the logo.

  • The code posted by @kfrajer is what I was talking about.

    But please do not do this, it is downright unsafe.

  • Thanks for all your answers. I tried the solution provided by kfrajer but i couldn't get it running. For now my solution will be to create the logo instance in the setup and draw like its supposed to be done. for the next vacation i will try to understand the processing architecture... ;-)

    thanks for the help.

  • edited February 2017

    And that's where the magic comes in. Once you create the Logo variable and initialize it, you can allow Processing to handle the drawing part automatically.

    Try this code if you want:

    Logo[] l;
    
    void setup(){
      size(640, 360);
      l = new Logo[10];
      for(int i = 0; i < 10; i++){
        l[i] = new Logo();//Only initialization required
      }
    }
    
    void draw(){
      background(0);
      //That's it. No more drawing stuff.
    }
    
    
    //The class
    public class Logo{
      float posX, posY;
    
      public Logo(){
        posX = random(width);
        posY = random(height);
        registerMethod("draw", this);//tell the PApplet (sketch) to call draw() method of this class every time draw runs in the sketch
      }
    
      public void draw(){
        posX += random(-1, 1);
        posY += random(-1, 1);
        pushStyle();
        noStroke();
        fill(255, 0, 255);
        ellipse(posX, posY, 20, 20);
        popStyle();
      }
    }
    
Sign In or Register to comment.