HOME   |   TUTORIALS   |   DEVELOPMENT   |   GALLERY   |   GAMES   |   ABOUT US 
  

M:Script Tutorial Part 2: Reusable Functions

By Max Glick

Continuing with our fb_centerWaist example, we will turn some parts of the code into reusable functions. This will lighten our workload when we develop new scripts that can benefit from code we wrote for this script.

Here we will create 2 support functions that can be reused in other scripts. The first will handle the slider bias feature for positioning the waist over the feet, and the second will provide the functionality of the power slider for turning the effects of the script on and off.

Script Diagram

First, create a new script to hold these functions. Give this a header like before and include the messiah.h header:

//#messiahscript

//////////////////////////////////////////////
// fb_utilityTUT
// Usage: none
// A set of utility functions for use by other scripts
//
// By Max Glick 9/8/2002
//////////////////////////////////////////////

#include <messiah.h>

Now save this file as fb_utilityTUT().msa.

First, let's look at the sections we want to replace in the fb_waistCenterTUT script.

//snip
//find the centered value
centerValue = (((obj1Value+obj2Value)/2)*(1-(abs(sldBiasValue - .5)*2))) + (obj1Value*((max(sldBiasValue,.5)*2)-1)) + (obj2Value*((abs(min(sldBiasValue,.5)-.5)))*2);

newPosition = centerValue-((centerValue-objCValue)*sldPowerValue);
//snip

Now we will add our 2 utility functions. The first gives us the bias functionality via a slider. We will name this fb_sliderBiasTUT. The second function will produce the on/off functionality. This will be name fb_valueFaderTUT.

The fb_sliderBiasTUT function will return a double value based on the position of a slider. We will need the following information to make this function work.

  1. The default starting value we are adjusting
  2. The minimum value this can be
  3. The maximum possible value
  4. The current value of the slider

First, we add a header for this function

MAIN HEADER…

//////////////////////////////////////////////
// fb_sliderBiasTUT
//
//////////////////////////////////////////////

Now we will add the function call for fb_sliderBiasTUT.

MAIN HEADER…

//////////////////////////////////////////////
// fb_sliderBiasTUT
//
//////////////////////////////////////////////

double fb_sliderBiasTUT(double dBaseValue, double dValMin, double dValMax, double sldBiasValue) {

}

We name this function fb_sliderBiasTUT because we are making a specific sub-function to be used by other scripts. This script will not have a main function, since it will not have any default functionality. Now we will add a variable to act as the container for the value this function will return.

MAIN HEADER…

//////////////////////////////////////////////
// fb_sliderBiasTUT
//
//////////////////////////////////////////////

double fb_sliderBiasTUT(double dBaseValue, double dValMin, double dValMax, double sldBiasValue) {
  // default variables
  double dNewValue;


}

Now we will take the equation from fb_waistCenterTUT() the currently provides this functionality and update the variables to reflect the name s of the argument values passed to this script.

MAIN HEADER…

//////////////////////////////////////////////
// fb_sliderBiasTUT
//
//////////////////////////////////////////////

double fb_sliderBiasTUT(double dBaseValue, double dValMin, double dValMax, double sldBiasValue) {
  // default variables
  double dNewValue;

  dNewValue = (dBaseValue*(1-(abs(sldBiasValue - .5)*2))) + (dValMax*((max(sldBiasValue,.5)*2)-1)) + (dValMin*(abs(min(sldBiasValue,.5)-.5)*2));

}

Finally, we will return the resulting value so the calling script can use it. This will finish this script.

//#messiahscript

//////////////////////////////////////////////
// fb_utilityTUT
// Usage: none
// A set of utility functions for use by other scripts
//
// By Max Glick 9/8/2002
//////////////////////////////////////////////

#include <messiah.h>

//////////////////////////////////////////////
// fb_sliderBiasTUT
//
//////////////////////////////////////////////

double fb_sliderBiasTUT(double dBaseValue, double dValMin, double dValMax, double sldBiasValue) {
  // default variables
  double dNewValue;

  dNewValue = (dBaseValue*(1-(abs(sldBiasValue - .5)*2))) + (dValMax*((max(sldBiasValue,.5)*2)-1)) + (dValMin*(abs(min(sldBiasValue,.5)-.5)*2));

  // return Value
  return( dNewValue );

}

Now we will add the fb_valueFaderTUT() function. Once again, we will start off by creating a subhead for this function.

MAIN HEADER AND FB_SLIDERBIAS…

//////////////////////////////////////////////
// fb_sliderBiasTUT
//
//////////////////////////////////////////////

This function will return a double value that is either equal to or between the position generated by the script and the last keyframed position of the object. To determine this value, we will need to pass 3 arguments to the function.

  1. The keyframed position
  2. The modified position
  3. The current value for the control slider.

We can now define our function.

MAIN HEADER AND FB_SLIDERBIAS…

//////////////////////////////////////////////
// fb_valueFaderTUT
//
//////////////////////////////////////////////

double fb_valueFaderTUT(double keyPosition, double modPosition, double sldPowerValue) {

}

As with the last function, we will add a container to hold the computed value.

MAIN HEADER AND FB_SLIDERBIAS…

//////////////////////////////////////////////
// fb_valueFaderTUT
//
//////////////////////////////////////////////

double fb_valueFaderTUT(double keyPosition, double modPosition, double sldPowerValue) {
  // default variables
  double dNewValue;


}

Add the equation from fb_waistCenterTUT that currently handles this task, and update the variables.

MAIN HEADER AND FB_SLIDERBIAS…

