Weird coroutine issue

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
DumpOnIt
Prole
Posts: 30
Joined: Fri Apr 06, 2012 6:59 am

Weird coroutine issue

Post by DumpOnIt »

Hey all! I'm having a strange issue with coroutines.

I'm writing a dialog system for my game, and I use a coroutine for each dialog. In the dialog coroutine, I yield with two things: A string to display in the dialog box, and a table with a bunch of strings to choose from, when being asked a question.

The way answering questions is supposed to work is that the coroutine will be resumed by passing the string from the choice list back into the coroutine. For some reason, it doesn't use the value from the table of choices when passed back into the coroutine.

In my test program, I have a selectedChoice variable, which comes from choices table, using a choice number as a key to get a value from it. I have a print function right before my coroutine that displays the value of the selectedChoice variable. Inside the coroutine, I have another print function that displays the value of the variable that was passed into it.

The default value for selectedChoice is "No Response."

Here are some of the behaviors I noticed:
  • Passing a literal string into coroutine.yield will have the coroutine properly print that string.
  • Assigning a literal string value "test" to selectedChoice right before the first print statement will have both print "test."
  • Running my program unmodified will have the first print statement print whatever value is selected, while the coroutine will print "No Response"
Here is a small test program. I'm stumped, but I'm hoping that it's something I did that will be easy to fix. It opens a console on windows. Press space to display the next message. Press 1 or 2 to select a response.

Test file (.love, 669 bytes) (0.9.1)

Edit: Ah bugger, wrong subforum
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Weird coroutine issue

Post by Robin »

Code: Select all

testFunc = function()
	say("Hello, there!")
	local response = say("How are you?", {"Good", "Bad"})
	say("I heard you say: " .. response)
	say("Goodbye!")
	love.event.push("quit")
end
Extra arguments to coroutine.resume do not change the arguments to the wrapped function somehow (how would that work within nested function calls?) but are returned from coroutine.yield.
Help us help you: attach a .love.
User avatar
DumpOnIt
Prole
Posts: 30
Joined: Fri Apr 06, 2012 6:59 am

Re: Weird coroutine issue

Post by DumpOnIt »

Haha, that's pretty funny! I didn't realize that I had to get the return value from coroutine.yield. Up to now I guess the stuff I have been doing didn't require it! :oops:

Thanks muchly!
Post Reply

Who is online

Users browsing this forum: No registered users and 94 guests