We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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.
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.
@ens --
Can you say something about why this is a requirement for you?
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.For more details, see:
In that context, your question doesn't make sense -- it looks like you are asking:
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-
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).That means, you atleast need to instantiate an instance of your class somewhere before it can do anything on its own.
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()
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
and the output returns:
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 (andpost()
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 ofdraw()
inside anif
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.
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: