inkle / inkle/ink

Observe Variables (beginner question)

Open
#244 6 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
4.9k
Forks
540
PR merge metrics
No merged PRs in 30d

Description

Sorry for a probably very novice question, but I'm very new to both unity and ink.

I'm trying to learn by modifying the ink demos (The Intercept & the demos from the Unite Europe talk) and I'm getting along pretty well, especially because ink has been a real blessing for interactive fiction.

(Apologies in advance if my description of the issue is horrible, but I'm still learning all the correct terms)
Something I can't figure out for the life of me right now is how the Observe Variables function works.
I've added a test variable ("fullness") into the ink script that starts on 0 and then depending on your first branching choice it gets set to 1, later in the text 2, then 3.
I've added a Text object to the UI of The Intercept that is supposed to show the progress of that variable.
In it I've set up a variable, names "full" and an if statement that tells the text object that if the variable is 0, it puts out "- - -" in the UI. If it's 1, it puts out "I - -". If it's 3, it puts out "I I I" into the UI.
This works if I manually set variable (independently form the ink file so far). So that part works.

Then I've tried the variablesState, so that the variable is being set by the according variable in the ink file. I did this in the same script and I'm not sure if that's the right way to do it, since I had to load the full ink story file into the script to make it work. BUT once I added that link to the story in the beginning I was able to use variablesState and when I started the game it loaded up the correct output ("- - -", "I - -", "I I I"). But it didn't track the progress of the variable throughout the choices.

And I know that I need the ObserveVariable delegate function to make that work, but I just can't seem to get it to work no matter what I try.
I've tried to adapt this example:

```
void Update(){
int full = 0;

story.ObserveVariable("fullness", (string varName, object newValue) =>
{
full = (int)newValue;
});
}
```

I don't know what I'm doing wrong. Do I need to have the variable observed in another script and then just feed it into the text game object? Maybe the GameState script? Maybe just not in the "void Update()" part of that script?
Or does it not matter and I'm just messing this up in another way?

I've tried deleting the "int full = 0;" line and replaced it by the previous variablesState version:
int full = (int) story.variablesState["fullness"];
and this way it would at least read my first "VAR fullness = 0" in the ink story. But it still wouldn't update when the variable changed throughout the game.

Thanks for any help or pointers in advance. Even seeing a working example of the ObserveVariable for a UI command would be a huge help.

~Gabriela

PS:
This is the code, for reference:
```
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using Ink.Runtime;
using UnityEngine.UI;

using Debug = UnityEngine.Debug;

public class TextUI : MonoBehaviour {

public Text fullnessUI;
public int full;

public TextAsset storyJSON;
// The ink runtime story object
private Story story;

// Use this for initialization
void Start () {
}

// Update is called once per frame
void Update () {
alphaTween.Loop();
int full = (int) story.variablesState["fullness"];

story.ObserveVariable("fullness", (string varName, object newValue) =>
{
full = (int)newValue;
});

if (full == 1) {
fullnessUI.text = ("I - -").ToString ();
}
if (full == 2) {
fullnessUI.text = ("I I -").ToString ();
}
if (full == 3) {
fullnessUI.text = ("I I I").ToString ();
}
else {
fullnessUI.text = ("- - -︎").ToString ();
}
}

public FloatTween alphaTween = new FloatTween();

private CanvasGroup canvasGroup {
get {
return GetComponent();
}
}

private void Awake () {
alphaTween.OnChange += OnChangeAlphaTween;
}

void OnChangeAlphaTween (float currentValue) {
canvasGroup.alpha = currentValue;
}

public void FadeIn () {
alphaTween.Tween(0, 1, 5, AnimationCurve.EaseInOut(0,0,1,1));
}

public void Show () {
canvasGroup.alpha = 1;
}

public void Hide () {
alphaTween.Stop();
canvasGroup.alpha = 0;
}
}
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reading the TextUI script, especially its Start and Update methods, and trace where the Ink Story is created or loaded from storyJSON. Check the ObserveVariable usage against the variable updates made by the choices, then verify that the UI text reflects fullness changes during play.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, unity
Domain
game-dev
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.