Referencing a database through Self Made Script

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
fluffythekami
Posts: 12
Joined: Tue Dec 27, 2022 4:35 pm

Referencing a database through Self Made Script

Post by fluffythekami »

Hi there.

I'm trying to create a script that references a database so that I can use it special NPC interactions. The problem is I'm trying to get the conversation from that database, but I can never seem to find a suitable reference for that database. I've even tried adding the Extra Databases component to my Dialogue Manager, and made a coroutine to wait until the first frame of update to try and load those conversation, but it still seems to not be able to find the database.

Would you happen to know of a better way to reference this database to retrieve those conversations? Or maybe I'm going about it the wrong way?

Script for reference:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PixelCrushers.DialogueSystem;

public class RelationshipHandler : MonoBehaviour
{
public DialogueDatabase database;

[Header("Conversations")]
public List<string> ConversationNames;
List<Conversation> conversations;

public List<string> smallTalkNames;
List<Conversation> smallTalkConversations;

[Header("Relationship Stats")]
public int relationshipLevel;

void Start()
{
StartCoroutine(LoadConversations());
}

public void AddLevel()
{
//this happens at the end of the conversation to advance
relationshipLevel++;
}

public void CheckLevel()
{
//this would check the save data for that
}

private IEnumerator LoadConversations()
{
yield return new WaitForFixedUpdate();

for (int i = 0; i < ConversationNames.Count; i++)
{
conversations.Add(database.GetConversation(ConversationNames));
}

for (int i = 0; i < smallTalkNames.Count; i++)
{
smallTalkConversations.Add(database.GetConversation(smallTalkNames));
}
}

public void StartConversation()
{
DialogueManager.instance.StartConversation(ConversationNames[relationshipLevel], GameManager.instance.player.transform, this.gameObject.transform);
}

public void SmallTalkConversation()
{
DialogueManager.instance.StartConversation(ConversationNames[Random.Range(0, smallTalkConversations.Count)], GameManager.instance.player.transform, this.gameObject.transform);
}
}
User avatar
Tony Li
Posts: 21681
Joined: Thu Jul 18, 2013 1:27 pm

Re: Referencing a database through Self Made Script

Post by Tony Li »

Hi,

Unless I misunderstand you, I think you can omit most of that code. Wouldn't this suffice?

Code: Select all

using System.Collections.Generic;
using UnityEngine;
using PixelCrushers.DialogueSystem;

public class RelationshipHandler: MonoBehaviour {

    [Header("Conversations")]
    public List < string > ConversationNames;

    public List < string > smallTalkNames;

    [Header("Relationship Stats")]
    public int relationshipLevel;

    public void AddLevel() {
        //this happens at the end of the conversation to advance
        relationshipLevel++;
    }

    public void CheckLevel() {
        //this would check the save data for that
    }

    public void StartConversation() {
        DialogueManager.instance.StartConversation(ConversationNames[relationshipLevel], GameManager.instance.player.transform, this.gameObject.transform);
    }

    public void SmallTalkConversation() {
        DialogueManager.instance.StartConversation(smallTalkNames[Random.Range(0, smallTalkConversations.Count)], GameManager.instance.player.transform, this.gameObject.transform);
    }
}
In your original script, nothing uses the conversations and smallTalkConversations list.

If you ever do need a reference to the dialogue database, just use the DialogueManager.masterDatabase property.
fluffythekami
Posts: 12
Joined: Tue Dec 27, 2022 4:35 pm

Re: Referencing a database through Self Made Script

Post by fluffythekami »

I'm not entirely sure what I was thinking when I wrote that, but you've got me on the right path so thank you haha.

So now the issue I'm having is that when I tell the script to start a conversation using DialogueManager.instance.StartConversation(), it seems to ignore the string that I put in for the conversation i'm looking for, and always uses the first conversation entry of the master database no matter what string I put in. I guess that would be the conversation that has the ID of 1. Do you have any idea as to why that might happen?

Edit: I should add that the conversation that I want to pull comes from an extra database from the Extra Database Component, and not the initial database that the Dialogue Manager starts with.
User avatar
Tony Li
Posts: 21681
Joined: Thu Jul 18, 2013 1:27 pm

Re: Referencing a database through Self Made Script

Post by Tony Li »

Hi,

It doesn't matter which database the conversation is in, as long as the database has been loaded into memory by being assigned to the Dialogue Manager's Initial Database field, or an Extra Databases component, or C# method DialogueManager.AddDatabase(). All databases are "merged" in memory into DialogueManager.masterDatabase.

DialogueManager.StartConversation("title", actor, conversant) will always start the conversation title at the <START> node.

If you want to start a conversation at a specific dialogue entry, specify the entry ID. For example, to start at entry ID 42:

DialogueManager.StartConversation("title", actor, conversant, 42);
fluffythekami
Posts: 12
Joined: Tue Dec 27, 2022 4:35 pm

Re: Referencing a database through Self Made Script

Post by fluffythekami »

I see that now, with your last post, it sort of helped me narrow down the problem.

For whatever reason, StartConversation() is pulling from the master database, but almost seems like its playing the 2 conversations that share an ID, despite me inputting it by title. For example, database 1 has a conversation (with a different title but) with an ID of 5. database 2 also has a conversation with an ID of 5.

If I check both databases, both conversations are being played, however, it seems as though database 1 gets priority and is shown on the game screen and not database 2.



Hopefully that video explains it better.
fluffythekami
Posts: 12
Joined: Tue Dec 27, 2022 4:35 pm

Re: Referencing a database through Self Made Script

Post by fluffythekami »

I guess as an update post. I'm not entirely why what's going on in the video happens, but all I did was, through code, remove the initial database from the master database, start the conversation, and then add the initial database back in the next line.

Works for now but I'm still not sure why the other issue happens!
User avatar
Tony Li
Posts: 21681
Joined: Thu Jul 18, 2013 1:27 pm

Re: Referencing a database through Self Made Script

Post by Tony Li »

Hi,

Since both databases get merged into DialogueManager.masterDatabase at runtime, the databases should have unique internal IDs. In other words, if database A has a conversation with internal ID 5, then database B should not also have a conversation with internal ID 5. You can read more about this, including tips such as setting Base ID and using the Unique ID Tool, in Working With Multiple Databases.
fluffythekami
Posts: 12
Joined: Tue Dec 27, 2022 4:35 pm

Re: Referencing a database through Self Made Script

Post by fluffythekami »

Thanks again! I'll give that a look. Always appreciate your help.
User avatar
Tony Li
Posts: 21681
Joined: Thu Jul 18, 2013 1:27 pm

Re: Referencing a database through Self Made Script

Post by Tony Li »

Happy to help!
Post Reply