User Defined Type

HI.

I am trying to create the equivilant of a UDT and getting totally confused :frowning:

Basically I am trying to create stats storage for 4 riders.

Rider1

id
name
laptimes[]

Rider2

id
name
laptimes[]

and so on.

I have tried a few variations, non of which seem to even run. This is the only one I can get to run without error, but it doesnt do what I am wanting it to do.

[lua]local riders = {
id = 0,
name = 0,
riderno = 0,
lapno = 0,
laptime = {}
}
riders2 = {}

riders2[1] = riders
riders2[2] = riders
riders2[1].laptime[2] = 0
riders2[2].laptime[2] = 10

print(riders2[1].laptime[2])
print(riders2[2].laptime[2])[/lua]

When I run this example, Lap 2 has the same time for both riders2[1] and riders2[2]??

How can I write this so that an array of riders can have their own set of laps? Any help would be greatly appreciated.

thanks
[import]uid: 103163 topic_id: 20538 reply_id: 320538[/import]

You define a table named “riders” which has some attributes. Then you set riders2[1] and riders2[2] as being the table riders, so actually when you call for riders2[1] or riders2[2], you’re refering to the same “riders” table you created first.

[code]
local riders = {
{
id = 1,
name = “John”,
riderno = 1,
lapno = 3,
laptime = { }
},
{
id = 2,
name = “Steve”,
riderno = 2,
lapno = 5,
laptime = { }
}
}

riders[1] = riders
riders[2] = riders
riders[1].laptime[2] = 0
riders[2].laptime[2] = 10

print(riders[1].laptime[2])
print(riders[2].laptime[2])
[/code] [import]uid: 61899 topic_id: 20538 reply_id: 80599[/import]

Thanks for your reply, and I thought you were onto something there, but unfortunately it still gives the same error that I was getting before

“attempt to index field ‘laptime’ (a nil value)”

[import]uid: 103163 topic_id: 20538 reply_id: 80603[/import]

I’m sorry but I made a mistake, I copied 2 unnecessary lines from your code above, this is the correct one:

[code]
local riders = {
{
id = 1,
name = “John”,
riderno = 1,
lapno = 3,
laptime = { }
},
{
id = 2,
name = “Steve”,
riderno = 2,
lapno = 5,
laptime = { }
}
}

riders[1].laptime[2] = 0
riders[2].laptime[2] = 10

print(riders[1].laptime[2])
print(riders[2].laptime[2])
[/code] [import]uid: 61899 topic_id: 20538 reply_id: 80624[/import]

Thats excellent thanks!
Only other question is why didnt i see this before :slight_smile:

[import]uid: 103163 topic_id: 20538 reply_id: 80627[/import]