Initializer

init(predicate:retractingFact:grade:)

Creates a data-driven rule with the specified predicate, whose action retracts a fact in the rule system evaluating the rule.

Declaration

convenience init(predicate: NSPredicate, retractingFact fact: NSObjectProtocol, grade: Float)

Parameters

pred

A predicate to be tested when the rule is evaluated.

fact

An object representing a fact to retract when the rule’s predicate is satisfied. (For details on facts in rule systems, see the facts property in GKRuleSystem.)

grade

An amount by which to decrease the fact’s membership grade if the rule’s predicate is satisfied.

Return Value

A new rule object.

Discussion

Rules created using this method encode their predicate and action when archived with the NSKeyedArchiver class. You can use this feature to support saving and loading rules, editing rules in-game, or building tools that separate your gameplay design and game programming tasks.

Rules based on NSPredicate objects typically test information in the state dictionary of the rule system evaluating the rule. For example, the following code creates a rule you might use to determine whether an enemy character in a game behaves aggressively. (This example presumes the rule system’s state dictionary contains an object for the key player, which in turn exposes a numeric value for the key health.)

let healthTest = NSPredicate(format: "$player.health > 50")
let rule = GKRule(predicate: healthTest, retractingFact: "player_weak", grade: 0.5)

See Also

Creating Data-Driven Rules

init(predicate: NSPredicate, assertingFact: NSObjectProtocol, grade: Float)

Creates a data-driven rule with the specified predicate, whose action asserts a fact in the rule system evaluating the rule.