Making references to different classes

I am currently developing a Mas Effect like game, on a extremely small scale, I am currently building a conversation system, my problem is that I need a couple of if-statements for every new NPC, is there a way to make it more streamlined?

Here is the code I use on the player:

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

public class ConversationSystem : MonoBehaviour
{
    private TextControlShopClerk1 sCText;

    public int threshold;
   
    public GameObject wheel;

    public GameObject TChoiceImg;
    public GameObject TRChoiceImg;
    public GameObject TLChoiceImg;
    public GameObject BChoiceImg;
    public GameObject BRChoiceImg;
    public GameObject BLChoiceImg;

    public Text TChoiceTxt;
    public Text TRChoiceTxt;
    public Text TLChoiceTxt;
    public Text BChoiceTxt;
    public Text BRChoiceTxt;
    public Text BLChoiceTxt;

    private GameObject player;

    public bool inConv;

    private bool numpad;

    private bool isTChoice;
    private bool isTRChoice;
    private bool isTLChoice;
    private bool isBChoice;
    private bool isBRChoice;
    private bool isBLChoice;

    public bool TChosen;
    public bool TRChosen;
    public bool TLChosen;
    public bool BChosen;
    public bool BRChosen;
    public bool BLChosen;

    private float xdifference;
    private float ydifference;
    private float zdifference;

    private float difference;

    void Start()
    {
        sCText = FindObjectOfType(typeof(TextControlShopClerk1)) as TextControlShopClerk1;
        player = GameObject.FindGameObjectWithTag("Player");
        Reset();
        sCText.text.GetComponent<TextMesh>().text = "";
        print("ConvStart");
    }

    void Update()
    {
        if (inConv)
        {
            DifferenceCalc();

            if (difference > threshold)
            {
                sCText.text.SetActive(false);
                EndConv();
            }
            if (difference < threshold)
            {
                sCText.text.SetActive(true);
            }

            if (isTChoice)
            {
                if (Input.GetButton("TopChoiceNum"))
                {
                    ResetChosen();
                    DisablePicks();
                    TChosen = true;
                    numpad = true;
                }
            }
            if (isTRChoice)
            {
                if (Input.GetButton("TopRightChoiceNum"))
                {
                    ResetChosen();
                    DisablePicks();
                    TRChosen = true;
                    numpad = true;
                }
            }
            if (isTLChoice)
            {
                if (Input.GetButton("TopLeftChoiceNum"))
                {
                    ResetChosen();
                    DisablePicks();
                    TLChosen = true;
                    numpad = true;
                }
            }
            if (isBChoice)
            {
                if (Input.GetButton("BottomChoiceNum"))
                {
                    ResetChosen();
                    DisablePicks();
                    BChosen = true;
                    numpad = true;
                }
            }
            if (isBRChoice)
            {
                if (Input.GetButton("BottomRightChoiceNum"))
                {
                    ResetChosen();
                    DisablePicks();
                    BRChosen = true;
                    numpad = true;
                }
            }
            if (isBLChoice)
            {
                if (Input.GetButton("BottomLeftChoiceNum"))
                {
                    ResetChosen();
                    DisablePicks();
                    BLChosen = true;
                    numpad = true;
                }
            }

            if (Input.GetButton("TopChoice") && Input.GetButton("RightChoice") == false && Input.GetButton("LeftChoice") == false && Input.GetButton("BottomChoice") == false)
            {
                ResetChosen();
                TChosen = true;
            }
            if (Input.GetButton("TopChoice") && Input.GetButton("RightChoice") && Input.GetButton("LeftChoice") == false && Input.GetButton("BottomChoice") == false)
            {
                ResetChosen();
                TRChosen = true;
            }
            if (Input.GetButton("TopChoice") && Input.GetButton("RightChoice") == false && Input.GetButton("LeftChoice") && Input.GetButton("BottomChoice") == false)
            {
                ResetChosen();
                TLChosen = true;
            }
            if (Input.GetButton("TopChoice") == false && Input.GetButton("RightChoice") == false && Input.GetButton("LeftChoice") == false && Input.GetButton("BottomChoice"))
            {
                ResetChosen();
                BChosen = true;
            }
            if (Input.GetButton("TopChoice") == false && Input.GetButton("RightChoice") == false && Input.GetButton("LeftChoice") && Input.GetButton("BottomChoice"))
            {
                ResetChosen();
                BLChosen = true;
            }
            if (Input.GetButton("TopChoice") == false && Input.GetButton("RightChoice") && Input.GetButton("LeftChoice") == false && Input.GetButton("BottomChoice"))
            {
                ResetChosen();
                BRChosen = true;
            }

            if (TChosen)
            {
                DisablePicks();
                TChoiceImg.SetActive(true);
            }
            if (TLChosen)
            {
                DisablePicks();
                TLChoiceImg.SetActive(true);
            }
            if (TRChosen)
            {
                DisablePicks();
                TRChoiceImg.SetActive(true);
            }
            if (BChosen)
            {
                DisablePicks();
                BChoiceImg.SetActive(true);
            }
            if (BLChosen)
            {
                DisablePicks();
                BLChoiceImg.SetActive(true);
            }
            if (BRChosen)
            {
                DisablePicks();
                BRChoiceImg.SetActive(true);
            }

            if (Input.GetKey("Confirm") || numpad)
            {
                numpad = false;
                sCText.Conv();
                ResetChosen();
            }

        }
        else
        {
            DifferenceCalc();

            if (difference > threshold)
            {
                sCText.text.SetActive(false);
                sCText.text.GetComponent<TextMesh>().text = "";
            }
            if (difference < threshold)
            {
                sCText.text.SetActive(true);
                EndConv();
            }
        }
    }

