isDown, keypressed or wasPressed?

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
Sashz
Prole
Posts: 5
Joined: Sun Sep 24, 2023 3:13 pm

isDown, keypressed or wasPressed?

Post by Sashz »

I'm creating a text based game (with images) and I don't really know how to change the scene using the directional keys.
When I use isDown, depending on the duration of pressing the key, the game skips the next scene.

Example:

Code: Select all

function update_forest(dt)
  if love.keyboard.isDown('right') then
    scene = 'waterfall'
  end
  if love.keyboard.isDown('left') then
    scene = 'cabin'
  end
end

function update_waterfall(dt)
  if love.keyboard.isDown('left') then
    scene = 'forest'
  end
end

function update_cabin(dt)
  if love.keyboard.isDown('right') then
    scene = 'forest'
  end
end
In this case, if I'm in 'waterfall' and press left, I go to 'cabin', and 'waterfall' appears in just a blink of an eye.
I know that keypressed or wasPressed can solve my case, but I don't know how to implement this. I also know that there are better ways to write this code, but I'm new to LOVE2D and I'd like to continue in this more amateur way for now...
All help will be appreciated!
User avatar
BrotSagtMist
Party member
Posts: 634
Joined: Fri Aug 06, 2021 10:30 pm

Re: isDown, keypressed or wasPressed?

Post by BrotSagtMist »

For one time events you only want to react the moment the key is pressed

Code: Select all

function love.keypressed(k)
 if k=="right" then next() end
end
and then have all your scene switch logic in its own function rather than having it scattered around in every scene.
In this case we need two additional tables and a variable holding where we are.

Code: Select all

nextlist={cabin="waterfall",waterfall="drowning"}
backlist={cabin="title",waterfall="cabin"}
So when either next() or a back going function is called they just need to lookup where the way is going to.

Code: Select all

function next()
position=nextlist[position]
end
function back()
position=backlist[position]
end
obey
User avatar
zorg
Party member
Posts: 3446
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: isDown, keypressed or wasPressed?

Post by zorg »

Also, there is no such thing as wasPressed.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
Sashz
Prole
Posts: 5
Joined: Sun Sep 24, 2023 3:13 pm

Re: isDown, keypressed or wasPressed?

Post by Sashz »

zorg wrote: Thu Sep 28, 2023 6:45 pm Also, there is no such thing as wasPressed.
You are right.
It was Google Bard who suggested I use this...
User avatar
Sashz
Prole
Posts: 5
Joined: Sun Sep 24, 2023 3:13 pm

Re: isDown, keypressed or wasPressed?

Post by Sashz »

BrotSagtMist wrote: Thu Sep 28, 2023 5:36 pm For one time events you only want to react the moment the key is pressed

Code: Select all

function love.keypressed(k)
 if k=="right" then next() end
end
and then have all your scene switch logic in its own function rather than having it scattered around in every scene.
In this case we need two additional tables and a variable holding where we are.

Code: Select all

nextlist={cabin="waterfall",waterfall="drowning"}
backlist={cabin="title",waterfall="cabin"}
So when either next() or a back going function is called they just need to lookup where the way is going to.

Code: Select all

function next()
position=nextlist[position]
end
function back()
position=backlist[position]
end
Thanks for the answer.

In addition to moving forward and backward, the player can turn right or left, so the final result looked like this:

Code: Select all

next_list = {}
back_list = {}
right_list = {}
left_list = {}

function love.load()
  next_list = {trail = 'forest', forest = 'lake'}
  back_list = {forest = 'trail', lake = 'forest'}
  right_list = {forest = 'waterfall', waterfall = 'trail'}
  left_list = {forest = 'cabin', cabin = 'trail'}
end

function next()
  position = next_list[position]
end

function back()
  position = back_list[position]
end

function go_right()
  position = right_list[position]
end

function go_left()
  position = left_list[position]
end

function love.keypressed.(key)
  if key == 'up' then
    next_list()
  end
  if key == 'back' then
    back_list()
  end
  if key == 'right' then
    right_list()
  end
  if key == 'left' then
    left_list()
  end
Although no error appears, the scene is not changed.
Where is the error?
User avatar
dusoft
Party member
Posts: 520
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: isDown, keypressed or wasPressed?

Post by dusoft »

Sashz wrote: Thu Sep 28, 2023 9:34 pm Although no error appears, the scene is not changed.
Where is the error?
You will need to use both love.update() to process updates and love.draw() to paint updates.
Also:

Code: Select all

love.keypressed.(key)
needs to become

Code: Select all

love.keypressed(key)
Use some environment (IDE) that will announce (and fix) such typos and common errors.

Also, don't trust code from LLMs such as Bard.

See my experiment: https://ambience.sk/lua-code-generation ... e-chatgpt/
User avatar
BrotSagtMist
Party member
Posts: 634
Joined: Fri Aug 06, 2021 10:30 pm

Re: isDown, keypressed or wasPressed?

Post by BrotSagtMist »

No error appears?
I cant even count all the stuff wrong here.
Aside from typos, the main thing is youre calling the tables not the functions.
obey
User avatar
Sashz
Prole
Posts: 5
Joined: Sun Sep 24, 2023 3:13 pm

Re: isDown, keypressed or wasPressed?

Post by Sashz »

BrotSagtMist wrote: Thu Sep 28, 2023 10:00 pm No error appears?
I cant even count all the stuff wrong here.
Aside from typos, the main thing is youre calling the tables not the functions.
In fact, I had already corrected this error of calling tables instead of functions, but I ended up making the same mistake here on the forum. And I also made typing errors, because I ended up creating this snippet of code right here on the page.

This is the first time I've asked for help with game development. I will be more careful in the future!

I'm sorry for that.
User avatar
Sashz
Prole
Posts: 5
Joined: Sun Sep 24, 2023 3:13 pm

Re: isDown, keypressed or wasPressed?

Post by Sashz »

dusoft wrote: Thu Sep 28, 2023 9:58 pm
Sashz wrote: Thu Sep 28, 2023 9:34 pm Although no error appears, the scene is not changed.
Where is the error?
You will need to use both love.update() to process updates and love.draw() to paint updates.
Also:

Code: Select all

love.keypressed.(key)
needs to become

Code: Select all

love.keypressed(key)
Use some environment (IDE) that will announce (and fix) such typos and common errors.

Also, don't trust code from LLMs such as Bard.

See my experiment: https://ambience.sk/lua-code-generation ... e-chatgpt/
The code I created has the love.update(dt) and love.draw() functions, I just didn't post it here because I thought it wouldn't be necessary. In the future I will format my questions better here on the forum.

This snippet of code I posted I ended up writing right here in the post, hence the errors... On the computer I use Sublime Text.

In the "function love.keypressed(key)" part, I call the functions, not the tables, as I put here. I had already corrected this error, but I ended up posting it here incorrectly.

I'm sorry for that.

In fact, the player should just type to advance in the game, but I ended up deciding to add directional keys so he can move around. However, the scene is not being changed.

And yes, things like Bard can't be trusted.
User avatar
BrotSagtMist
Party member
Posts: 634
Joined: Fri Aug 06, 2021 10:30 pm

Re: isDown, keypressed or wasPressed?

Post by BrotSagtMist »

Why dont you just post the entire thing exactly as you intend to make it?
Troubleshooting works best for .love files, these snippets tell us little to nothing.
obey
Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests