Waiting for user input?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Blaza
Prole
Posts: 2
Joined: Mon Aug 31, 2020 6:17 pm

Waiting for user input?

Post by Blaza »

Hello everyone,

I am currently learning Love and was wondering if there was any way to wait for user input before proceeding on with the program? I was thinking of setting up a love.event.wait() until love.keypressed() ~= nill but I couldn't get it to work, presumably from syntax errors.

Am I on the right track there? For reference I am attempting to make a roguelike game and would like the program to just pause until the player does a certain input then processing the rest of the actors turns afterwards.

Thank you for your help in advance
User avatar
Ulhar
Prole
Posts: 2
Joined: Sat Aug 29, 2020 6:55 pm
Contact:

Re: Waiting for user input?

Post by Ulhar »

I suppose you are going for a turn-based game? If so, I would say don't update the game's current state if a key wasn't just pressed. (Also worth noting: love.keypressed is an event callback. It doesn't return a key that was just pressed, rather it gets called when a key is pressed, passing the key that was pressed.)

Code: Select all

-- main.lua
local waiting = false
local lastKey

function love.keypressed(key)
	lastKey = key
end

function love.update(dtime)
	if lastKey then
		-- do your turn logic
	end
	
	lastKey = nil -- so it doesn't trigger things in later update calls
end

function love.draw()
	-- This part is up to you, as is this whole thing, really
end
Hopefully, this helps :P
RNavega
Party member
Posts: 270
Joined: Sun Aug 16, 2020 1:28 pm

Re: Waiting for user input?

Post by RNavega »

The logic should be similar to when doing a pause screen ("Press P to pause"): you need to set up a finite-state-machine that has a state for this.

Here's the theory:
- http://howtomakeanrpg.com/a/state-machines.html
- https://gameprogrammingpatterns.com/state.html
- https://gamedevelopment.tutsplus.com/tu ... edev-11867

The love.event.wait() function completely blocks execution until an event happens. It's better used for editor-like programs (tilemap level editors, scene layout editors, that kind of thing), when you can absolutely block the program until input happens.

If you're making a game, even turn-based, it's likely you still want the program to keep running in the background and polling for input. You might have some timer variable that you want keep track of, or update the sprite idle animations, or having visual changes happen to the mouse movements (like units that highlight when you hover the mouse over them), or letting the user navigate the viewport etc.

So the love callbacks that Ulhar showed above tell you what happened (mouse move, keypress etc.). On each of those callbacks you should ask the current game state, whatever it is, to process that mousemove or keypress so the game can respond to what happened, even if the result is "do nothing" (like in your pause screen or turn-wait-screen in case the player pressed the wrong key).
User avatar
Blaza
Prole
Posts: 2
Joined: Mon Aug 31, 2020 6:17 pm

Re: Waiting for user input?

Post by Blaza »

Ulhar, thank you for the code snippit. I didn't know that "if var then" would trigger when it is nill, thought it was boolean only. That is good to know, along with knowing the event callback about love.keypressed.

RNavega thank you for the links, I will read up on them and go about implementing a state machine, I have mainly used those in the past for animation handling in the Godot engine. I wanted to try something a bit more bare bones than an engine, which is how I landed here.

Thank you both for the help!
RNavega
Party member
Posts: 270
Joined: Sun Aug 16, 2020 1:28 pm

Re: Waiting for user input?

Post by RNavega »

I'm also getting familiar with LÖVE, so if you make any breakthroughs do share! x)

Hm, I just tested here, if you paste "love.event.wait()" in the middle of your code, when the program reaches that point it halts until you do any of the unblocking events (mouse-move/mouse-press/keypress/key-release etc.).
So this is very useful for debugging, like a "wait key" type of thing, if you need the program to stop at a specific point.
User avatar
Ulhar
Prole
Posts: 2
Joined: Sat Aug 29, 2020 6:55 pm
Contact:

Re: Waiting for user input?

Post by Ulhar »

Blaza wrote: Tue Sep 01, 2020 12:47 pm I didn't know that "if var then" would trigger when it is nill, thought it was boolean only.
Lua's if statements can accept any value, not just booleans. nil and false are falsy, everything else is truthy. (Also worth noting that the number zero is still truthy, unlike most languages I can think of.)
Post Reply

Who is online

Users browsing this forum: Google [Bot], pgimeno and 4 guests