I should start by saying that until a week ago I'd never written a line of code in my life. But my head is bursting with game ideas and after some searching I stumbled across Lua and Löve and it seemed the best of possible places to start. I quickly fell in löve (I'm sorry but these damn puns just write themselves!) and, after following a few tutorials and doing some reading on the wiki, I started trying to put together something of my own. Here's what I'm after:
The long-distance goal is a game that incorporates dialogue trees and so I figured the best way of learning about these would be crafting together their essential skeleton: a text-based adventure. I soon realized that this requires the implementation of state machines and here's where I've gotten stuck. I read chapter 6.3 on Proper Tail Calls in Roberto Ierusalimschy book and thought my problems solved with the return command but I was wrong. The solution seems banally simple but I just can't figure out the right terminology.
So, would somebody please be kind enough to explain to me (or redirect me to a place where this is explained, should I have missed that during my forum searches) how I go about putting together the following:
(it should perhaps be underlined that the practical content of the below is completely arbitrary; I'm only after figuring out the mechanics of how exactly one implements it)
- on startup there should be a greeting prompting you to press space
- having pressed space there should pop up a new prompt, telling you to press either 1 or 2
- if you press 1 you should get output1
- if you press 2 you should get output2
Code: Select all
function love.load() love.graphics.setFont(12) greeting = "Hello Wörld (Press SPACE to proceed.)" options1 = "" options2 = "Are you impressed? (Press ONE for Yes, TWO for No.)" output0 = "" output1 = "I know, right? It's amazing what a neobum can do!" output2 = "Why would you sit there and lie like that to yourself?" end function love.update(dt) end function love.keyreleased(key) if key == " " then options1 = options2 end if key == "1" then output0 = output1 elseif key == "2" then output0 = output2 end if key == "return" then love.load() end end function love.draw() love.graphics.setColor(0, 255, 255, 255) love.graphics.print(greeting, 50, 50) love.graphics.print(options1, 60, 80) love.graphics.print(output0, 70, 110) end
The problem is that all three keys (spacebar, 1 and 2 respectively) are constantly listened for and will give their respective outputs regardless of when they are pressed. I need the spacebar to be a prerequisite for the other two options and once one of them is selected the other should no longer be available to press. As I say, I know this needs to be done by setting various states and calling various functions. Instinctively it's so straight forward. In theory I can see the whole thing set up in front of me but I just can't figure out how to phrase the damned thing!
Somebody please help me! I've been running around in circles for days and it's long since stopped being funny (well, all right, it is a little bit funny). If this is all comprehensively covered in another thread then I apologize for blundering in here and wasting your time. If it isn't... how about we comprehensively cover it here?