Log In  


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

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!

36


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]
		[i]yield()
	end
end[/i]

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?


Nice work!


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


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


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
    yield()
  end
end[/i]

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.)


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.



[Please log in to post a comment]