Newbie Question about Lap Timing

Posts: 27
Hi Guys,

We're running an Indy 500 type event, and we are doing the proper way to qualify with the event, using a 4 lap average. Is there a way to show the last 4 laps on screen for a qualifier, and have them average out?

I'm a total newbie into this, and going to be diving deep into it over the weekend, and is there any good starter templates that I can use to build off of that anyone would recommend? I'm also not opposed to buying an entire theme built if the price is right.
Posts: 785
The simplest way to show laptimes is to use the "laptimes" dataset. It will return the laptimes of the currently followed / focused driver, starting with the most recent and counting down. You can use a static ticker with 4 items to show the last 4 laps.

I don't believe we offer a ready-made average laptime binding. To get that you would need a script to grab the laps you want and average them yourself. In principle we can add average laptimes but then there are many questions to answer and not everyone may want the same thing (e.g. how many laps to average? How do you handle invalid laps? etc).

A relatively simple script could take as input an "entitysessionresult_object" and go through the Laps list of the result, taking only valid laps and calculating their average:
using System;
using ATVO.ThemesSDK;
using ATVO.ThemeEditor.ThemeModels;
using ATVO.ThemeEditor.Scripting.DotNET;
using ATVO.ThemesSDK.Data.Results;
using System.Collections.Generic;
using System.Linq;

namespace Scripts
{
public class AverageLaptime : IScript
{
public object Execute(ThemeContentItem item, object value, string parameter, ISimulation sim)
{
var result = (IEntitySessionResult)value;

var lastLapIndex = result.Laps.Count - 1;
var laptimes = new List<float>();

// Loop backwards 4 laps starting from the most recent lap
for (int i = lastLapIndex; i <= lastLapIndex - 4; i--)
{
// Stop if we reached the first lap
if (i < 0)
break;

var lap = result.Laps[i];
if (lap.Time > 0) // invalid laps have -1 laptime
{
laptimes.Add(lap.Time);
}
}

// Calculate the average over the valid laps
var averageLaptime = laptimes.Average();
return averageLaptime.ToString();
}
}
}


You can adapt this depending on your needs, for example how to handle invalid laps etc.

Or if you prefer a bit shorter notation:
var averageLaptime = result.Laps 	// Laps list of this result
.Select(l => l.Time) // Select the laptime
.Reverse() // Reverse so we start at the last lap
.Where(t => t > 0) // Take only valid laps where the time > 0
.Take(4) // Take up to 4 laps
.Average(); // Average
return averageLaptime.ToString();

Edited (1 time)
Posts: 27
I must be missing a step. I made a widget, then a subwidget and a label, with the laptimes data set, and all I see on my screen when i test it in a session is the {0:0} and nothing updates.

Like I said, this is my first time using this, and its all rather confusing so far. I appreciate the help so far.
Posts: 287
Do you test only in EditTheme or in ATVO with a real Qualifiying session ?
Cause scripts can't return informations without iracing running.
Edited (1 time)
Posts: 785
Billy Rowlee wrote:
I must be missing a step. I made a widget, then a subwidget and a label, with the laptimes data set, and all I see on my screen when i test it in a session is the {0:0} and nothing updates.

Like I said, this is my first time using this, and its all rather confusing so far. I appreciate the help so far.
Did you also apply a binding to the label so it shows the data you want? In your case most likely the 'time' or 'time_formatted' data binding. Please read my post on the data binding system for more tips: https://atvo.appgineering.com/Forum/Thread/12

Emmanuel Suter wrote:
Do you test only in EditTheme or in ATVO with a real Qualifiying session ?
Cause scripts can't return informations without iracing running.
Scripts run in the editor too when you "debug" the theme (press the play button).
Posts: 100
Nick Thissen wrote:
The simplest way to show laptimes is to use the "laptimes" dataset. It will return the laptimes of the currently followed / focused driver, starting with the most recent and counting down. You can use a static ticker with 4 items to show the last 4 laps.

I don't believe we offer a ready-made average laptime binding. To get that you would need a script to grab the laps you want and average them yourself. In principle we can add average laptimes but then there are many questions to answer and not everyone may want the same thing (e.g. how many laps to average? How do you handle invalid laps? etc).

A relatively simple script could take as input an "entitysessionresult_object" and go through the Laps list of the result, taking only valid laps and calculating their average:



I've been trying to figure this out, forever... Was pulling my hair out a few months ago. You make this look so easy! This will be very useful, I'll have to test this out sometime. Thank you!