Best method to convert number variable to text variable

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
annathehank
Posts: 95
Joined: Sun May 03, 2020 2:17 pm

Best method to convert number variable to text variable

Post by annathehank »

Hi again! I promise one day I'll be able to go through a session without needing to ask a question :lol:

I have a points system set up in the variables that basically tracks how much another character likes you. It starts at 50 for neutral and you can gain or lose points as you interact with them.

I have a 'Quest' for each character the player can view that tells them some information about them and updates with new information. I currently have a line that displays the relationship in the points value as Relationship: 10 by using the Lua command [var=Charactername_Points].

I was wondering what the best method would be to convert that to be able to say Relationship: Like/hated/loved, etc.

Is there a way to easily do that through the variable editor in Dialogue system, such as creating a string variable with an if/then value?

Or do I need to have some kind of scrip that says if variable ["Charactername_Points"] >= 51 and variable ["Charactername_Points"] <= 75 set Variable["Relationship"] = Liked (etc) and attach that somewhere to the dialogue manager?

Thank you in advance <3
User avatar
Tony Li
Posts: 22047
Joined: Thu Jul 18, 2013 1:27 pm

Re: Best method to convert number variable to text variable

Post by Tony Li »

Hi,

In Dialogue Text and quest entry text, you can use the [var=variable] tag to show the value of a single variable, or the [lua(code)] tag to show the result of a Lua expression.

One solution is to register a C# method with Lua. That C# method could check the variable value and return an appropriate string. For example, say you register a C# method called GetFeeling(x):

Code: Select all

string GetFeeling(double x) // (C# methods that talk to Lua should use doubles for numbers)
{
    if (x <= 50) return "hated";
    else if (x >= 75) return "loved";
    else return "liked";
}
Then you can use that in your Dialogue Text:
  • Dialogue Text: Relationship: [lua(GetFeeling("Variable["Charactername_Points"]))]

---

Alternatively, you could skip all that and use this obscure bit of Lua syntax that we'll call a "ternary":

condition and true-value or false-value

For example, say a variable x is set to a random value 1-100. (Note that, to keep the code shorter and easier to read, I'm using a local Lua variable x and not a Lua variable in the Dialogue System table Variable["x"].) If x is less than 50, you want to return the string "heads". Otherwise return the string "tails". To do that, use this syntax:

Code: Select all

(x < 50) and "heads" or "tails"
You can nest one ternary inside another like this:

condition1 and condition-1-true-value or ( condition2 and condition-2-true-value or condition-2-false-value )

For example, let's say you want to return hated, liked, or loved based on the value of x:
  • hated: x <= 50
  • loved: x >= 75
  • liked: 51 <= x < 75
The expression could look like this:

Code: Select all

(x <= 50) and "hated" or ((x >= 75) and "loved" or "liked")
To use your Variable["Charactername_Points"] variable instead of x, it looks like:

Code: Select all

(Variable["Charactername_Points"] <= 50) and "hated" or ((Variable["Charactername_Points"] >= 75) and "loved" or "liked")

Here's an example:
  • Dialogue Text: Relationship: [lua((Variable["Charactername_Points"] <= 50) and "hated" or ((Variable["Charactername_Points"] >= 75) and "loved" or "liked"))]
annathehank
Posts: 95
Joined: Sun May 03, 2020 2:17 pm

Re: Best method to convert number variable to text variable

Post by annathehank »

Oh wow! That ternary system is really interesting!

If I did go with the C# script, would I just make sure that it's attached to my dialogue manager? Does it need to be attached to anything at all or just be in the project somewhere to work?
User avatar
Tony Li
Posts: 22047
Joined: Thu Jul 18, 2013 1:27 pm

Re: Best method to convert number variable to text variable

Post by Tony Li »

Yup, just put it on your Dialogue Manager. You can register the Lua function in Awake. The starter template script TemplateCustomLua.cs has an example that registers the function in OnEnable and unregisters it in OnDisable. But if it's on the Dialogue Manager you can get rid of all of that:

Code: Select all

using UnityEngine;
using PixelCrushers.DialogueSystem;

public class MyLuaFunctions : MonoBehaviour
{

    private void Awake()
    {
        Lua.RegisterFunction("GetFeeling", this, SymbolExtensions.GetMethodInfo(() => GetFeeling((double)0)));
    }
    
    public string GetFeeling(double x) // (C# methods that talk to Lua should use doubles for numbers)
    {
        if (x <= 50) return "hated";
        else if (x >= 75) return "loved";
        else return "liked";
    }
}
Also see Custom Lua Function Info Asset. It's something you can set up so you don't have to type the GetFeeling() function manually in your Conditions field. Instead, it adds the function to the "..." dropdown menus.
annathehank
Posts: 95
Joined: Sun May 03, 2020 2:17 pm

Re: Best method to convert number variable to text variable

Post by annathehank »

Excellent! Thank you so much <3
User avatar
Tony Li
Posts: 22047
Joined: Thu Jul 18, 2013 1:27 pm

Re: Best method to convert number variable to text variable

Post by Tony Li »

Happy to help!
Post Reply