If you really want to stick with the for loop, rather than opting for a foreach, which is easier to understand in the long run and I suspect probably better optimised (a .NET expert might want to confirm or deny), then the following:
Yup, you could do that, but (in my opinion ) the foreach makes it clearer as to what's happening, and the C# complier produces the same underlying (byte-)code anyway. As a rule of thumb in C#, unless I specifically want to know which iteration of a loop I'm on, I always use foreach.
NumC contains the number of valid compcars in the struct? So if you have 5 players on the track. only one compcar comes in. So wtf is i < MCI.NumC gonna do?
No. A CompCar is a single entry. The CompCar Info variable is defined as an array of up to 8 CompCar structs.
If NumC was always 1 then there would be absolutely no point in its existance.
If LFS_External is telling you that there is always 1, then that is a bug.
struct IS_MCI // Multi Car Info - if more than 8 in race then more than one of these is sent { byte Size; // 4 + NumC * 28 byte Type; // ISP_MCI byte ReqI; // 0 unless this is a reply to an TINY_MCI request byte NumC; // number of valid CompCar structs in this packet
[B]CompCar Info[8];[/B] // car info for each player, 1 to 8 of these (NumC) };
There is usually more than one CompCar struct, unless there is only one person on the track. The CompCar is an array of up to 8 different CompCar structs, which are aligned one after the other in memory.
The NumC tells you how many CompCar's are in the MCI packet, regardless of how many players are on track.
If you want you could also use mci.Info.Length to get the number of elements in the CompCar array (presuming LFS_External doesn't use a fix sized array for it...).
No, that's cool. This discussion isn't really about C#, it's about the inner workings of InSim.
I think it's the Visual Studio IDE. Using intellisense so much means I'm only fast at typing the first two characters of every word... The rest take ages...
Yes, if you loop over the players then the program will crash (ArgumentOutOfRange exception) if there are more players than CompCars (which there will be if there are more than eight players). You need to loop over the CompCars in the MCI packet instead. I already posted previously in this thread a foreach loop that I think should work.
I agree foreach is cleaner, but you can't really use foreach here because the last mci packet has some invalid info[] objects since LFS External uses a static array for it.
Though you could use foreach and make a counter that breaks the loop when mci.numc is reached, but I don't think that is quite efficient.
Something like this:
void MCI(Packets.IS_MCI MCI) { byte counter;
foreach (Packets.CompCar info in MCI.Info) { // code
Yes, I would rather use foreach as like you say, its cleaner and much easier to use. I was going to say exactly what T-RonX said, only he beat me to it
Thats the reason for my persistance in the 'for' loop. I think i might try and carry on with my own lib. Although, Ill probley have the same issues...
i have got some buttons saying the location of yourself coming up but they only come up on the first person out of the pits, unless they spectate then join again, how could i get around this?
if two people left the pits the second person has the first persons MCI information like co-ordinates, could someone help me?