Originally Posted by
Spaghetti
Code:
bike_angle = GetDataIndex("bike_angle")
I'm not clear, bike_angle is a different variable than my bikeAngle and it contains the data field index? Why the field index must be persisted in a global variable?
The data file is loaded into memory as a two-dimensional array of data channels (columns) and data samples (rows). GetDataIndex("channel name") is the function that searches the channel names and returns the numerical index of the desired channel / field, so that we know which column in the array to use. That result is what you'd use as the parameter to GetDataValue() (or you can use one of the DFT_* constants instead if it's a common field). For the sake of demonstration (ie this works, but you don't want to actually do it this way), if you were to combine it, you'd get the actual value from the "bike_angle" data field by doing this: GetDataValue(GetDataIndex("bike_angle"))
This process is split into two separate functions for efficiency, because we don't need to repeat that channel name search for every new frame... That index will always be the same, so it's better to do that in the Background script and put the resulting channel index into a variable, which you can then use with GetDataValue() in the Foreground script. It's not a big difference in performance in this case, but every little bit helps. This fits right into the overall distinction between the Background and Foreground scripts... The Background script is only executed once (and reinitializes when you seek backwards or make a change to the display object), while the Foreground script is executed for each and every frame. Therefore, you want to lighten the load for that Foreground script as much as possible, and do as much of the work as you can in the Background script.
By the way, I noticed the speedometer has an indicator with two needles, one with the current values and another with the maximum value over a certain time frame which does exactly what I'm looking for. Except I haven't found a way to print the max needle value. Only the current speed value is displayed.
If I'm understanding correctly, you're talking about "Needle + Peak" mode, which is useful for indicating RPM shift points or other observed maximums. A good starting point for that would be to look at the "Maximum" style under the Enhanced Display Objects... That's a similar effect, but just doesn't automatically reset. Here's a quick and simple modification that would turn it into a Peak value, resetting whenever the max value hasn't increased for 5 seconds... New code is in bold.
Background Script
Code:
//This display requires "Persistent Script Variables" to be enabled
NumberColor = ColorA;
FontSize = 52;
Decimals = 2;
OffsetX = 170;
DefaultValue = -999999999; //Lower than any expected data value
MaxValue = DefaultValue;
HoldDuration = 5.0; //Seconds to hold the max display before resetting
MaxTime = -HoldDuration;
Foreground Script
Code:
if(DataValue > MaxValue || SampleTime > MaxTime + HoldDuration)
{
//Got a new maximum value, or it is time to reset it
MaxValue = DataValue;
MaxTime = SampleTime;
}
DrawNumber(MaxValue, Decimals, OffsetX, 0, NumberColor, FontSize, 0);
You could also take that further, like maybe displaying nothing until the DataValue starts increasing again by at least a certain amount, or incorporating DataTrigger to filter out low values, etc. Lots of possibilities to experiment with.
Here's a copy of that object you can download: Peak.rro