How to pass an icon through the dialogue system?

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
diccon
Posts: 27
Joined: Wed Sep 23, 2020 2:20 pm

How to pass an icon through the dialogue system?

Post by diccon »

I'm implementing a custom input panel to replace the Text Field UI for numerical inputs, and I'd like it to contain an icon for the item being transferred (see the image for what I mean). When I initialize the conversation I read form a list of components and push the names and quantities as variables, which are presented as Responses that the player selects. However I can't work out how to pass the icons through as well. The icons are stored as Sprite references in my component.
I see that there is a files Variable type but I can't find any documentation about how that is used so I'm not sure if that is the correct way, any help is much appreciated.

Image
User avatar
Tony Li
Posts: 22051
Joined: Thu Jul 18, 2013 1:27 pm

Re: How to pass an icon through the dialogue system?

Post by Tony Li »

Hi,

The Files type probably isn't what you're looking for. It just holds a text string. (Chat Mapper uses the Files type to manage file lists, but the Dialogue System doesn't.)

Instead, you can add a custom field that uses the Number or Text type. If you use a Number, you can reference the icon by its index number in your component's list. If you use a Text field, you can reference the icon by its name. Or you can create a custom field type. Custom field types are typically used to provide a custom dropdown selection menu.

In your custom input panel, look up the field in the current dialogue entry, which you can access via DialogueManager.currentConversationState.subtitle.dialogueEntry. For example, say it's a Text field named "Icon" that contains the name of a sprite in a Resources folder (to make the example simple). The relevant code might look something like this:

Code: Select all

public override void Open() // Overrides StandardUIInputField.Open.
{
    base.Open();
    var entry = DialogueManager.currentConversationState.subtitle.dialogueEntry;
    var iconName = Field.LookupValue(entry, "Icon");
    myImage.sprite = Resources.Load<Sprite>(iconName);
}
diccon
Posts: 27
Joined: Wed Sep 23, 2020 2:20 pm

Re: How to pass an icon through the dialogue system?

Post by diccon »

Thanks for your quick reply again, I've spent some time looking at these options and I think I'm actually going to try expanding the Item class to have a Sprite property (similar to Actor). That way I can maintain some kind of encapsulation and keep everything inside the DialogueSystem.

If you can think of any red flags for this approach please let me know otherwise I'll let you know how I get on..
User avatar
Tony Li
Posts: 22051
Joined: Thu Jul 18, 2013 1:27 pm

Re: How to pass an icon through the dialogue system?

Post by Tony Li »

Hi,

I recommend adding a custom field type to Items instead of directly modifying the Dialogue System's source code. This way you won't lose your modifications when you update the Dialogue System.

The drawer for your custom field type can populate a dropdown menu with the available item icons. You could get the list of icons from Resources, or by creating a ScriptableObject asset that references the available icons, or whatever other way you prefer. Your ScriptableObject asset could be as simple as this:

Code: Select all

[CreateAssetMenu]
public class IconList : ScriptableObject {
    public Sprite[] icons;
}
Your custom field type drawer can then look up the IconList asset via AssetDatabase.LoadAssetAtPath or whichever way you like.
diccon
Posts: 27
Joined: Wed Sep 23, 2020 2:20 pm

Re: How to pass an icon through the dialogue system?

Post by diccon »

Thanks for reply, even though I didn't do it the way you suggested I do appreciate it, I use source control so I'm can merge my changes into any new versions of the source.

I'd really love to be able to make a custom field that took an object reference, have you ever tried to implement that? It looks like the LuaInterpreter has a LuaUserData type so it should be possible to push an object into a table. If I get some free time I'll see if I can get it to work.
User avatar
Tony Li
Posts: 22051
Joined: Thu Jul 18, 2013 1:27 pm

Re: How to pass an icon through the dialogue system?

Post by Tony Li »

Since many devs import dialogue from external editors such articy:draft and Chat Mapper, I generally avoid direct Unity asset references in dialogue databases since those references would be broken when importing and exporting out of Unity.

The custom field type approach I described above, while a bit indirect to initially implement, at least maintains references when importing and exporting. I've seen projects that use it for item reward dropdowns in quest fields, background image dropdowns in location fields, etc.

But the source is yours to edit, so please have at it. If you come up with a clever solution for direct object references, please feel free to share it here if you care to.
Post Reply