# 07 Nested Objects

This example shows you how to use nested objects in a blackboard.

These files can be found in the /Examples/\[example] and /Blackboards/Examples folders.

![Example 07 Nested Objects editor](/files/jVrJ6pVZxJpYEARJNNzT)

## NestedObjectBlackboard.cs

```csharp
﻿using System;
using CrashKonijn.RabbitBlackboard.Contracts;
using UnityEngine;

namespace CrashKonijn.BlackboardPro.Blackboards.Examples
{
    public enum GunType
    {
        Pistol,
        Rifle,
        Shotgun,
    }

    // Make sure the class is partial and uses [Serializable] attribute
    // The IAllAreSignals interface is used to mark the class as a signal container
    // With IAllAreSignals all valid fields are signals
    [Serializable]
    public partial class Gun : IAllAreSignals
    {
        private GunType _type;
    }

    // Make sure the class is partial and uses [Serializable] attribute
    // The ISomeAreSignals interface is used to mark the class as a signal container
    // With ISomeAreSignals you must manually mark fields as signals
    [Serializable]
    public partial class Bullets : ISomeAreSignals
    {
        // This won't be generated as a signal
        // The serializefield makes sure it's saved in the editor
        [SerializeField]
        private int clipSize = 5;

        // You manually have to mark the field as a signal
        [Signal]
        [RabbitBlackboard.Contracts.Min(0)]
        private int inGun = 5;

        // This marks it as a computed signal
        [Signal]
        private static bool _isEmpty(Signals.InGun inGun) => inGun.Value <= 0;

        // Methods won't collide with generated code
        public void Shoot()
        {
            // Make sure you use the property instead of the field
            // Updating the field won't do anything
            this.InGun--;
        }

        public void Reload()
        {
            this.InGun = this.clipSize;
        }
    }

    public partial class NestedObjectBlackboard : BlackboardBehaviour
    {
        // This is a nested object.
        private Bullets _bullets;

        // This is a nested object
        private Gun _gun;
    }
}

```

## NestedObjectBehaviour.cs

```csharp
﻿using CrashKonijn.BlackboardPro.Blackboards.Examples;
using CrashKonijn.RabbitBlackboard.Contracts;
using UnityEngine;

namespace CrashKonijn.BlackboardPro.Examples._07_nested_objects
{
    [RequireComponent(typeof(NestedObjectBlackboard))]
    public class NestedObjectBehaviour : MonoBehaviour
    {
        /* Simple hack to show a button in the editor */
        [Header("Shoots a bullet.")]
        [Button(nameof(Shoot))]
        public string shootButton;

        /* Simple hack to show a button in the editor */
        [Header("Reloads the gun")]
        [Button(nameof(Reload))]
        public string reloadButton;

        private NestedObjectBlackboard blackboard;

        public NestedObjectBlackboard Blackboard
        {
            get
            {
                if (this.blackboard == null)
                {
                    this.blackboard = this.GetComponent<NestedObjectBlackboard>();
                }

                return this.blackboard;
            }
        }

        private void OnEnable()
        {
            this.Blackboard.Bullets.InGunSignal.OnValueChanged.AddListener(this.OnBulletsChange);
        }

        private void OnDisable()
        {
            this.Blackboard.Bullets.InGunSignal.OnValueChanged.RemoveListener(this.OnBulletsChange);
        }

        private void OnBulletsChange(int bullets)
        {
            Debug.Log($"Bullets changed: {bullets}");
        }

        public void Shoot()
        {
            this.Blackboard.Bullets.Shoot();
        }

        public void Reload()
        {
            this.Blackboard.Bullets.Reload();
        }
    }
}

```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://blackboard.crashkonijn.com/examples/07_nested_objects.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
