GetPortraitSprite() by image name

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
rakkar
Posts: 51
Joined: Mon Jan 13, 2020 10:39 pm

GetPortraitSprite() by image name

Post by rakkar »

In Actor.cs GetPortraitSprite() it picks a sprite either by pic=index or by DialogueManager.LoadAsset. However, it is reasonable to also check if the sprite was assigned in the editor rather than Resources. Using Resources is a bad idea in general because it causes that asset to load when the game starts and remains in memory forever. Picking by filename contains more information and is less likely to mess up than picking by index. I propose the following improvement:

Code: Select all

public Sprite GetPortraitSprite()
{
	//--- Was: return UITools.GetSprite(portrait, spritePortrait);
	//--- Instead, check for override set by SetPortrait():
	var originalDebugLevel = DialogueDebug.level; // Suppress logging for Lua return Actor[].Current_Portrait.
	DialogueDebug.level = DialogueDebug.DebugLevel.Warning;
	string imageName = DialogueLua.GetActorField(Name, DialogueSystemFields.CurrentPortrait).asString;
	DialogueDebug.level = originalDebugLevel;
	if (string.IsNullOrEmpty(imageName))
	{
		return GetPortraitSprite(1);
	}
	else if (imageName.StartsWith("pic="))
	{
		return GetPortraitSprite(Tools.StringToInt(imageName.Substring("pic=".Length)));
	}
	else
	{
		// KevinJ:
		for (int index = 0; index < alternatePortraits.Count; index++)
		{
			if (alternatePortraits[index] != null && string.Equals(alternatePortraits[index].name, imageName))
			{
				return UITools.GetSprite(alternatePortraits[index], null);
			}
		}
		for (int index = 0; index < spritePortraits.Count; index++)
		{
			if (spritePortraits[index] != null && string.Equals(spritePortraits[index].name, imageName))
			{
				return UITools.GetSprite(null, spritePortraits[index]);
			}
		}

		return UITools.CreateSprite(DialogueManager.LoadAsset(imageName) as Texture2D);
	}
}
User avatar
Tony Li
Posts: 22051
Joined: Thu Jul 18, 2013 1:27 pm

Re: GetPortraitSprite() by image name

Post by Tony Li »

Hi,

That's a reasonable addition. I'll add it.

However, out of curiosity, why wouldn't you just use pic=index if the image is already assigned to the portrait list in the editor?

Also, DialogueManager.LoadAsset can also load from assetbundles and addressables, not just Resources.
rakkar
Posts: 51
Joined: Mon Jan 13, 2020 10:39 pm

Re: GetPortraitSprite() by image name

Post by rakkar »

"Why wouldn't you just use pic=index if the image is already assigned to the portrait list in the editor?"

For a large team that is an error prone approach. An artist updating the images wouldn't know which order is referenced by what. It's easy to tell them don't change the filename.
User avatar
Tony Li
Posts: 22051
Joined: Thu Jul 18, 2013 1:27 pm

Re: GetPortraitSprite() by image name

Post by Tony Li »

Makes sense. It'll be in the next release.
Post Reply