Page 1 of 1

GetPortraitSprite() by image name

Posted: Tue Oct 20, 2020 9:57 pm
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);
	}
}

Re: GetPortraitSprite() by image name

Posted: Tue Oct 20, 2020 11:15 pm
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.

Re: GetPortraitSprite() by image name

Posted: Tue Oct 27, 2020 11:16 am
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.

Re: GetPortraitSprite() by image name

Posted: Tue Oct 27, 2020 11:29 am
by Tony Li
Makes sense. It'll be in the next release.