    public void DifferenceCalc()
    {

        xdifference = transform.position.x - player.transform.position.x;
        ydifference = transform.position.y - player.transform.position.y;
        zdifference = transform.position.z - player.transform.position.z;

        if (xdifference < 0)
        {
            xdifference = xdifference * -1;
        }
        if (ydifference < 0)
        {
            ydifference = ydifference * -1;
        }
        if (zdifference < 0)
        {
            zdifference = zdifference * -1;
        }

        difference = Mathf.Sqrt((xdifference * xdifference) + (ydifference * ydifference) + (zdifference * zdifference));
    }

    public void EndConv()
    {
        inConv = false;
        DisableOptions();
    }

    public void DisablePicks()
    {
        TChoiceImg.SetActive(false);
        TRChoiceImg.SetActive(false);
        TLChoiceImg.SetActive(false);
        BChoiceImg.SetActive(false);
        BRChoiceImg.SetActive(false);
        BLChoiceImg.SetActive(false);
    }

    public void DisableOptions()
    {
        ResetOptions();
        wheel.SetActive(false);
        DisablePicks();
    }

    public void TChoice(bool active, string text)
    {
        TChoiceTxt.text = text;
        if (active)
        {
            isTChoice = true;
        }
        else
        {
            TChoiceTxt.text = "";
            isTChoice = false;
        }
    }

    public void TRChoice(bool active, string text)
    {
        TRChoiceTxt.text = text;
        if (active)
        {
            isTRChoice = true;
        }
        else
        {
            TRChoiceTxt.text = "";
            isTRChoice = false;
        }
    }

    public void TLChoice(bool active, string text)
    {
        TLChoiceTxt.text = text;
        if (active)
        {
            isTLChoice = true;
        }
        else
        {
            TLChoiceTxt.text = "";
            isTLChoice = false;
        }
    }

    public void BChoice(bool active, string text)
    {
        BChoiceTxt.text = text;
        if (active)
        {
            isBChoice = true;
        }
        else
        {
            BChoiceTxt.text = "";
            isBChoice = false;
        }
    }

    public void BRChoice(bool active, string text)
    {
        BRChoiceTxt.text = text;
        if (active)
        {
            isBRChoice = true;
        }
        else
        {
            BRChoiceTxt.text = "";
            isBRChoice = false;
        }
    }

    public void BLChoice(bool active, string text)
    {
        BLChoiceTxt.text = text;
        if (active)
        {
            isBLChoice = true;
        }
        else
        {
            BLChoiceTxt.text = "";
            isBLChoice = false;
        }
    }

    public void Reset()
    {
        DisableOptions();

        ConvReset();

        ResetChosen();

        isBChoice = false;
        isBRChoice = false;
        isBLChoice = false;
        isTChoice = false;
        isTRChoice = false;
        isTLChoice = false;

        numpad = false;
    }

    public void ConvReset()
    {
        sCText.ConvReset();
    }

    public void ResetChosen()
    {
        TChosen = false;
        TLChosen = false;
        TRChosen = false;
        BChosen = false;
        BRChosen = false;
        BLChosen = false;
    }

    public void ResetOptions()
    {
        TChoice(false, "");
        TRChoice(false, "");
        TLChoice(false, "");
        BChoice(false, "");
        BRChoice(false, "");
        BLChoice(false, "");
    }
}

and here is the code I use on the NPC:

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

public class TextControlShopClerk1: MonoBehaviour
{

    private ConversationSystem player;

    public GameObject text;

