Adding a custom Function onto the Naninovel

Using Naninovel C# APIs: adding custom actor implementations or commands, overriding engine services, integrating with other systems, etc.
Post Reply
noa1359
Posts: 8
Joined: 17 Jan 2021 10:20

Adding a custom Function onto the Naninovel

Post 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

Elringus
admin
Posts: 521
Joined: 11 May 2020 18:03

Re: Adding a custom Function onto the Naninovel

Post by Elringus »

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

noa1359
Posts: 8
Joined: 17 Jan 2021 10:20

Re: Adding a custom Function onto the Naninovel

Post 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

Elringus
admin
Posts: 521
Joined: 11 May 2020 18:03

Re: Adding a custom Function onto the Naninovel

Post 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#.

noa1359
Posts: 8
Joined: 17 Jan 2021 10:20

Re: Adding a custom Function onto the Naninovel

Post 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!! :)

noa1359
Posts: 8
Joined: 17 Jan 2021 10:20

Re: Adding a custom Function onto the Naninovel

Post 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

Elringus
admin
Posts: 521
Joined: 11 May 2020 18:03

Re: Adding a custom Function onto the Naninovel

Post by Elringus »

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

tr5414
Posts: 3
Joined: 03 Jul 2021 23:51

Re: Adding a custom Function onto the Naninovel

Post by tr5414 »

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

Elringus
admin
Posts: 521
Joined: 11 May 2020 18:03

Re: Adding a custom Function onto the Naninovel

Post by Elringus »

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

Post Reply