1 Reply
      Latest reply on Mar 1, 2018 6:43 AM by bubbleslabrador
      bubbleslabrador Level 1 Level 1 (0 points)

        Hi there,

         

        I cannot figure out how on earth to use Gameplay Kits DecisionTrees. I have seen a few examples of how to create them, however I get a lot of errors when it comes to the NSObjectProtocol sections. I found I can use NSString and stuff in place of them and the decision tree that is created looks fine but I get lost with inputting the answers into the tree to get an appropriate answer. I have attempted below at it in a class but it produces nothing of what I want. Are they obsolete or is the documentation just strange, cause the example it gives dont work.

         

         

        //ATTEMPT//

        import UIKit

        import GameplayKit

        import SpriteKit

        class AITrees {

          

          

          

            let CanCastAny = "Can I Cast Any Cards In My Hand?" as NSString

            let MoveNextTurn = "Move To Next Turn" as NSString

            let OneSlotLeft = "Do I Have One Slot Left" as NSString

            let PrintYes = "Yes" as NSString

            let PrintNo = "No" as NSString

          

            init() {}

          

            func EasyAITree(){

              

                let easyDecisionTree = GKDecisionTree(attribute: CanCastAny)

                let root = easyDecisionTree.rootNode

             

              

                root?.createBranch(predicate: NSPredicate(value: true), attribute: OneSlotLeft)

                root?.createBranch(predicate: NSPredicate(value: false), attribute: MoveNextTurn)

              

              

                let answers: [NSString : NSObjectProtocol] = [

                    "Can I Cast Any Cards In My Hand?" : false as NSObjectProtocol

                ]

              

                print(answers)

              

                print(easyDecisionTree)

              

                print(easyDecisionTree.findAction(forAnswers: answers)!)

              

              

            }

        }

        ///////////////////////////////////////////////

        • Re: How Do You Use GKDecisionTrees?
          bubbleslabrador Level 1 Level 1 (0 points)

          I did it! After a day of slamming in random answers and reading random blogs on the internet, i've scooby doo'd the answer.

           

          I couldn't figure out how to use a predicate as true/false, but by using a value of 1 or 0 and creating a dictionary of String hashable to NSNumbers (I know I could've used 0 or 1 in place or true and false but I just found it simpler to understand in creating). Also anyone looking at the documentation and has no idea what it means by NSObjectProtocol like I did, it means like NSString or NSNumber or NSArray; I couldn't really find a lot that was saying this. I left a working class below for the DecisionTree, the answer it produces in the state below is "No" but you can expand it out.

           

          import UIKit

          import GameplayKit

          import SpriteKit

          class AITrees {

            

            

            

              let CanCastAny = "Can I Cast Any Cards In My Hand?" as NSString

              let MoveNextTurn = "Move To Next Turn" as NSString

              let OneSlotLeft = "Do I Have One Slot Left" as NSString

              let PrintYes = "Yes" as NSString

              let PrintNo = "No" as NSString

            

              init() {}

            

              func EasyAITree(){

                

                  let easyDecisionTree = GKDecisionTree(attribute: CanCastAny)

                  let root = easyDecisionTree.rootNode

                

                

                  let insideBranch = root?.createBranch(value: 1, attribute: OneSlotLeft)

                  root?.createBranch(value: 0, attribute: MoveNextTurn)

                  insideBranch?.createBranch(value: 1, attribute: PrintYes)

                  insideBranch?.createBranch(value: 0, attribute: PrintNo)

                

                  let answers = [

                      "Can I Cast Any Cards In My Hand?" : true as NSNumber,

                      "Do I Have One Slot Left" : false as NSNumber

                  ]

                

                  print(answers)

                

                  print(easyDecisionTree)

                

                  let answer = easyDecisionTree.findAction(forAnswers: answers)

                  print(answer!)

              }

          }