Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting InkList variables #920

Open
AdrienLemaire opened this issue Dec 9, 2021 · 2 comments
Open

Setting InkList variables #920

AdrienLemaire opened this issue Dec 9, 2021 · 2 comments

Comments

@AdrienLemaire
Copy link

AdrienLemaire commented Dec 9, 2021

Describe the bug

To Reproduce

import { InkList, InkListItem } from "inkjs/engine/InkList";
import { ListValue } from "inkjs/engine/Value";

const myList = new InkList()
data.forEach((item) => {
  myList.Add(new InkListItem("myList", item.strName), item.intValue);
});

// Option 1
const value = ListValue.Create(myList);
ink.variablesState.SetGlobal("myList", value)

// Option 2
ink.variablesState.$("myList", myList);

Expected behavior

the ink story is updated with the global LIST variable myList and the new content added.

Environment

  • OS: Linux
  • Web browser version: Chrome
  • inkjs version: 2.0.0

Additional context

I'm trying to modify the initial value of a list in an ink story, and am failing to because of the error above. Wondering if this is due to the Value.Create internal call, when Value is an abstract class ?

I'm having trouble to handle this InkList to InkObject conversion. What would be the appropriate way to replace an InkList variable with new content ?

As a side note, the type of VariableState.$ should be updated to allow a single parameter for read access. Right now, I must call ink.variablesState.$("myList", undefined) if I want to read the content.

-    $(variableName: string, value: any): any;
+    $(variableName: string, value?: any): any;
@AdrienLemaire
Copy link
Author

AdrienLemaire commented Dec 9, 2021

After spending 5 hours debugging the code, I found a convoluted way to update a global list.

import { InkList, InkListItem } from "inkjs/engine/InkList";
import { ListValue } from "inkjs/engine/Value";

// 1. Create fake InkList
const myList = new InkList()
data.forEach((item) => {
  myList.Add(new InkListItem("myList", item.strName), item.intValue);
});

// 2. Retrieve a copy of the existing ListValue
const listValue = ink.variablesState.GetVariableWithName(
  "myList"
)! as ListValue;

// 3. Mutate the ListValue value with our new list
listValue.value = myList

// 4. Replace the global ListValue
ink.variablesState.SetGlobal("myList", listValue);

// 5. Get ink to use the new list when calling Continue
ink.variablesState.SnapshotDefaultGlobals();

@y-lohse
Copy link
Owner

y-lohse commented Feb 1, 2022

Hi! Thanks for reporting this and a workaround too. I haven't touched inkjs in a while and probably won't for some more time, so I have no idea what could cause this at this point. If you want to provide a PR to fix the issue though I'd be happy to assist :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants