Page 1 of 3

Trouble displaying a Variable

Posted: Mon Feb 09, 2015 5:14 am
by slapdashash
Hi,



I need to display a global integer variable (called 'scale') onscreen while I test my conversation system. Each dialogue choice made either adds or subtracts to/from this global variable (using the script field in each Dialogue Entry - "Variable[scale] = Variable[scale] + 1"). I need it to update as it goes through the conversation so I can keep an eye on the value, but I'm having trouble making Unity UI display this data.



It's worth noting that I'm also using Adventure Creator, but have turned it off to use Dialogue System for conversations. So I've made sure to create the 'scale' variable is within Dialogue System and AC.



How can I display the variable onscreen?



(I've read through the doc about alerts, quest tracking and syncing AC data with Dialogue Systems and I'm still very confused.)



Trouble displaying a Variable

Posted: Mon Feb 09, 2015 5:25 am
by Tony Li
Hi,



Add a C# script like this to your scene:

using UnityEngine;

using System.Collections;

using PixelCrushers.DialogueSystem;



public class WatchScale : MonoBehaviour {



public UnityEngine.UI.Text scaleLabel; //<--Assign this.



void Start() {

DialogueManager.AddLuaObserver("Variable[scale]", LuaWatchFrequency.EveryDialogueEntry, OnScaleChanged);

}



void OnScaleChanged(LuaWatchItem luaWatchItem, Lua.Result newValue) {

scaleLabel.text = newValue.AsString;

}



}



At the end of every dialogue entry, it will check the value of Variable[scale]. If it changed, it will call OnScaleChanged().

Trouble displaying a Variable

Posted: Mon Feb 09, 2015 5:30 am
by slapdashash
Ok great.



How does that get displayed through the UI exactly? still not understanding how the Dialogue Database file communicates with the GUI skin / Dialogue Manager to display things.



Also, can I pass that variable through to AC's variable system at the end of every dialogue entry instead? Mainly because I understand how to use AC more than Dialogue System at this point :)



Trouble displaying a Variable

Posted: Mon Feb 09, 2015 5:55 am
by Tony Li
The script above assumes that you've set up a Unity UI "Text" element and assigned it to "scaleLabel". If you're using legacy Unity GUI instead:

using System.Collections;

using PixelCrushers.DialogueSystem;

using PixelCrushers.DialogueSystem.UnityGUI;



public class WatchScale : MonoBehaviour {



public GUILabel scaleLabel; //<--Assign this.



void Start() {

DialogueManager.AddLuaObserver("Variable['scale']", LuaWatchFrequency.EveryDialogueEntry, OnScaleChanged);

}



void OnScaleChanged(LuaWatchItem luaWatchItem, Lua.Result newValue) {

scaleLabel.SetUnformattedText(newValue.AsString);

var bridge = DialogueManager.Instance.GetComponent<AdventureBridge>();

bridge.SyncLuaToVariables();

}



void OnConversationStart(Transform actor) {

var value = Lua.Run("return Variable['scale']");

scaleLabel.SetUnformattedText(value.AsString);

}



}

The code above also calls the bridge script's SyncLuaToVariables() method. This updates AC's variable system with the current values in the Dialogue System's Lua environment.

Trouble displaying a Variable

Posted: Mon Feb 09, 2015 6:14 am
by slapdashash
Sorry to be a pain, but I'm fairly new to Unity...



I not exactly sure how to add this C# script to my scene. Where does it sit exactly?



To get something to display I've copied an Alert gameobject (with a GUILabel script) from a UI prefab and have included that into my Dialogue UI tree, and it's displaying onscreen. Am I on the right track?

Trouble displaying a Variable

Posted: Mon Feb 09, 2015 7:18 am
by Tony Li
3844 wrote: To get something to display I’ve copied an Alert gameobject (with a GUILabel script) from a UI prefab and have included that into my Dialogue UI tree, and it’s displaying onscreen. Am I on the right track?


Yes, that should work fine. Steps to add the script:



1. Save the script in my second reply as WatchScale.cs.



2. Add the script to the Dialogue Manager GameObject.



3. Locate the GUILabel GameObject that you copied, and assign it to the script's "Scale Label" field.



I'm going to edit the script in my previous reply to also update the label when the conversation starts.

Trouble displaying a Variable

Posted: Mon Feb 09, 2015 7:50 am
by slapdashash
Getting this error:



"Can't add script - Can't add component 'WatchScale' because it doesn't exist."

Trouble displaying a Variable

Posted: Mon Feb 09, 2015 8:00 am
by slapdashash
Also, to follow up on the AC stuff...



While you've been helping me (THANK YOU!) I've realised I'll also need to trigger various NPC, and sometimes PC, animations throughout the conversation. I'd really like to return control to AC throughout a conversation whenever possible to do just that.



Is this possible?



If not are there any other sequencer commands besides AC() and ACCam() that I could use to directly communicate with AC?

Trouble displaying a Variable

Posted: Mon Feb 09, 2015 8:10 am
by Tony Li
For the “Can’t add script – Can’t add component ‘WatchScale’ because it doesn’t exist.”:



1. Is the file named exactly "WatchScale.cs"? The Project view won't show the file extension. If you see "WatchScale.cs" in the Project view, then the file itself is probably named "WatchScale.cs.cs". If this is the case, delete the extra ".cs".



2. Are there any compiler errors in the Console view?







For NPC and PC animation, AC() is supposed to be the one-stop method for communicating with AC. You can also use built-in Dialogue System sequencer commands such as Animation() [for legacy animation] and AnimatorPlay() [for Mecanim] without having to go to AC.

Trouble displaying a Variable

Posted: Tue Feb 10, 2015 12:09 am
by slapdashash
Here's the compile error:



"Assets/WatchScale.cs(5,27): error CS0246: The type or namespace name `MonoBehaviour' could not be found. Are you missing a using directive or an assembly reference?"



I checked the WatchScale.cs and it's named correctly, and sitting in my Assets folder (not sure if that matters).







I dived into the Sequencer commands last night and am very impressed with the level of control. Feeling more confident about using them now. Thanks.