foreach error moving trackevents to right

RonnieS wrote on 7/27/2016, 3:24 PM
I have solved my problem but it took so long to get to the bottom of it, I thought I would post this in the hope that it helps somebody else like me - relatively new to Vegas Scripting. Although I know a lot more about how Vegas works through this simple misunderstanding (or lack of knowledge) on my part.
On one track I had a series of titles, each had a duration of 4 seconds.
TrackEvent.Start was
43.00
64.50
79.00
83.00
87.50 etc
I wanted to place an intro file in and move all the events in each track to the right by the length of it ... simple ... I used the code ...


foreach(TrackEvent event in track.Events)

event.start = event.start + introlength; (in this case it was (24.50)

If you are here because of this problem, you know what happened.

The events seemed to get shuffled up with nothing having the correct start time. I eventually came across a posting by SonyPJM mentioning that Events are internally sorted by their start times. The penny dropped.

So when I changed one event's start property, the list resorted, so the next foreach statement picked the incorrect "next" one. It was chaos.

To figure this out it took 6-8 hours of reading entries on this wonderful forum. (From great subscribers such as JetDV and JohnnyRey (thanks guys for the hard work you put in over the years to help guys like me - just learning)

Initially, I solved it by naming the events individually in the rest of the code as they were added programmatically. This worked. I was pleased and went to bed.

Lying there, exhausted, it occurred to me that if I added the time to the list in reverse I wouldn't get this problem. So I used the code
for (i = track.Events.Count() - 1; i >= 0; i--)
{
track.Events[i].Start = track.Events[i].Start + introlength;;
}

Worked a treat.

So I guess if you add time to Event.Start work in reverse through the collection, in you subtract time work through from to top.

Hope this has been helpful.

Wish I knew how to add the code in a nice box in courier font. That looks so good.

Comments

Gary James wrote on 7/28/2016, 8:54 AM
An alternate way of doing this, like I did in TimelineTools, is to create a List of the Event Start and End positions. Then working from the old positions stored in the List, you could process the Event re-positioning either forward or backwards.
jetdv wrote on 8/6/2016, 11:30 AM
Yes, you have to go through the list backwards! Glad you figured it out.
Harmony wrote on 12/12/2020, 10:50 AM

Thank you so much! I had suspected this to be the case but nice thinking on the solution!