Post

Replies

Boosts

Views

Activity

Reply to Concurrent Processing of SKSpriteNode Copied Data in SpriteKit
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:
Oct ’22
Reply to Concurrent Processing of SKSpriteNode Copied Data in SpriteKit
Getting closer. I tried copying the data before accessing the background thread. The issue I have now is that Swift considers 't1Vehicle' a constant whereas I need it to be a variable. I used a struct here which I have not done before (perhaps there's a better way?). The error code when trying to sort t1Vehicle is "Cannot use mutating member on immutable value: 't1Vehicle' is a 'let' constant". I've tried a few things to get around this without success. It only allows me to make a method mutating. In the code below I've only included the data from one track for readability. //This code on Main Thread! var temp1 = sKLAllVehicles.dropFirst()      //Straight Track Vehicles: Ignore 'All Vehicles' var t1Vehicle: [NodeData] = [] for (index, veh1Node) in temp1.enumerated() {     t1Vehicle[index].name = veh1Node.name!     t1Vehicle[index].size = veh1Node.size     t1Vehicle[index].position = veh1Node.position     t1Vehicle[index].lane = veh1Node.lane     t1Vehicle[index].laps = veh1Node.laps     t1Vehicle[index].speed = veh1Node.physicsBody!.velocity.dy } Task {     let result = await findObstacles(t1Vehicle: t1Vehicle)     let t1Vehicle = result.t1Vehicle     let t2Vehicle = result.t2Vehicle } ...and I included the function as a method within a struct (I'd never done this before!) struct NodeData {     var name: String     var position: CGPoint     var size: CGSize     var lane: CGFloat     var speed: CGFloat     var laps: CGFloat mutating func findObstacles(t1Vehicle: inout [NodeData]) async -> (t1Vehicle: [NodeData], t2Vehicle: [NodeData]) {         //  Do calculations on background thread                  t1Vehicle.sort(by: {$0.position.y < $1.position.y}) //ERROR: Cannot use mutating member on immutable value: 't1Vehicle' is a 'let' constant                  //Rest of method here            } } Any idea how to correct this? Thanks!
Topic: Programming Languages SubTopic: Swift Tags:
Oct ’22
Reply to Concurrent Processing of SKSpriteNode Copied Data in SpriteKit
Thanks for your feedback Quinn. I was beginning to think the only way I could achieve a result was by iterating over every SKNode, extracting information such as position, speed and size then writing all this into another array. Iterating through hundreds of nodes this way would be time consuming in itself and far from elegant. I wasn’t aware of ‘Sendable’ before now but it does sound like it could do the job. A little research I did just now indicates that I should also make a ‘deep copy’ of the original. I’ve quickly tried this without success. If you could shed any light it would be appreciated. I haven’t added the ‘Sendable’ part yet. Figured I need a deep copy first. The code I tried was:    var t1Vehicle = sKLAllVehicles.dropFirst()     t1Vehicle = t1Vehicle.map{ $0.copy() } //<- error here Task {     let result = await findObstacles()     let t1Vehicle = result.t1Vehicle     let t2Vehicle = result.t2Vehicle } The second line has the error “Cannot assign value of type '[Any]' to type 'Array.SubSequence' (aka ‘ArraySlice’)”. Any suggestions to solve this and include the Sendable option? Also, should I be returning values from the function as type ‘Vehicle’ or would this also have to change? (Sorry for being a pain but the only coding I’ve done before was assembler so I’m on a steep learning curve!)
Topic: Programming Languages SubTopic: Swift Tags:
Sep ’22