Apologies, the sun got to me today and I wasn't thinking properly. It got to my iMac too, it shut itself down, so I'm on nights.
Thinking about how I had so many different checks there was no one size fits all, but the commonality between them was the initial check to see if the node existed, so I created a function to streamline that, accepting a dictionary and returning a bool value for each, of which then the dedicated function could then make the specific changes:
import SpriteKit
import GameplayKit
enum NormalSeasonGUISKLabelNodes: String, CaseIterable {
case labelRatingA
case labelRatingB
case labelRatingC
case labelRatingD
case labelRatingE
}
enum NormalSeasonGUISKSpritelNodes: String, CaseIterable {
case ratingButtonA
case ratingButtonB
case ratingButtonC
case ratingButtonD
case ratingButtonE
}
var labelRatingA: SKLabelNode = SKLabelNode()
var labelRatingB: SKLabelNode = SKLabelNode()
var labelRatingC: SKLabelNode = SKLabelNode()
var labelRatingD: SKLabelNode = SKLabelNode()
var labelRatingE: SKLabelNode = SKLabelNode()
var ratingButtonA: SKSpriteNode = SKSpriteNode()
var ratingButtonB: SKSpriteNode = SKSpriteNode()
var ratingButtonC: SKSpriteNode = SKSpriteNode()
var ratingButtonD: SKSpriteNode = SKSpriteNode()
var ratingButtonE: SKSpriteNode = SKSpriteNode()
lazy var normalSeasonGUISKLabelNodes: [NormalSeasonGUISKLabelNodes.RawValue : SKLabelNode] =
[
NormalSeasonGUISKLabelNodes.labelRatingA.rawValue : labelRatingA,
NormalSeasonGUISKLabelNodes.labelRatingB.rawValue : labelRatingB,
NormalSeasonGUISKLabelNodes.labelRatingC.rawValue : labelRatingC,
NormalSeasonGUISKLabelNodes.labelRatingD.rawValue : labelRatingD,
NormalSeasonGUISKLabelNodes.labelRatingE.rawValue : labelRatingE,
]
lazy var normalSeasonGUISKSpritelNodes: [NormalSeasonGUISKSpritelNodes.RawValue : SKSpriteNode] =
[
NormalSeasonGUISKSpritelNodes.ratingButtonA.rawValue : ratingButtonA,
NormalSeasonGUISKSpritelNodes.ratingButtonB.rawValue : ratingButtonB,
NormalSeasonGUISKSpritelNodes.ratingButtonC.rawValue : ratingButtonC,
NormalSeasonGUISKSpritelNodes.ratingButtonD.rawValue : ratingButtonD,
NormalSeasonGUISKSpritelNodes.ratingButtonE.rawValue : ratingButtonE,
]
//if node is present then associate it with GUI element and return true value
func nodesPresent(nodesList: [String : Any], forClass: AnyClass) -> Bool {
var returnValue: Bool = false
for (key, value) in nodesList {
var object = value as! SKLabelNode //cannot get forClass parameter accepted
self.enumerateChildNodes(withName: "//*") {
node, stop in
if let theCamera: SKCameraNode = node as? SKCameraNode {
self.camera = theCamera
if theCamera.childNode(withName: key) is SKLabelNode {
object = theCamera.childNode(withName: key) as! SKLabelNode
print("node present : \(key)")
returnValue = true
} else {
print("node missing : \(key)")
returnValue = false
}
}
}
}
return returnValue
}
//setup the main nodes for camera GUI: both labels and buttons, performing specific actions
func setupMainGUINodes() {
//setup label nodes
for (key, value) in normalSeasonGUISKLabelNodes {
if nodesPresent(nodesList: ([key : value]), forClass: SKLabelNode.self) == true {
value.text = ""
}
}
//setup button nodes
for (key, value) in normalSeasonGUISKSpritelNodes {
if nodesPresent(nodesList: ([key : value]), forClass: SKSpriteNode.self) == true {
for spriteNode in NormalSeasonGUISKSpritelNodes.allCases {
switch spriteNode {
case .ratingButtonA : ratingButtonA.isHidden = true
case .ratingButtonB : print("")
case .ratingButtonC : print("")
case .ratingButtonD : ratingButtonD.isHidden = true; ratingButtonD.name = "egg"
case .ratingButtonE : ratingButtonE.isHidden = true; ratingButtonE.name = "bacon"
}
}
}
}
}
This allows me the flexibility to cut down on the repetitive code, performing the uncommon changes whenever needed, whilst still being readable.
The only issue was the "forClass" parameter, which I could not get it to accept, that will obviously need to change if it's to be of general use.
nb. if you know of a way to do nothing on a case statement (I use empty print) please let me know. The case statements must be exhaustive, so I have to put it in, even when I don't need to add any more changes to something.