Julio Jerez wrote:here is another problem that I realize we may have.
Unit has two event function that is call by unity: Update and FixUpdate.
we know how those works,
it seem we may use that to control objects but that will be a mistake because the operation will be out of synch with the physic update.
The newton update has it own update function that call force an torque for rigid bodies.
but the rigid body is part of the plug in,
I what we nee to do is, I think, iterate over each Gameobject and check for scripts, and for each script in the script has and NewtonUpdate method, then call that function. But I do no know if this is possible.
it is possible to query a component to see if it implement a method?
I suppose we can make a base class script like this
- Code: Select all
abstract public class NewtonScript : MonoBehaviour
{
abstract public void OnNewtonUpdate(NewtonWorld world);
...
}
then script are derived from the base class an implement interface,
that was we can iterate over those script
is there an more elegant or better way of doing this?
The NewtonScript example above is the the first thing that came to my mind.
Then on start Newtonworld should be able to find them and keep them in an array.
FindObjectsOfType
https://docs.unity3d.com/ScriptReferenc ... fType.htmlHopefully that function works the way I think.
Though we could always use reflection.
It should work equally well to loop through the scene and do this check on every component.
- Code: Select all
if(component is NewtonScript)
The "is" operator checks if an object is of that type or is derived from it.
I think we can check specifically for a method as well.
Using this code
- Code: Select all
public static bool HasMethod(this object objectToCheck, string methodName)
{
var type = objectToCheck.GetType();
return type.GetMethod(methodName) != null;
}
Unity also the the function SendMessage and BroadCastMessage but that would be too slow for this.
Oh, forgot about interfaces. This could be an option as well...
- Code: Select all
We add this to the Plugin
interface INewton
{
void OnNewtonUpdate(NewtonWorld world);
}
Then you could declare you script component like this
public class MyRobotController : MonoBehaviour, INewton
{
}
And again use "is".
- Code: Select all
if(object is INewton)
But I think we should start with making the NewtonScript class that extends the MonoBehaviour.
Other than OnNewtonUpdate later we probably would want to add more functions, such as OnCollision, OnTrigger, OnSubmitConstraint(!?).
The user then just add the functions he wants.
The interface variant above would force the user to implement every function in the interface and that would be bad design i guess.