Log In  

Cart #25268 | 2016-07-14 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA

Hey folks! This is a demo cart to showcase use of coroutines to create animations and dialogue.
By having a single active 'script' coroutine we can do the following:

  • reveal text frame by frame
  • do nothing until player presses [x]
  • give the player a choice of answers [up/down + x] to select
  • do nothing until npc has finished walking
  • do several of the above simultaneously, and only continue once all of them are finished.

all this without blocking the _update() function

For more info on coroutines, check out the forum post. Also keep an eye out for Fanzine #5, where Dan Sanderson will be exploring these topics!

P#25342 2016-07-15 13:00 ( Edited 2016-07-17 23:38)

Great stuff!

For a quick text-skip button, add an IF to REVEAL_TEXT()

[i]function reveal_text(str)
    text = ""
    for i=1, #str do
        text = text..sub(str,i,i)[/i]
        [b]if (btnp(4)) text=str break[/b]

For a text speed option, add a variable and use it as the for loop increment / sub() length value.

Heck, just adding opening and closing exposition you could turn this into any number of jokes/epics/deep thoughts. This makes me want to declare a one-screen RPG jam using this cart as a base. Anyone interested?

P#25351 2016-07-15 13:58 ( Edited 2016-07-15 18:04)

Nice work!

P#25359 2016-07-15 15:52 ( Edited 2016-07-15 19:52)

Sweet! This might come in handy for a project or two of mine so I'll save it for later.

P#25361 2016-07-15 17:08 ( Edited 2016-07-15 21:08)

Nice system, seems pretty clear to use. I didn't knew coroutines at all, so thanks for sharing that.

P#25363 2016-07-15 17:24 ( Edited 2016-07-15 21:24)

Sometimes I don't have much to add except how to save tokens, but hey, gotta go with what I know. You can eliminate the string concatenation like this:

[i]function reveal_text(str)
  [s]text = ""[/s]
  for i=1, #str do
    text = [s]text..sub(str,i,i)[/s][/i] [b]sub(str,1,i)[/b][i]
    if (btnp(4)) text=str break

I think the previous code was the sort that's better in C/C++, but worse in Lua. This is worse in C++, but better in Lua, due to the fact that Lua never modifies strings, preferring only to copy parts of old ones into new ones, discarding the old ones afterwards.

(Personally, I have found this to be a tough paradigm shift to accept.)

P#25382 2016-07-16 00:22 ( Edited 2016-07-16 04:23)

Aha, thanks for pointing that out! Repeated string concatenation is definitely not a desirable thing as far as performance goes (though I guess it's not a huge issue in this case since the code only runs once per frame), but yeah this way is definitely better for saving tokens and being a more elegant use of the available functions.

P#25470 2016-07-17 19:38 ( Edited 2016-07-17 23:38)

[Please log in to post a comment]