Get Actor Portrait

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
mrklepper
Posts: 11
Joined: Mon Mar 14, 2022 12:07 pm

Get Actor Portrait

Post by mrklepper »

Hi

I am trying to get the actor portrait of a (future) conversation, like so:

DialogueManager.masterDatabase.GetActor(conversation.GetDialogueEntry(1).ActorID).GetPortraitSprite(1).texture)

However, it returns a NullReference Exception.

I have assigned the Portrait to the actor object both as a sprite and texture2d.

The conversation is the right one, as other data loads correctly, such as subtitle.

What am I doing wrong?

Thank you!
User avatar
Tony Li
Posts: 22145
Joined: Thu Jul 18, 2013 1:27 pm

Re: Get Actor Portrait

Post by Tony Li »

Hi,

Let's break it down to make it easier to identify which part is causing the NullReferenceException.

First, if you want to get the portrait of the conversation's Conversant (typically the NPC), you can just do this:

Code: Select all

Actor conversant = DialogueManager.masterDatabase.GetActor(conversation.ConversantID);
if (conversant != null)
{
    Sprite sprite = conversant.GetPortraitSprite();
}
If you instead want to get the portrait of whoever speaks the first node of the conversation (i.e., the node linked from <START>), try this:

Code: Select all

DialogueEntry startEntry = conversation.GetFirstDialogueEntry();
if (startEntry.outgoingLinks.Count > 0)
{
    DialogueEntry firstSpokenEntry = conversation.GetDialogueEntry(startEntry.outgoingLinks[0].destinationDialogueID);
    if (firstSpokenEntry != null)
    {
        Actor speaker = DialogueManager.masterDatabase.GetActor(firstSpokenEntry.ActorID);
        if (speaker != null)
        {
            Sprite sprite = speaker.GetPortraitSprite();
        }
    }
}
Post Reply