Skip to content

Example: Writing a New Component

jsutlive edited this page May 2, 2023 · 8 revisions

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.

Inheriting from the base class

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(){
    
    }
}

Using the awake method to set an initial value for a variable

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;
     }
}

Using the onValidate method to recalculate parameters after a change in the GUI

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;
     }
}

Using the start method to run calculations at the start of a simulation

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;
     }
}
Clone this wiki locally