NPC Activation

The NPCActivation Script Component can be added to any gameObject where the visibility is dependent on a Key,Value pair of data stored in the userChoice dictionary. The key and valid values for visualization are configured in the inspector. In the case of RobotState, the Robot's visibility in the Store, Forrest, and EndScene require that key: RobotState has value: companion. It that key-value pair does not exist in the dictionary, then the gameObject has SetActive( false ).

The image above shows that the key: RobotState must have value of companion.

NPCActivation: For a given key, there can be a list of possible values, such that if any of these values exist in the dictionary for the given key, then the gameObject will be visible in the scene.

This occurs in the BeginScene when the ChoiceButton, with the SaveChoice script attached: as configured below: Selling the Robot: saves: key: RobotState, value: parts,

We can execute either ActivateNPC( ) or DeactivateNPC( ) using a UI button if we want the robot disappears to disappear when the Button with SaveChoice is Executed

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

public class NPCActivation : MonoBehaviour
{
    [SerializeField]
    string key; //value required in order to be visible

    [SerializeField]
    string[] active_values; //set values that determine visibility within each scene


    // Start is called before the first frame update
    void Start()
    {
       
           CheckActiveStatus(); //check curValue stored value to see if it matches any scene set values, so that it is visibl
    }

    //call each time gameData playerData is updated
    //check choiceData dictionary to see if the curValue matches any values for activation of
    //this gameObject within this scene.
    public void CheckActiveStatus()
    {
        string curValue = GameData.instanceRef.GetChoice(key);
        bool activate = false;
        //for key, see if the actual stored value matches any of the listed values
        //if so, then the gameObject should be activated.
        for( int i=0; i < active_values.Length; i++)
        {
            if (curValue == active_values[i]) //see if current value matches any required for visibility
            {
                activate = true;
            }
        }
        if(activate)
        {
            ActivateNPC();
        }
        else
        {
            DisableNPC();
        }
        
    }

    // Update is called once per frame
    public void ActivateNPC()
    {
        this.gameObject.SetActive(true);
    }

    public void DisableNPC()
    {
        this.gameObject.SetActive(false);
    }

} //end class

Last updated