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 GameData to Version3 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:

  • The parent gameObject: ChoicePanel, requires:

    • Hide_Show_Panel script component

    • CanvasGroup component - REMEMBER TO ADD CANVAS GROUP

    • Vertical Layout Group component

  • Each UI-Button will require an instance of the SaveChoice Script (see below).

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.

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

SaveChoice.cs - Attach to each child Button

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

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

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

SaveChoice.cs

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

Last updated