<!--
{
  "availability" : [
    "iOS: 9.0.0 -",
    "iPadOS: 9.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.11.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "GameplayKit",
  "identifier" : "/documentation/GameplayKit/GKCompositeBehavior",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "GameplayKit"
    ],
    "preciseIdentifier" : "c:objc(cs)GKCompositeBehavior"
  },
  "title" : "GKCompositeBehavior"
}
-->

# GKCompositeBehavior

A set of behaviors, each of which is a set of goals, that together influence the movement of an agent.

```
class GKCompositeBehavior
```

## Overview

By composing [`GKGoal`](/documentation/GameplayKit/GKGoal) objects into subgroups ([`GKBehavior`](/documentation/GameplayKit/GKBehavior) objects) and composing those behaviors into composite behaviors, you can control certain aspects of a [`GKAgent`](/documentation/GameplayKit/GKAgent) object’s movement in concert. To assign a behavior to an agent, use its [`behavior`](/documentation/GameplayKit/GKAgent/behavior) property.

For example, you might create a behavior for a set of agents to stay together as a flock (with cohesion, alignment, and separation goals) while loosely following a path. With a single [`GKBehavior`](/documentation/GameplayKit/GKBehavior) object, whenever you want to change the importance of the flocking goals relative to the path-following goals, you’d need to individually change the weight of each goal. With a composite behavior, you can adjust the relative influence of a group of goals together, as in the following code.

```swift
let flock = GKBehavior(goals: [
    GKGoal(toAlignWith: agents, maxDistance: 10, maxAngle: .pi/4),
    GKGoal(toCohereWith: agents, maxDistance: 10, maxAngle: .pi/4),
    GKGoal(toSeparateFrom: agents, maxDistance: 10, maxAngle: .pi/4),
])
let meanderOnPath = GKBehavior(goals: [
    GKGoal(toFollow: path, maxPredictionTime: 1, forward: true),
    GKGoal(toWander: 10)
])
let composite = GKCompositeBehavior(behaviors: [
    flock, meanderOnPath
])
```

After constructing this behavior, you can use the [`setWeight(_:for:)`](/documentation/GameplayKit/GKCompositeBehavior/setWeight(_:for:)) method to increase or decrease the influence of the `flock` and `meanderOnPath` behaviors relative to one another.

To learn more about using goals and agents, see [Agents, Goals, and Behaviors](https://developer.apple.com/library/archive/documentation/General/Conceptual/GameplayKit_Guide/Agent.html#//apple_ref/doc/uid/TP40015172-CH8) in [GameplayKit Programming Guide](https://developer.apple.com/library/archive/documentation/General/Conceptual/GameplayKit_Guide/index.html#//apple_ref/doc/uid/TP40015172).

## Topics

### Creating a Composite Behavior

[`init(behaviors:)`](/documentation/GameplayKit/GKCompositeBehavior/init(behaviors:))

Creates a composite behavior from the specified individual behaviors.

[`init(behaviors:andWeights:)`](/documentation/GameplayKit/GKCompositeBehavior/init(behaviors:andWeights:))

Creates a behavior with the specified behaviors and weights.

### Managing the Individual Behaviors in a Composite Behavior

[`setWeight(_:for:)`](/documentation/GameplayKit/GKCompositeBehavior/setWeight(_:for:))

Sets the weight for the specified individual behavior’s influence on agents, adding that behavior to the composite behavior if it is not already present.

[`weight(for:)`](/documentation/GameplayKit/GKCompositeBehavior/weight(for:))

Returns the weight for the specified individual behavior’s influence on agents.

[`remove(_:)`](/documentation/GameplayKit/GKCompositeBehavior/remove(_:))

Removes the specified individual behavior from the composite behavior.

[`removeAllBehaviors()`](/documentation/GameplayKit/GKCompositeBehavior/removeAllBehaviors())

Removes all individual behaviors from the composite behavior.

[`behaviorCount`](/documentation/GameplayKit/GKCompositeBehavior/behaviorCount)

The number of individual behaviors in the composite behavior.

### Working with Behaviors Using Subscript Syntax

[`subscript(_:)`](/documentation/GameplayKit/GKCompositeBehavior/subscript(_:)-6jng9)

Returns the weight associated with the behavior specified by subscript syntax.

[`setObject:forKeyedSubscript:`](/documentation/GameplayKit/GKCompositeBehavior/setObject:forKeyedSubscript:)

Sets the weight for the behavior specified by subscript syntax.

[`subscript(_:)`](/documentation/GameplayKit/GKCompositeBehavior/subscript(_:)-6krdg)

Returns the individual behavior at the specified index in the composite behavior’s list of behaviors.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)