Hi, I'm Swoffle - I've been trying lots of different things to solve this issue. None of them worked, so I thought: "Why not put my issue in the forums?". So here it is, folks:
I've given making a Visual Novel type game a shot - using the mighty jolly LÖVE engine! It's been going quite well, for now my code is set up so that the player has to press space to see the next image (in the future the player will also be able to, ofcourse, make choices). However, at the moment all I can do is press space three times before my code does nothing anymore. Here's the code for all you fine fellows wanting to help me:
Many thanks in advance!
If you don't like taking the main.lua file, that's fine! You can find my code below:
local Intro1
local Intro2
local Intro3
local Louis1
local Louis2
local drawimage1 = true
local drawimage2 = false
local drawimage3 = false
local drawimageLouis1 = false
local drawimageLouis2 = false
function love.load()
Intro1 = love.graphics.newImage("Intro1.png")
drawimage1 = true
Intro2 = love.graphics.newImage("Intro2.png")
drawimage2 = false
Intro3 = love.graphics.newImage("Intro3.png")
drawimage3 = false
Louis1 = love.graphics.newImage("Louis1.png")
drawimageLouis1 = false
Louis2 = love.graphics.newImage("Louis2.png")
drawimageLouis2 = false
IntroMusic = love.audio.newSource("Night of Chaos.mp3")
IntroMusic:setVolume(0.3)
IntroMusic:setPitch(1)
end
function love.update(dt)
function love.keypressed( key )
if key == "space" then
IntroMusic:play()
if drawimage1 == true then
drawimage2 = true
drawimage1 = false
elseif drawimage2 == true then
if key == "space" then
drawimage3 = true
drawimage2 = false
elseif drawimage3 == true then
if key == "space" then
drawimageLouis1 = true
drawimage3 = false
elseif drawimageLouis1 == true then
if key == "space" then
drawimageLouis1 = false
drawimageLouis2 = true
elseif drawimageLouis2 == true then
if key == "space" then
end
end
end
end
end
end
end
end
function love.draw()
if drawimage1 == true then
love.graphics.draw(Intro1, 0, 0)
drawimageLouis1 = false
elseif drawimage2 == true then
love.graphics.draw(Intro2, 0, 0)
drawimage1 = false
elseif drawimage3 == true then
love.graphics.draw(Intro3, 0, 0)
drawimage2 = false
elseif drawimageLouis1 == true then
love.graphics.draw(Louis1, 0, 0)
drawimage3 = false
elseif drawimageLouis2 == true then
love.graphics.draw(Louis2, 0, 0)
drawimageLouis1 = false
end
end
Newbie question - Visual Novel type setup not working!
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Prole
- Posts: 7
- Joined: Sun Feb 25, 2018 11:23 am
Re: Newbie question - Visual Novel type setup not working!
Did you really write this main.lua with no indentation whatsoever, or did the file get messed up somehow?! You should always indent your code (with spaces or tabs, whichever you like), that way you can actually read it and see what's going on.
With indentation, here is your update function:
Hopefully this helps you see the issue right away. The problem you're talking about is because your nested conditions are screwed up.
1. You put your `end`s in the wrong places. (wrong for what you wanted the code to do, but not wrong enough to cause errors)
2. You keep repeating `if key == "space" then` one inside the other.
You check `if key == "space" then` in the first line inside your love.keypressed function. You don't need to repeat it inside the same block, you've already checked it, you know it is true. Because of the second extra `if` (with no `end` nearby to close it), your check for `drawimage3` is inside the check for 'drawimage2', and in sequence with `if key == "space"`, so it that code will never get run.
With indentation, here is your update function:
Code: Select all
function love.update(dt)
function love.keypressed( key )
if key == "space" then
IntroMusic:play()
if drawimage1 == true then
drawimage2 = true
drawimage1 = false
elseif drawimage2 == true then
if key == "space" then
drawimage3 = true
drawimage2 = false
elseif drawimage3 == true then
if key == "space" then
drawimageLouis1 = true
drawimage3 = false
elseif drawimageLouis1 == true then
if key == "space" then
drawimageLouis1 = false
drawimageLouis2 = true
elseif drawimageLouis2 == true then
if key == "space" then
end
end
end
end
end
end
end
end
1. You put your `end`s in the wrong places. (wrong for what you wanted the code to do, but not wrong enough to cause errors)
2. You keep repeating `if key == "space" then` one inside the other.
You check `if key == "space" then` in the first line inside your love.keypressed function. You don't need to repeat it inside the same block, you've already checked it, you know it is true. Because of the second extra `if` (with no `end` nearby to close it), your check for `drawimage3` is inside the check for 'drawimage2', and in sequence with `if key == "space"`, so it that code will never get run.
-
- Prole
- Posts: 7
- Joined: Sun Feb 25, 2018 11:23 am
Re: Newbie question - Visual Novel type setup not working!
Hi! Thanks for the mighty jolly tips, I'm a rather inexperienced programmer, hence the silly mistakes of mine. Thank you for helping me fix the issue, though! You're completely right, I only had to put "if key == "space" then" once after the keypressed function.
You truly made my day! I hope you have a good one!
You truly made my day! I hope you have a good one!
- zorg
- Party member
- Posts: 3465
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: Newbie question - Visual Novel type setup not working!
To expand on a few things already observed:
- The indentation might have been fine, the bigger issue was that they didn't paste all that code into [ code ] [ /code ] blocks.
- You do not want to ever define any functions inside either love.update or love.draw, like how you are doing currently, since this way, they will be redefined each frame, which is pointless work.
- Ross is wrong in that currently, the horrid elseif block would actually work how you want it to; plain if-then-else blocks would be worse since then, the logic wouldn't stop... unless you put a return for each branch... and then you realize, the keypressed, or any other function, for that matter, will always start at the first line of code inside it, and can't resume execution from arbitrary points, unless you keep track of unnecessarily complex state, and do even more branching, whether the if-then-else or if-then-elseif kind.
The easier solution to all this is learning how to use coroutines; they are practically made for stuff like this; keeping code flow clean.
The harder solution would be a script parser, like what ren'py may do.
- The indentation might have been fine, the bigger issue was that they didn't paste all that code into [ code ] [ /code ] blocks.
- You do not want to ever define any functions inside either love.update or love.draw, like how you are doing currently, since this way, they will be redefined each frame, which is pointless work.
- Ross is wrong in that currently, the horrid elseif block would actually work how you want it to; plain if-then-else blocks would be worse since then, the logic wouldn't stop... unless you put a return for each branch... and then you realize, the keypressed, or any other function, for that matter, will always start at the first line of code inside it, and can't resume execution from arbitrary points, unless you keep track of unnecessarily complex state, and do even more branching, whether the if-then-else or if-then-elseif kind.
The easier solution to all this is learning how to use coroutines; they are practically made for stuff like this; keeping code flow clean.
The harder solution would be a script parser, like what ren'py may do.
Me and my stuff True 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.
Re: Newbie question - Visual Novel type setup not working!
@zorg - Yeah, I originally assumed it was just the forum formatting, but then I checked the actual file he uploaded, and it was the same.
Zorg beat me to the issue with defining 'love.keypressed' inside 'love.update', but I will add my 2 cents too. When you use the `function` keyword, you are defining that function. So you're not calling `love.keypressed` every update, you're just re-setting what the function does (to exactly the same thing every time).
If you want to check keyboard input on update (instead of or in addition to using keypressed and keyreleased events), you should call `love.keyboard.isDown()` or `love.keyboard.isScancodeDown()'. See the love.keyboard docs.
The other thing is, using if/elseif/else conditionals for this sort of thing will only get you so far. You should learn to use tables, they will make your life a lot easier. Basically, anytime you find yourself writing a chunk of almost identical code more than twice, you should think about using a table, or putting that code in its own function. That's what computers are for: to do repetitive work for you.
As an example, using a table to store your images, your main.lua could be rewritten like this: (note: I didn't test this)
Zorg beat me to the issue with defining 'love.keypressed' inside 'love.update', but I will add my 2 cents too. When you use the `function` keyword, you are defining that function. So you're not calling `love.keypressed` every update, you're just re-setting what the function does (to exactly the same thing every time).
If you want to check keyboard input on update (instead of or in addition to using keypressed and keyreleased events), you should call `love.keyboard.isDown()` or `love.keyboard.isScancodeDown()'. See the love.keyboard docs.
The other thing is, using if/elseif/else conditionals for this sort of thing will only get you so far. You should learn to use tables, they will make your life a lot easier. Basically, anytime you find yourself writing a chunk of almost identical code more than twice, you should think about using a table, or putting that code in its own function. That's what computers are for: to do repetitive work for you.
As an example, using a table to store your images, your main.lua could be rewritten like this: (note: I didn't test this)
Code: Select all
local images = {
"Intro1.png",
"Intro2.png",
"Intro3.png",
"Louis1.png",
"Louis2.png",
}
local introMusic
local curImgIndex = 1
function love.load()
for i,imgName in ipairs(images) do
images[i] = love.graphics.newImage(imgName)
end
introMusic = love.audio.newSource("Night of Chaos.mp3")
introMusic:setVolume(0.3)
end
function love.keypressed(key)
if key == "space" then
if not introMusic:isPlaying() then
introMusic:play()
end
local i, img = next(images, curImgIndex)
if i then
curImgIndex = i
end
end
end
function love.draw()
love.graphics.draw(images[curImgIndex])
end
Who is online
Users browsing this forum: Bing [Bot], Google [Bot] and 8 guests