1
0
Fork 0
mirror of https://github.com/Reuh/anselme.git synced 2025-10-27 16:49:31 +00:00

Discard returns values from choices

This commit is contained in:
Étienne Fildadut 2021-04-12 17:36:18 +02:00
parent f9edaff7e7
commit e454888b9f
4 changed files with 61 additions and 1 deletions

View file

@ -264,6 +264,25 @@ $ hey
{hey} = 5
```
Be careful when using `@` in a choice block. Choice blocks are not ran right as they are read, but at the next event flush (i.e. empty line). This means that if there is no flush in the function itself, the choice will be ran *after* the function has already been executed and returning a value at this point makes no sense:
```
$ f
> a
@1
@2
(f will return 2 since the choice is run after the @2 line)
~ f = 2
Yes.
(Choice block is actually ran right before the "Yes" line, when the choice event is flushed.)
```
For this reason, Anselme will discard returns values sent from within a choice block. Returns inside choice block still have the expected behaviour of stopping the execution of the block.
* empty line: flush events, i.e., if there are any pending lines of text or choices, send them to your game. See [Event buffer](#event-buffer). This line always keep the same identation as the last non-empty line, so you don't need to put invisible whitespace on an empty-looking line. Is also automatically added at the end of a file.
* regular text: write some text into the event buffer. Support [text interpolation](#text-interpolation).

View file

@ -143,7 +143,8 @@ local function run_line(state, line)
local v, e = run_block(state, choice.block)
tags:pop(state)
if e then return v, e end
if v then return v end
-- discard return value from choice block as the execution is delayed until an event flush
-- and we don't want to stop the execution of another function unexpectedly
end
end
end

View file

@ -0,0 +1,10 @@
$ f
> a
x
@1
y
@2
~ f = 2
~ choose(1)
Yes.

View file

@ -0,0 +1,30 @@
local _={}
_[13]={}
_[12]={}
_[11]={}
_[10]={data="Yes.",tags=_[13]}
_[9]={data="x",tags=_[12]}
_[8]={data="a",tags=_[11]}
_[7]={_[10]}
_[6]={_[9]}
_[5]={_[8]}
_[4]={"return"}
_[3]={"text",_[7]}
_[2]={"text",_[6]}
_[1]={"choice",_[5]}
return {_[1],_[2],_[3],_[4]}
--[[
{ "choice", { {
data = "a",
tags = {}
} } }
{ "text", { {
data = "x",
tags = {}
} } }
{ "text", { {
data = "Yes.",
tags = {}
} } }
{ "return" }
]]--