-
Notifications
You must be signed in to change notification settings - Fork 2
Example: Writing a New Component
Writing new components is a simple way to add new behaviors to the system that can be exposed to the GUI. No UI changes are necessary to incorporate new components. Make sure the Component is in the root folder of the "Component" subdirectory in the source code. In order to add your new component via GUI, make sure to update the UserComponets.csv file located in the "Assets" directory of the project.
To start, first make sure to inherit from the Component base class. A template has been provided as part of this repository. To use it, select "Component (Cell Mechanics Simulator)" when writing a new script. You can also inherit from another component subclass, such as Mesh or Force. A new component using this template should look something like this:
package Component;
public class MyNewComponent extends Component{
// Start is called once per frame
@Override
public void start(){
}
// Update is called once per physics step
@Override
public void update(){
}
}
The awake method is called when the instance of the behavior is created. If we want to set a default parameter, we can do so here. This method will not be called again, even as the simulation changes states. There is another important use of the awake function, for subscribing to events, which will be detailed later.
package Component;
public class MyNewComponent extends Component{
int foo;
@Override
public void awake(){
foo = 5;
}
}
By declaring a variable as "public", you can edit it in the GUI. If you want changes in this variable to cause immediate recalculations/ changes to other variables, you need to use onValidate, which is called every time a GUI field is changed.
package Component;
public class MyNewComponent extends Component{
int foo;
public int bar; //bar is public and can be modified in the GUI
@Override
public void awake(){
foo = 5;
}
@Override
public void onValidate(){
foo = bar + 1;
}
}
If you don't need the calculation to happen immediately every time the GUI is called, setup calculations are best run during the start function. This function is run once prior to the update loop beginning.
package Component;
public class MyNewComponent extends Component{
int foo;
public int bar; //bar is public and can be modified in the GUI
@Override
public void awake(){
foo = 5;
}
@Override
public void start(){
foo = bar + 1;
}
}