Page 1 of 1

How to access C# variables in Conditions

Posted: Mon Dec 14, 2015 10:16 am
by Tony Li
This is a reply to DustVoltrage's YouTube post on the Dialogue System for Unity - Quests tutorial video:
Is there any way to access conditions with C#instead of lua ??

...how can I get the value of an array through it ?

I've got a skill tree (which is an array) : I need to check the int of the the first element (my array is in another gameobject).
How can I do that through the RegisterFunction ?
Here's an example: Let's say your skill tree is in a script named SkillTree.cs that's on a GameObject named PlayerSkills:

Code: Select all

public class SkillTree : MonoBehaviour {
    public int[] skills;
}
Add this script (SkillLuaFunctions.cs) to your Dialogue Manager:

Code: Select all

using UnityEngine;
using PixelCrushers.DialogueSystem;

public class SkullLuaFunctions : MonoBehaviour {

    void OnEnable() {
        Lua.RegisterFunction("GetSkillValue", this, typeof(SkillLuaFunctions).GetMethod("GetSkillValue"));
    }

    void OnDisable() {
        Lua.UnregisterFunction("GetSkillValue");
    }

    public double GetSkillValue(string gameObjectName, double arrayIndex) {
        var go = GameObject.Find(gameObjectName);
        if (go != null) {
            var skillTree = go.GetComponent<SkillTree>();
            if (skillTree != null) {
                return skillTree.skills[(int) arrayIndex];
            }
        }
        Debug.LogError("Can't find SkillTree script on GameObject " + gameObjectName);
        return 0;
    }
}
This essentially gives the Dialogue System direct access to a C# function.

If you want to check if a skill value is greater than 5 in a conversation, set the Conditions field to:

Code: Select all

GetSkillValue("PlayerSkills", 0) > 5
If you want to check the skill value in a trigger such as Quest Trigger, set Condition > Lua Conditions to the same as above.

If you need further help, please feel free to reply to this post or send an example project to tony (at) pixelcrushers.com.