0

This class contain the Dialogues List:

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

[System.Serializable]
public class Conversation
{
    public string conversationName;
    public List<Dialogue> Dialogues = new List<Dialogue>();
}

And I want to be able to call and use the Dialogues in another scripts without making it static.

I need to use it in this script in 3 places:

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;

public class DialogueTrigger : MonoBehaviour
{
    public List<Conversation> conversations = new List<Conversation>();

    [HideInInspector]
    public int dialogueNum = 0;

    private bool triggered = false;
    private List<Dialogue> oldDialogue;

    public void TriggerDialogue()
    {
        if (triggered == false)
        {
            if (FindObjectOfType<DialogueManager>() != null)
            {
                FindObjectOfType<DialogueManager>().StartDialogue(Dialogues[dialogueNum]);
                dialogueNum += 1;
            }
            triggered = true;
        }
    }

    private void Update()
    {
        if (DialogueManager.dialogueEnded == true)
        {
            if (dialogueNum == Dialogues.Count)
            {
                return;
            }
            else
            {
                FindObjectOfType<DialogueManager>().StartDialogue(Dialogues[dialogueNum]);
                DialogueManager.dialogueEnded = false;
                dialogueNum += 1;
            }
        }
    }
}

And in this part of script:

private void EndDialogue()
    {
        dialogueEnded = true;
        if (trigger.dialogueNum == Dialogues.Count)
            canvas.SetActive(false);
        Debug.Log("End of conversation.");
    }

In all places I called it Dialogues but now it's not public static since I'm using it in a third script editor type script and it can't be static:

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;

[CustomEditor(typeof(DialogueTrigger))]
public class DialogueTriggerEditor : Editor
{
    private SerializedProperty _conversations;

    private void OnEnable()
    {
        _conversations = serializedObject.FindProperty("conversations");
    }

    public override void OnInspectorGUI()
    {
        //base.OnInspectorGUI();

        serializedObject.Update();

        if (_conversations.arraySize == 0)
            _conversations.ClearArray();

        _conversations.arraySize = EditorGUILayout.IntField("Conversations Size", _conversations.arraySize);

        for (int x = 0; x < _conversations.arraySize; x++)
        {
            var conversation = _conversations.GetArrayElementAtIndex(x);

            var conversationName = conversation.FindPropertyRelative("conversationName");

            EditorGUI.indentLevel++;
            EditorGUILayout.PropertyField(conversationName);

            EditorGUI.indentLevel++;
            var _dialogues = conversation.FindPropertyRelative("Dialogues");

            _dialogues.arraySize = EditorGUILayout.IntField("Dialogues size", _dialogues.arraySize);

            for (int i = 0; i < _dialogues.arraySize; i++)
            {
                var dialogue = _dialogues.GetArrayElementAtIndex(i);
                EditorGUI.indentLevel++;
                EditorGUILayout.PropertyField(dialogue, new GUIContent("Dialogue " + i), true);

                EditorGUI.indentLevel--;
            }

            if (_dialogues.arraySize > 0)
            {
                if (GUILayout.Button("Save Conversation"))
                {

                }
            }

            EditorGUI.indentLevel--;
            EditorGUI.indentLevel--;
        }

        serializedObject.ApplyModifiedProperties();
    }
}

And since _conversations is a SerializedProperty type It will not get the Dialogues if Dialogues will be static.

yochi le
  • 489
  • 9
  • 21
  • What exactly are you asking? Simply access the variable `dialogueTriggerReference.conversations` take e.g. the first and access it's `Dialogues` field: `dialogueTriggerReference.conversations[0].Dialogues` .. what is the problem? – derHugo Apr 06 '19 at 21:42
  • @derHugo Right this is working. – yochi le Apr 06 '19 at 22:09
  • You should use observer pattern. Read more [here](https://learn.microsoft.com/en-us/dotnet/standard/events/observer-design-pattern)
    Or use the event delegation. Read more [here](https://learn.microsoft.com/en-us/dotnet/standard/events/) Basically you will use a loose coupling relationship between classes and receive the `Dialogue` object in any of them on the time that you need it.
    – Ali Alp Apr 06 '19 at 20:47

0 Answers0