# SaveChoice, ChoicePanel

### **ChoicePanel Prefab:**

You will need to create a simple Prefab that you can use in multiple times within one scene and within multiple scenes to allow the player to make choices during gameplay to determine subsequent game conditions.

Update G[ameData to Version3 ](/cs2335/overview-branchlogic/gamedata-v3.md)to include logic for SaveChoice

The **ChoicePanel** prefab is quite simple, It's very similar to the OptionPanel prefab,  it must be configured as detailed below: &#x20;

* The parent gameObject: **ChoicePanel**,  requires:
  * &#x20;[**Hide\_Show\_Panel**](/cs2335/project-2-dialog/hideshow-panel-script.md) script component&#x20;
  * **CanvasGroup** component - **REMEMBER TO ADD CANVAS GROUP**
  * **Vertical Layout Group** component<br>
* **Each UI-Button** will require an instance of the [**SaveChoice Script**](/cs2335/overview-branchlogic/dictionary-user-choice-data/choicepanel.md#savechoice-cs) (see below).&#x20;

![](/files/-MLdcmJPbZMfh5yomKL5)

![](/files/-MLdciEmwwWAPiRPgOTN)

![](/files/-MLdcsjtbJ4cvMHmveod)

![](/files/-MM7hW9KjZUnfAqSaXyq)

![](/files/-MM7hc9vNcN1OO-MQOxP)

## SaveChoice.cs

The general idea is that we'd like a simple script that can be attached to any Button so that clicking the Button will update the database for the key,value pair associated with the Button. &#x20;

**Button onClick.AddListener( )**  In addition to storing the SaveChoice key,value pair we can also configure these buttons to open panels, trigger dialog, to implement our conditional branching narrative structure. This will be covered in the section: [Configure UI-Button Listeners](/cs2335/overview-branchlogic/configure-ui-button-listeners.md)

### SaveChoice.cs - Attach to each child Button

![](/files/-MM7bnzRGJRt4zNTnYJn)

#### Set the Key, Value pair for the SaveChoice Component

The script below is attached to a Button, and has code to be executed when the Button is clicked, so first we have to find the Button Component in Start, this is a bit of a work-around.  When one of the Buttons is clicked, it updated the Dictionary in GameData.  Then it finds it's parent and gets the CanvasGroup of the parent and hides the entire panel.  Then it checks to see if the parent panel has an attached Hide\_Show\_Panel script with a nextPanelToOpen.  It opens that panel and checks to see if there is a ShowChoice script on that Panel, if so, it executes: UpdateDisplay( ).  See code for that script below.

### KeyValuePair.cs - Data Class created for Dictionary Data&#x20;

```csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class KeyValuePair
{
    public string key;
    public string value;
}

```

### SaveChoice.cs

```csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class SaveChoice : MonoBehaviour
{
    //set in inspector for each Button

    [SerializeField]
    ConversationList nextConversation; //if next conversation exists

    [SerializeField]
    DialogManager dialogManager; //if next conversation exists, set dialogManager

    [SerializeField]
    CanvasGroup nextPanelToOpen; //if not using dialogManager or nextConversatoin

    [SerializeField]
    public List<KeyValuePair> choiceList; //a list of dictionary key-value pairs to be set when the button is clicked

    Button thisItem;
    // Start is called before the first frame update
    private void Start()
    {
        thisItem = GetComponent<Button>();
        thisItem.onClick.AddListener(OnChoiceSelected);
    }

    public void OnChoiceSelected()
    {
        //loop through list of keyValuePairs, Save key,value pair to GameData dictionary: choiceData
        foreach (KeyValuePair keyValuePair in choiceList)
        {
            //if special gameState - type value
                 GameData.instanceRef.SaveChoice(keyValuePair.key, keyValuePair.value); //don't use improveState value
       
            Debug.Log("Saved Key " + keyValuePair.key + " Saved Value " + GameData.instanceRef.GetChoice(keyValuePair.key));  //pull from stored value
        }

        thisItem.Select(); //show Button selected Image

        //Find Parent Panel and hide
        GameObject parentGameObject = this.transform.parent.gameObject; //find parent
        if (parentGameObject != null)
        {
            CanvasGroup parentCG = parentGameObject.GetComponent<CanvasGroup>();
            if (parentCG != null)
            {
                Utility.HideCG(parentCG); //hide the parent choice panel
            }
        }
        //either set dialogManager with nextConversation
        if (nextConversation != null && dialogManager != null) //NextPanel will be dialogManager
        {
            dialogManager.TriggeredDialog(nextConversation); //
        }
        else if (nextPanelToOpen != null)//or show panel nextPanelToOpenindicated 
        {
            Utility.ShowCG(nextPanelToOpen); //on to open next panel specified on parent
        }
    }

}//end class


```

###


---

# 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://kdoore.gitbook.io/cs2335/overview-branchlogic/dictionary-user-choice-data/choicepanel.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.
