Tinting Layer of Layered Character

Posted: 25 Jun 2022 00:11
by moonrise

I created a basic code to change the color of my characters eyes. It changes based of a variable to different colors. However, it doesn't update unless I rollback and sometimes the colors aren't correct or even changed.

My current set up is this. In Unity on the layered character I have attached the script as a component to the eyes themselves. When the game runs there is no change to the color of the eyes. The game uses the default color set in UNity. However, when the character is called. It logs that the eyes have changed to the appropriate value. When I roll-back the eyes update, but are either white or a bright yellow. No where near the colors I choose in the script and far from it.

This is not what I want. I want the eyes to change colors when I call my other function UpdateTemperment. When I tried to add it there, I got an error saying "Assets\Scripts\CScripts\Check.cs(56,9): error CS0120: An object reference is required for the non-static field, method, or property 'EyeColor.UpdateEyes()'" No idea how to fix that.

So I just went with a Start() temporarily. It doesn't change the eye color unless you roll back and even then the colors are not correct....How would I remedy this?

Am I doing this properly?

Code: Select all

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Naninovel;

[Naninovel.ExpressionFunctions]
public class EyeColor : MonoBehaviour
{
    SpriteRenderer m_SpriteRenderer;

// Start is called before the first frame update
void Start()
{
    var variableManager = Engine.GetService<ICustomVariableManager>();
    string active_temper = variableManager.GetVariableValue("temper");

    if (active_temper == "brash")
    {
        // Dark Blue Eyes
        m_SpriteRenderer = GetComponent<SpriteRenderer>();
        m_SpriteRenderer.color = new Color(34,96,169,255);
    }
    else if (active_temper == "shy")
    {
        //Green brown eyes.
        m_SpriteRenderer = GetComponent<SpriteRenderer>();
        m_SpriteRenderer.color = new Color(59,78,0,255);
    }
    else
    {
        //Teal blue eyes.
        m_SpriteRenderer = GetComponent<SpriteRenderer>();
        m_SpriteRenderer.color = new Color(35,156,167,255);
    }
    Debug.Log("From Start() I updated the eyes to " + active_temper);
}

void UpdateEyes()
{
    var variableManager = Engine.GetService<ICustomVariableManager>();
    string active_temper = variableManager.GetVariableValue("temper");

    if (active_temper == "brash")
    {
        // Dark Blue Eyes
        m_SpriteRenderer = GetComponent<SpriteRenderer>();
        m_SpriteRenderer.color = new Color(34,96,169,255);
    }
    else if (active_temper == "shy")
    {
        //Green brown eyes.
        m_SpriteRenderer = GetComponent<SpriteRenderer>();
        m_SpriteRenderer.color = new Color(59,78,0,255);
    }
    else
    {
        //Teal blue eyes.
        m_SpriteRenderer = GetComponent<SpriteRenderer>();
        m_SpriteRenderer.color = new Color(35,156,167,255);
    }
    Debug.Log("From UpdateEyes: The eyes are " + active_temper);
}
}

Re: Tinting Layer of Layered Character

Posted: 25 Jun 2022 00:42
by Elringus

If you want something to work with rollback you'll need to use a state; check the article for more info: https://naninovel.com/guide/state-manag ... stom-state


Re: Tinting Layer of Layered Character

Posted: 25 Jun 2022 00:46
by Elringus

I'd also suggest learning basic C# and Unity before attempting to implement this.


Re: Tinting Layer of Layered Character

Posted: 26 Jun 2022 04:06
by moonrise
Elringus wrote: 25 Jun 2022 00:46

I'd also suggest learning basic C# and Unity before attempting to implement this.

Yeah, I'm more used to python, and have been trial and error learning the differences. Are you suggesting that I can't just change variables and see them reflected on the sprite? I have to do something else?


Re: Tinting Layer of Layered Character

Posted: 26 Jun 2022 13:26
by Elringus

It's possible. If you want to perform something when naninovel's custom variable changes, check out OnVariableUpdated event of ICustomVariableManager engine service.


Re: Tinting Layer of Layered Character

Posted: 27 Jun 2022 14:12
by moonrise

So, after re-reading the unity API. I realized wasn't using the proper Color to change the color variable. I had to change it to Color32 to be able to input the values. So the color of the eyes are now correct at least.

Where can I find more information on OnVariableUpdated to implement this? Is that something I attach to the object in Unity?

Code fix if anyone was curious:

Code: Select all

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Naninovel;


[Naninovel.ExpressionFunctions]
public class EyeColor : MonoBehaviour
{

public void SetEyeColor()
{
    SpriteRenderer spriteRenderer = GetComponent<SpriteRenderer>();
    var variableManager = Engine.GetService<ICustomVariableManager>();
    string active_temper = variableManager.GetVariableValue("temper");

    var brash_eyes = Color.blue;//(124,17,67,255); //a, rgb
    var shy_eyes = new Color32(107,60,21,255);
    var kind_eyes = new Color32(18,121,131,255);

    if (active_temper == "brash")
    {
        // Dark Blue Eyes
        spriteRenderer.color = brash_eyes;
    }
    else if (active_temper == "shy")
    {
        //Green brown eyes.
        spriteRenderer.color = shy_eyes;
    }
    else
    {
        //Teal blue eyes.
        spriteRenderer.color = kind_eyes;
    }
}
}

Re: Tinting Layer of Layered Character

Posted: 27 Jun 2022 17:55
by idaot

It's an event which you can subscribe to (https://www.youtube.com/watch?v=k4JlFxPcqlg) You can find examples of OnVariableUpdated in action by searching through the codebase.