Adding a custom Function onto the Naninovel

Posted: 06 Feb 2021 09:37
by noa1359

Hi there,
I wanted to use MoveTowards on my script on Naninovel so I could change my EXPbar gradually, unfortunately, I can only use Random and CalculateProgress
So I tried to add this function as you explained in the doc:

Code: Select all

[Naninovel.ExpressionFunctions]
public static class CustomFunctions
{
	public static float MoveTowards (float current, float target, float maxDelta) => Mathf.MoveTowards(current, target,maxDelta);
}

It doesn't get accepted as a function in the script and therefore I can't use it...
IDK what to do, please help me


Re: Adding a custom Function onto the Naninovel

Posted: 06 Feb 2021 10:51
by Elringus

Hi, Can you please clarify how you're using this function in scripts and what you expect to happen.


Re: Adding a custom Function onto the Naninovel

Posted: 06 Feb 2021 12:20
by noa1359
Elringus wrote: 06 Feb 2021 10:51

Hi, Can you please clarify how you're using this function in scripts and what you expect to happen.

Well I say that @set EXPbar(AKA the value of the bar)=MoveTowards(0, charaEXP,charaEXPamount)
charaEXP is the EXP the character currently has
and charaEXPamount is the EXP the character needs to level up!
What I expect to happen is for the bar to go gradually from 0 to the charaEXP it currently has BUT it can't pass the charaEXPamount
It says to me that that function doesn't exist though


Re: Adding a custom Function onto the Naninovel

Posted: 06 Feb 2021 13:18
by Elringus

Not sure why it's not recognizing the function. Check the inventory example project, there are custom functions used there and they're working fine, afaik. By the way, I'd suggest implementing the UI logic in C# (or visual scripting), as naninovel scripts are not really intended for that kind of stuff. You can set the exp custom variable in naninovel script and react accordingly (update the UI properties and run the required animations) in C#.


Re: Adding a custom Function onto the Naninovel

Posted: 06 Feb 2021 13:20
by noa1359
Elringus wrote: 06 Feb 2021 13:18

Not sure why it's not recognizing the function. Check the inventory example project, there are custom functions used there and they're working fine, afaik. By the way, I'd suggest implementing the UI logic in C# (or visual scripting), as naninovel scripts are not really intended for that kind of stuff. You can set the exp value custom variable in naninovel script and react accordingly (update the UI value with the required animations) in C#.

Thank you so much! I'm not very good with C# coding but I think I'll do that! :) Thank you for the help!! :)


Re: Adding a custom Function onto the Naninovel

Posted: 06 Feb 2021 14:24
by noa1359
Elringus wrote: 06 Feb 2021 13:18

Not sure why it's not recognizing the function. Check the inventory example project, there are custom functions used there and they're working fine, afaik. By the way, I'd suggest implementing the UI logic in C# (or visual scripting), as naninovel scripts are not really intended for that kind of stuff. You can set the exp custom variable in naninovel script and react accordingly (update the UI properties and run the required animations) in C#.

Code: Select all

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

public class EXPbaranimation : MonoBehaviour
{
    public Slider EXPbar;
    // Update is called once per frame
    void Update()
    {
        var variableManager = Engine.GetService<ICustomVariableManager>();
        variableManager.TryGetVariableValue<float>("charaEXP", out var charaEXP);
        variableManager.TryGetVariableValue<float>("charaEXPamount", out var charaEXPamount);
        EXPbar.maxValue = charaEXPamount;
        EXPbar.value = Mathf.MoveTowards(EXPbar.value, charaEXP, 10f * Time.deltaTime);
    }
}

I wrote this code yet I strangely got those 2 errors, I don't understand them at all :(
Image


Re: Adding a custom Function onto the Naninovel

Posted: 06 Feb 2021 15:39
by Elringus

Look for C# docs on null reference exceptions and how to deal with them.


Re: Adding a custom Function onto the Naninovel

Posted: 07 Jul 2021 00:26
by tr5414

Im getting the exact same errors trying to do the same thing. Did you make any progress?


Re: Adding a custom Function onto the Naninovel

Posted: 07 Jul 2021 09:45
by Elringus

Make sure the engine is initialized at the time you're accessing the APIs. https://naninovel.com/guide/integration ... engine-api