    public bool convVeryBeg;
    private bool convStart;
    private bool convFirstQuest;
    private bool convPriceshigh;
    private bool convWhoIAm;

    private void Start()
    {
        player = FindObjectOfType(typeof(ConversationSystem)) as ConversationSystem;
        print("TextControlStart");
    }

    public void StartConv()
    {
        player.Reset();
        text.SetActive(true);
        player.inConv = true;
        player.DisableOptions();
        player.wheel.SetActive(true);
        convVeryBeg = true;
        Conv();
    }

    public void Conv()
    {
        player.ResetOptions();
        if(convStart)
        {
            player.ResetOptions();
            if (convVeryBeg)
            {
                text.GetComponent<TextMesh>().text = "Hello, \n What can I do for you?";
                convVeryBeg = false;
            }
            player.BChoice(true, "Exit");
            player.TChoice(true, "Quest?");
            player.TLChoice(true, "Depends on what you have");
            player.TRChoice(true, "Like to buy");
            player.BLChoice(true, "Prices too high!");

            if (player.BChosen)
            {
                text.GetComponent<TextMesh>().text = "Have a nice day.";
                player.Reset();
                player.EndConv();
            }
            if (player.TChosen)
            {
                text.GetComponent<TextMesh>().text = "Actually, \n there is something you could help with.";
                convFirstQuest = true;
                convStart = false;
            }
            if (player.TLChosen)
            {
                Shop();
            }
            if (player.TRChosen)
            {
                Shop();
            }
            if (player.BLChosen)
            {
                text.GetComponent<TextMesh>().text = "Then go buy somewhere else";
                convPriceshigh = true;
                convStart = false;
            }

            player.ResetChosen();
        }

        if (convPriceshigh)
        {
            player.ResetOptions();
            player.BChoice(true, "Alright!");
            player.TRChoice(true, "Was worth it");
            player.TChoice(true, "Know who I am?");

            if (player.BChosen)
            {
                text.GetComponent<TextMesh>().text = "OK, bye.";
                player.Reset();
                player.EndConv();
            }
            if (player.TChosen)
            {
                text.GetComponent<TextMesh>().text = "No... \n No, I don't.";
                convPriceshigh = false;
                convWhoIAm = true;
            }
            if (player.TRChosen)
            {
                text.GetComponent<TextMesh>().text = "Was it? \n Really?";
                convPriceshigh = false;
                convStart = true;
                Conv();
                player.ResetChosen();
            }

            player.ResetChosen();
        }

        if (convWhoIAm)
        {
            player.ResetOptions();
            player.TChoice(true, "I am...");
            player.BChoice(true, "I don't have to do this");
            player.TRChoice(true, "OK");

            if (player.TChosen)
            {
                text.GetComponent<TextMesh>().text = "Not important.";
                convWhoIAm = false;
                convStart = true;
                player.ResetChosen();
                Conv();
            }
            if (player.BChosen)
            {
                text.GetComponent<TextMesh>().text = "Works for me.";
                player.Reset();
                player.EndConv();
            }
            if (player.TRChosen)
            {
                text.GetComponent<TextMesh>().text = "Good.";
                convWhoIAm = false;
                convStart = true;
                player.ResetChosen();
                Conv();
            }

            player.ResetChosen();
        }
    }

    public void ConvReset()
    {
        convStart = true;
        convPriceshigh = false;
        convFirstQuest = false;
        convWhoIAm = false;
    }

    private void Shop()
    {

    }
}

So how do I change the first code so that it doesn’t only with the Shop Clerk, but also with all other NPC?

Ok, I think your code can be upgraded in number of ways…
First, you should have the player that handle most of the conversation, and the NPC that change it’s answers. To do so, i think you should create an interface for your NPC conversations, so the player can call directly specific methods in the conversation like : conv.answer1(); or something like this.
Next, having 3 series of 6 bools makes your code unreadable. You could have a dictionary to hold the direction of the choice as a key and the message as the value. for the direction, use an enum. Here’s the resource for the dictionary : Dictionary<TKey,TValue> Classe (System.Collections.Generic) | Microsoft Learn
I really would have loved to help you there, but I think the only way I could would be by starting from scratch.

Take a look at this for interfaces : interface keyword - C# reference | Microsoft Learn
and at this for enums : Enumeration types - C# reference | Microsoft Learn

I think you should look for a more generic way for the conv, since there, no one will be able to help you with so much bools

good luck with that :slight_smile:

1 Like

Thanks, I will look into it and hopefully come back in a couple of days with a more coherent code.

By the way, check out the switch comparisons, it can help you to clean up those series of if that can be hard to read :wink:

1 Like