YarnSpinnerTool / YarnSpinner

Yarn Spinner is a tool for building interactive dialogue in games!
https://yarnspinner.dev
MIT License
2.3k stars 201 forks source link

When are functions evaluated? #356

Closed Eren121 closed 1 year ago

Eren121 commented 1 year ago

I have several questions regarding the timing of evaluation of the functions, especially. In the documentation, it's written that functions should be pure because their result can be cached.

So what I would like to do is a function hasItem("Name") to check if the player has a given item, reading the C# values. However, this make the function non pure. Is it ok to do so?

Additionally, is it written anywhere when the functions / if / set are evaluated? (begining of dialog, or line, or something else)

Because if for example, during the dialog, the item is added to the inventory of the player, the hasItem function should not be cached otherwise the result will be wrong.

desplesda commented 1 year ago

Currently (Yarn Spinner v2.2.4), functions are evaluated when the expression they're a part of is evaluated (for example, when an if statement is hit, or when a line with embedded expressions (my name is {$name}) is run.

We strongly encourage functions to be pure as much as possible, because it makes offline analysis significantly easier. You're totally fine to implement inventory-checking methods as non-pure; the tradeoff is that some upcoming (in-development) analysis features may have a harder time giving insights into your code.