MyFunction("string",int)

Using Naninovel C# APIs: adding custom actor implementations or commands, overriding engine services, integrating with other systems, etc.
Post Reply
moonrise
Posts: 8
Joined: 27 Oct 2021 23:05

MyFunction("string",int)

Post 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...   
}
idaot
support
Posts: 275
Joined: 01 Aug 2020 08:25

Re: MyFunction("string",int)

Post 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

moonrise
Posts: 8
Joined: 27 Oct 2021 23:05

Re: MyFunction("string",int)

Post 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

Post Reply