//////////////////////////////////////////////
// fb_valueFaderTUT
//
//////////////////////////////////////////////

double fb_valueFaderTUT(double keyPosition, double modPosition, double sldPowerValue) {
  // default variables
  double dNewValue;

  //find new value
  dNewValue = keyPosition - ((keyPosition - modPosition)*sldPowerValue);


}

Finally, we will return the resulting value so the calling script can use it. This will finish this script.

MAIN HEADER AND FB_SLIDERBIAS…

//////////////////////////////////////////////
// fb_valueFaderTUT
//
//////////////////////////////////////////////

double fb_valueFaderTUT(double keyPosition, double modPosition, double sldPowerValue) {
  // default variables
  double dNewValue;

  //find new value
  dNewValue = keyPosition - ((keyPosition - modPosition)*sldPowerValue);

  // return Value
return( dNewValue );

}

Here is the final fb_utilityTUT script:

//#messiahscript

//////////////////////////////////////////////
// fb_utilityTUT
// Usage: none
// A set of utility functions for use by other scripts
//
// By Max Glick 9/8/2002
//////////////////////////////////////////////

#include <messiah.h>

//////////////////////////////////////////////
// fb_sliderBiasTUT
//
//////////////////////////////////////////////

double fb_sliderBiasTUT(double dBaseValue, double dValMin, double dValMax, double sldBiasValue) {
  // default variables
  double dNewValue;

  dNewValue = (dBaseValue*(1-(abs(sldBiasValue - .5)*2))) + (dValMax*((max(sldBiasValue,.5)*2)-1)) + (dValMin*(abs(min(sldBiasValue,.5)-.5)*2));

  // return Value
  return( dNewValue );
}

//////////////////////////////////////////////
// fb_valueFaderTUT
//
//////////////////////////////////////////////

double fb_valueFaderTUT(double keyPosition, double modPosition, double sldPowerValue) {
  // default variables
  double dNewValue;

  //find new value
  dNewValue = keyPosition - ((keyPosition - modPosition)*sldPowerValue);

  // return Value
return( dNewValue );
}

Now we will replace the equations in the fb_waistCenter script with calls to these functions. Here is the original code for that section, as we saw earlier.

//snip
//find the centered value
centerValue = (((obj1Value+obj2Value)/2)*(1-(abs(sldBiasValue - .5)*2))) + (obj1Value*((max(sldBiasValue,.5)*2)-1)) + (obj2Value*((abs(min(sldBiasValue,.5)-.5)))*2);

newPosition = centerValue-((centerValue-objCValue)*sldPowerValue);
//snip

We will now replace it with calls to the functions we just created.

//snip
//find the centered value
centerValue = fb_utilityTUT.fb_sliderBiasTUT((obj1Value+obj2Value)/2, obj2Value, obj1Value, sldBiasValue);

//Adjust for off__on slider
newPosition = fb_utilityTUT.fb_valueFaderTUT(objCValue, centerValue, sldPowerValue);
//snip

To call these functions from the fb_waistCenter script, we must use the dot (.) notation, which is "the name of the msa file -- dot(.) -- the name of the function". The name of our script is fb_utilityTUT so we start with fb_utilityTUT., then add the function name. For example, fb_sliderBias, so the final call is fb_utilityTUT.fb_sliderBiasTUT(). Here's the updated fb_waistCetenter script.

//#messiahscript

//////////////////////////////////////////////
// fb_waistCenter() rev 2.0
//
// usage: fb_waistCenter(object1, object2, ObjectCentered, axis, slider, biasChannel, powerChannel)
// example fb_waistCenter(leftObject,rightObject,centerObject,0||2 [x/z axis],slider,"biasChannel","powerChannel")
// assign results to relevant channel of object to be centered
//
// (c) Max Glick 8/12/2002
// max@fullburner.com
//////////////////////////////////////////////

#include <messiah.h>

double main(object obj1, object obj2, object objC, int curAxis, object objSld, string bias, string power) {
//Default variables
  double obj1Value;  //control object 1
  double obj2Value;  //control object 2
  double objCValue;  //object to center
  double sldBiasValue;  //value of bias slider
  double sldPowerValue;  //value of power slider
  double centerValue;  //base center between object 1 and 2
  double newPosition;  //Return value
  int chnBias;  //chan name to find channel ID with
  int chnPower;  //chan name to find channel ID with

  //get slider channels
  chnBias = chanindex(objSld, bias);
  chnPower = chanindex(objSld, power);
  //get slider values
  sldBiasValue = motchan(objSld, chnBias, NOW);
  sldPowerValue = motchan(objSld, chnPower, NOW);
  //get current position
  obj1Value = fb_utility.fb_motchan3(obj1, curAxis, NOW);
  obj2Value = fb_utility.fb_motchan3(obj2, curAxis, NOW);
  objCValue = fb_utility.fb_motchan3(objC, curAxis, NOW);

  if (sldPowerValue == 0){
    newPosition = objCValue;

  } else {
    //find the centered value
    centerValue = fb_utility.fb_sliderBias((obj1Value+obj2Value)/2, obj2Value, obj1Value, sldBiasValue);

    //Adjust for off__on slider
    newPosition = fb_utility.fb_valueFader(objCValue, centerValue, sldPowerValue);
  }

  return( newPosition );
}

string help()
{
  string msg;
  msg = "Usage: fb_waistCenter(leftObject, rightObject, centerObject, 0||2 [x/z axis], slider, ''biasChannel'', ''powerChannel'')";
  MessageBox( msg, 0 );
  return( msg );
}