Hi Quinn, thanks for the feedback, it helped a lot. Answers to your questions and revised code follows:
No, the error appeared on the line where the sort occurred on t1Vehicle. This was mentioned and I included the error description to the comment on that line of code to reaffirm.
In the Task I had “let result = “ as I never alter the values I get back. I only need to read them back on the main thread and use these within some SKActions. They are altered during the function though, so I’ll change it to var for now in case it makes any difference. The changes occur before they are returned.
I think both adding the “var t1Vehicle = t1Vehicle” and the “inout” work. The reason I didn’t see the properties was my fault. I’d added a print instruction at the start of the function just to check and I was looking for “t1Vehicle.position”. I should have been looking for “t1Vehicle[x].position” where x is the offset. With this even the code I had worked.
You’re correct about me not needing the ‘enumerated’ in this loop. It was a hangover from my initial code. This will still be done within the function as the index tells me in what order along the road each vehicle is - they are sorted accordingly. Not needed in the main thread when creating the new array.
I suspect you’re right about never extending the array. I’ve never used one this way before. Guess I assumed the loop would progressively fill the array. No errors including ‘out of bounds’ came up. I’m not real sure what you meant by “NodeData (…)” however I made a change I think should work. See below..
//Run on Main Thread!
var temp1 = sKLAllVehicles.dropFirst()
var t1Vehicle: [NodeData] = []
var nodeData: NodeData = NodeData()
for (index, veh1Node) in temp1.enumerated() {
nodeData.name = veh1Node.name!
nodeData.size = veh1Node.size
nodeData.position = veh1Node.position
nodeData.lane = veh1Node.lane
nodeData.laps = veh1Node.laps
nodeData.speed = veh1Node.physicsBody!.velocity.dy
t1Vehicle.append(nodeData)
}
Although not shown here, I added an initialiser to the NodeData struct.
Topic:
Programming Languages
SubTopic:
Swift
Tags: