Using the 'Custom' panel to centralize code editing
Cleon Teunissen
29 Posts
To describe my question, let me explain how I work. View elements such as buttons, checkboxes and numberfields have among their properties that you can define an action. I write that action handling code in a method in the Custom panel. In the view element properties box, in the field 'Action' I only write the call to that method. That way, when I want to edit the code for that action I don't have to dig down into the view element tree, all the code is directly accessible in the Custom panel.
I now have two simulations that use the 'evolution event' capability of EJS. Can I do the same thing with the code in the 'Events for ODE evolution' box?
The reason I ask is that in the upper section, with the code for the zero condition, the code returns a value.
Of course, for one thing the method in the Custom panel must then not be declared as a 'void' method; void methods are methods that do not return a value. Java being a type-aware language I assume that when the returned value is type double you declare 'double' instead of 'void'.
Yeah, I could just try that and see if it works, but I decided to ask anyway.
Re: Using the 'Custom' panel to centralize code editing -
Francisco Esquembre
237 Posts
You can write:
return myCustomFunction(t,x,vx);
But you must pass all variables used by your function. The reason is that the event receives intermediate values of the variables in order to compute the zero function at many points inside the step. If you pass to the function all the affected variable, it should work.
Re: Using the 'Custom' panel to centralize code editing -
Cleon Teunissen
29 Posts
Ah, I see, if the box for the zero condition code contains only a single line of code it must be the return statement. The content of that return statement can itself be a call to a method (with the variables used being passed to that method.)
What I would have tried was just calling the method (same as I do with the Action fields), and that would have failed.