Page 1 of 1

Connect custom scriptable character to dialogue system

Posted: Mon May 22, 2023 4:57 pm
by Utkan
I have a scriptable character script that contains information such as the character's name, sprite, and other details. My goal is to connect this script with the dialogue system so that I can access the appropriate character when someone is talking.

I came across this code snippet:

Code: Select all

[ActorPopup(true)] public string actor;
It allowed me to choose an actor from the character scriptable object. However, I'm unsure about how to proceed from this point.

One possible solution is to create an additional script that manages all the different characters. When someone is talking, I can retrieve the actor's name and compare it with the list of characters to determine who it is. While this approach works, I believe there might be a more efficient way to accomplish this.

Re: Connect custom scriptable character to dialogue system

Posted: Mon May 22, 2023 7:33 pm
by Tony Li
Hi,

What you describe should be fine. Your other script could maintain a dictionary of characters for fast lookup. For example, if your scriptable character script is:

Code: Select all

public class ScriptableCharacter : ScriptableObject
{
    [ActorPopup(true)] public string actorName;
    public Sprite sprite;
    //...etc...
}
Then you could have another script like this:

Code: Select all

public class ScriptableCharacterList : ScriptableObject
{
    public List<ScriptableCharacter> characters; // Assign ScriptableCharacter assets in inspector.
    
    private Dictionary<string, ScriptableCharacter> dict = null;
    
    public ScriptableCharacter GetScriptableCharacter(string actorName)
    {
        if (dict == null) // Set up dictionary
        {
            dict = new Dictionary<string, ScriptableCharacter>();
            characters.ForEach(c => dict.Add(c.actorName, c));
        }
        ScriptableCharacter c;
        if (dict.TryGetValue(actorName, out c))
        {
            return c;
        }
        else
        {
            Debug.LogError($"No ScriptableCharacter named {actorName} is in the list.");
            return null;
        }
    }
}
Then you can just call the ScriptableCharacterList's GetScriptableCharacter() method to quickly get a reference to the ScriptableCharacter asset.