1
\$\begingroup\$
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;

public class ConversationTrigger : MonoBehaviour
{
    public List<Conversation> conversations = new List<Conversation>();
    public GameObject canvas;
    public bool conversationEnd = false;

    private bool isRunning = false;

    [HideInInspector]
    public static int conversationIndex;

    private DialogueManager dialoguemanager;

    private void Start()
    {
        conversationIndex = 0;
        dialoguemanager = FindObjectOfType<DialogueManager>();
    }

    List<int> conversationsToPlay = new List<int>();
    public List<int> StartConversations()
    {
        for (int i = 0; i < conversationsToPlay.Count; i++)
        {
            if (!isRunning)
            {
                StartCoroutine(PlayConversation(conversationsToPlay[i]));
            }
        }

        return conversationsToPlay;
    }

    public IEnumerator PlayConversation(int index)
    {
        isRunning = true;

        if (conversations.Count > 0 &&
            conversations[index].Dialogues.Count > 0)
        {
            for (int i = 0; i < conversations[index].Dialogues.Count; i++)
            {
                if (dialoguemanager != null)
                {
                    dialoguemanager.StartDialogue(conversations[index].Dialogues[i]);
                }

                while (DialogueManager.dialogueEnded == false)
                {
                    yield return null;
                }
            }

            conversationIndex = index;
            conversationEnd = true;
            canvas.SetActive(false);
            Debug.Log("Conversation Ended");
        }

        isRunning = false;
    }

    public void SaveConversations()
    {
        string jsonTransform = JsonHelper.ToJson(conversations.ToArray(), true);
        File.WriteAllText(@"d:\json.txt", jsonTransform);
    }

    public void LoadConversations()
    {
        string jsonTransform = File.ReadAllText(@"d:\json.txt");
        conversations.Clear();
        conversations.AddRange(JsonHelper.FromJson<Conversation>(jsonTransform));
    }
}

This is the loop part :

List<int> conversationsToPlay = new List<int>();
        public List<int> StartConversations()
        {
            for (int i = 0; i < conversationsToPlay.Count; i++)
            {
                if (!isRunning)
                {
                    StartCoroutine(PlayConversation(conversationsToPlay[i]));
                }
            }

            return conversationsToPlay;
        }

And this is he script where I'm adding the indexs :

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

public class NaviDialogue : MonoBehaviour
{
    public ObjectsManipulation op;
    public bool scaling = true;
    public Scaling scale;
    public ConversationTrigger conversationTrigger;

    private bool ended = false;
    private bool startConversation = false;

    private void Start()
    {
        scaling = true;
        op.canScale = false;
    }

    private void Update()
    {
        if (scaling == true && DOFControl.hasFinished == true)
        {
            DOFControl.hasFinished = false;
            scaling = false;
            op.Scaling();
            PlayerController.disablePlayerController = true;
            conversationTrigger.StartConversations().Add(0);
            conversationTrigger.StartConversations().Add(1);
            conversationTrigger.StartConversations().Add(2);
        }

The first problem is that it's adding one item then start the conversations. But I want it first to add all the items 0,1,2 and then only to StartConversations.

The second problem is that once it's playing the first conversation it does playing the conversation 0 when it finish the conversation nothing happened and I want it to start playing conversation 1.

The idea is to play conversation 0 after conversation 0 ended start conversation 1 when 1 ended start 2 until it played all the conversations in the list.

So this example :

conversationTrigger.StartConversations().Add(0);
conversationTrigger.StartConversations().Add(1);
conversationTrigger.StartConversations().Add(2);

Should work like : playing the first item when ended play the second item when ended play the last item.

\$\endgroup\$
2
  • \$\begingroup\$ Your StartConversations method is ill founded. What is that function supposed to even do? Right now it: (1) gets the conversations to play (2) starts playing them (3) returns the conversations to play so it can be modified. You then do that 3 times. \$\endgroup\$ Commented May 13, 2019 at 1:30
  • \$\begingroup\$ The StartConversations method should loop over all the indexs of conversations I want to play and play them one by one. When the first ended start the next one. But you right I messed that method all up. \$\endgroup\$
    – Daniel Lip
    Commented May 13, 2019 at 2:58

2 Answers 2

0
\$\begingroup\$

It should be a coroutine too:

public IEnumerator PlayConversations () {
    for (int i = 0; i < 5; i++) {
        yield return StartCoroutine (PlayConversation (_conversations[i]));
    }
}

Call it:

StartCoroutine (PlayConversations ());

And it will call 5 PlayConversation coroutines one after another.


Updated: It turns out that yield return StartCoroutine (SomeCoroutine ()); can be simplified to yield return SomeCoroutine (); in coroutines.

\$\endgroup\$
0
\$\begingroup\$

This sounds a like a situation where it might be useful to use an eventsystem/callbacks/delegates. Do some research on those keywords and see, if something like that fits your project better. It might be a little more work at the beginning, because you might need to learn something new, but I think it's the better way to go than to fix a problem, which you really don't need to solve right now.

\$\endgroup\$
1
  • \$\begingroup\$ This answer would be even better if it offered a sketch of a strategy for how events / callbacks / delegates could be used to improve this implementation. \$\endgroup\$
    – DMGregory
    Commented Jun 7, 2019 at 12:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .