MyFunction("string",int)

Posted: 07 Jun 2022 20:24
by moonrise

So, I think I figured out most of the coding needs myself. The only thing I'm struggling is how do I call this function in naninovel?

Code: Select all

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

[Naninovel.ExpressionFunctions]
public static class Personality
{
    // ...deleted code fro readability... 

// Changes int of brash, shy, and kind variables defined in Custom Variables. Then update the temper to match new personality.

public void Nature(string nature, int points)
{
    var variableManager = Engine.GetService<ICustomVariableManager>();

    if(nature == "shy")
    {
        variableManager.TryGetVariableValue<int>("shy", out var intValue);
        int x = intValue + points;
        string new_int = x.ToString();
        variableManager.SetVariableValue("shy", new_int);
    }
    else if (nature == "kind")
    {
        variableManager.TryGetVariableValue<int>("kind", out var intValue);
        int x = intValue + points;
        string new_int = x.ToString();
        variableManager.SetVariableValue("kind", new_int);
    }
    else
    {
        variableManager.TryGetVariableValue<int>("brash", out var intValue);
        int x = intValue + points;
        string new_int = x.ToString();
        variableManager.SetVariableValue("brash", new_int);
    }

    UpdateNature();
}
// ...deleted code fro readability...   
}

Re: MyFunction("string",int)

Posted: 08 Jun 2022 07:24
by idaot

To call a void function in naniscript, use a custom command: https://naninovel.com/guide/custom-commands.html

If your function is meant to return a value to be printed in text or included in a calculation, use custom script expressions: https://naninovel.com/guide/script-expr ... -functions


Re: MyFunction("string",int)

Posted: 08 Jun 2022 18:09
by moonrise

You know I looked at the custom command and realized it was a little different then my personality one which was returning a bool based on the temperment. I ended up moving the code and using a custom command script.

Code: Select all

using Naninovel;
using Naninovel.Commands;
using UnityEngine;

[CommandAlias("add")]
public class CharNature : Command
{
    public StringParameter Temper;
    public IntegerParameter Points;

public override UniTask ExecuteAsync (AsyncToken asyncToken = default)
{
    var variableManager = Engine.GetService<ICustomVariableManager>();

    if (Assigned(Points)==false)
    {
        Points = 1;
    }

    if (Temper == "kind")
    {
        variableManager.TryGetVariableValue<int>("kind", out var intValue);
        int x = intValue + Points;
        string new_int = x.ToString();
        variableManager.SetVariableValue("kind", new_int);            
        Debug.Log($"I added {Points} {Temper}!");
    }
    else if(Temper == "shy")
    {
        variableManager.TryGetVariableValue<int>("shy", out var intValue);
        int x = intValue + Points;
        string new_int = x.ToString();
        variableManager.SetVariableValue("shy", new_int);            
        Debug.Log($"I added {Points} {Temper}!");
    }
    else if(Temper == "brash")
    {
        variableManager.TryGetVariableValue<int>("brash", out var intValue);
        int x = intValue + Points;
        string new_int = x.ToString();
        variableManager.SetVariableValue("brash", new_int);       
        Debug.Log($"I added {Points} {Temper}!");
    }
    else
    {
        Debug.Log($"That's not valid personality!");
    }
    Personality.UpdateNature();

    return UniTask.CompletedTask;
}
}

Does it still need "return UniTask.CompletedTask;" to function properly? Also can I make the params more straight forward such as

@add brash,1