Clicking through large amounts of text

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
fade
Prole
Posts: 9
Joined: Tue Jan 26, 2016 9:47 pm

Clicking through large amounts of text

Post by fade »

Hey guys! Me again, now with a different issue. This one is another problem that kept me thinking for several hours until I gave up and decided to ask you for help again. The thing is I have this one part in my game, where there's a tonn of text. All of it comes in portions to a rather small dialog window. What I want to do is for the player to be able to hit space to go to the next segment of text.
Like for example:

Game shows begining of long text.
User hits SPACE
Game then shows Chapter 2 of text
User reads Chapter 2, hits SPACE
Game then shows Chapter 3 of text
etc...

I made 3 parts of my text into variables, which are called by pressing space, but after I hit space once, it shows me the last part of the text, never even showing all the others! Here's my code. Please, point me to the right direction! Thanks a bunch!

Code: Select all

if inttex1 == true then
gr.printf("You have ventured far from your birthplace, to attend the court of Faderon, God-King of all Space, and Lord-Protector of Matter and Time. You came here seeking entry into the Royal Guard, and grand riches not imaginable in thine sickly distant feod. However, only a ruin of it's former glory greets you, as you draw near. Castle Fade, mighty citadel worthy of containing the Outer Gods - now a shambles. It's Gardens a mockery of Nature. It's walls and parapets as black and crooked as beggar's teeth, point aimlessly at the darkened, stormy sky. It's moat, like a tongue of rotten wood, sticking out of the uneven mouth of the main entrance.", 25, 455, 580, "center" )
end
if inttex2 == true then
gr.printf("A passing merchant you meet near the road to the keep, spits on the ground and mumbles with contempt: `Tis a bad omen to be striding near the perverted lands of the accursed Corpulent King!`. Left unsettled by this encounter, you make your way to the gate, and acknowledge your presence. Minutes later, you are already being escorted into the throne room, to be greeted by Lord Faderon himself!.. ", 25, 455, 580, "center")
end
if inttex3 == true then
gr.printf("`Ack! Pleasant day!` - The Corpulent King mutters, nursing a goblet of murky wine - `We hath a "..playerclass.." in our lively court! Disturbs me greatly tis, that news of my ordeal art so widely spread across mine land!`\nYou tell King Faderon that you came to serve his court, but you did not hear of any ordeal.\n`Then thou art but a fool, lowly "..playername.."!` - The Corpulent King spills some of his wine with a drunkard's gesture - `What need I for a fool, when gates to All Hells opened below my bustling halls, and all my Cosmic Might hath gone and left me?!`", 25, 455, 580, "center" )
end

if inttex4 == true then
gr.printf("`But perhaps, it hath not left me for good... For I am Faderon, King of Past, Present and Future, and all the Planes and Spheres of Outer Space are mine to rule and destroy!` - King Faderon pushes himself closer to you - `What say ye, lowly fool? Ye offspring of a dirty harlot! Mayhap thou wisheth to aid his liege in times of peril, and descend the caverns below? Or shall I be forced to set my She-Jesters upon ye for a terrible knifing?!`\nWill you agree to serve King Faderon through his ordeal? Press Y to serve. Press N to get knifed by She-Jesters.`", 25, 455, 580, "center" )

end

function love.keypressed(space, isrepeat )

if kb.isDown("space") then
inttex1 = true

if kb.isDown("space") and inttex1 == true then
inttex1 = false
inttex2 = true
end

if kb.isDown("space") and kb.isDown ("space") and inttex2 == true then
inttex2 = false
inttex3 = true
end
end
0x29a
Prole
Posts: 27
Joined: Sun Jul 06, 2014 10:22 pm

Re: Clicking through large amounts of text

Post by 0x29a »

Code: Select all

texts = {} 
texts[1] = "blablabla" 
texts[2] = "bablalba" 
-- etc, etc, etc... 

currentText=1

if kb.isDown("space") then
	currentText = currentText + 1;
end

gr.printf(texts[currentText],, 25, 455, 580, "center" ) 


You can make it even better way, it's the first thing that I thought of.
Also - refactor. If you're using same thing over and over again, write a function for it

Also, keyPressed would work better I think, but it's very late at night and I cant think clearly

Hope it helps
Cheers!
User avatar
Beelz
Party member
Posts: 234
Joined: Thu Sep 24, 2015 1:05 pm
Location: New York, USA
Contact:

Re: Clicking through large amounts of text

Post by Beelz »

I didn't plan on completely rewriting your code, but yea... And by no means is this the best way to accomplish what your trying to do, but it works. (and sorry but i didn't proof read the texts)

Code: Select all

local textTable = {
	[["You have ventured far from your birthplace, to attend the court of Faderon, 
	God-King of all Space, and Lord-Protector of Matter and Time. You came here 
	seeking entry into the Royal Guard, and grand riches not imaginable in thine 
	sickly distant feod. However, only a ruin of it's former glory greets you, as 
	you draw near. Castle Fade, mighty citadel worthy of containing the Outer Gods 
	- now a shambles. It's Gardens a mockery of Nature. It's walls and parapets as 
	black and crooked as beggar's teeth, point aimlessly at the darkened, stormy 
	sky. It's moat, like a tongue of rotten wood, sticking out of the uneven mouth 
	of the main entrance."]],

	[["A passing merchant you meet near the road to the keep, spits on the ground and 
	mumbles with contempt: `Tis a bad omen to be striding near the perverted lands 
	of the accursed Corpulent King!`. Left unsettled by this encounter, you make your 
	way to the gate, and acknowledge your presence. Minutes later, you are already 
	being escorted into the throne room, to be greeted by Lord Faderon himself!.. ",
	"`Ack! Pleasant day!` - The Corpulent King mutters, nursing a goblet of murky 
	wine - `We hath a THINGY" ..playerclass .. " in our lively court! Disturbs me greatly tis,
	that news of my ordeal art so widely spread across mine land!`\nYou tell King 
	Faderon that you came to serve his court, but you did not hear of any ordeal.\n
	`Then thou art but a fool, lowly BUBBA"..playername .. "!` - The Corpulent King spills 
	some of his wine with a drunkard's gesture - `What need I for a fool, when gates 
	to All Hells opened below my bustling halls, and all my Cosmic Might hath gone 
	and left me?!`"]],

	[["`But perhaps, it hath not left me for good... For I am Faderon, King of Past, 
	Present and Future, and all the Planes and Spheres of Outer Space are mine to rule 
	and destroy!` - King Faderon pushes himself closer to you - `What say ye, lowly 
	fool? Ye offspring of a dirty harlot! Mayhap thou wisheth to aid his liege in times 
	of peril, and descend the caverns below? Or shall I be forced to set my She-Jesters 
	upon ye for a terrible knifing?!`\nWill you agree to serve King Faderon through his 
	ordeal? Press Y to serve. Press N to get knifed by She-Jesters.`"]]	
}

local i = 1
local shouldShow = true

function love.draw()
	if shouldShow then
		love.graphics.printf(textTable[i], love.graphics.getWidth()/2-300, 200, 600, "center")
	end
end

function love.keypressed(key, unicode)
	if key == " " then
		if i < 3 then
			i = i + 1
		else
			shouldShow = false
		end
	end
end

Code: Select all

if self:hasBeer() then self:drink()
else self:getBeer() end
GitHub -- Website
fade
Prole
Posts: 9
Joined: Tue Jan 26, 2016 9:47 pm

Re: Clicking through large amounts of text

Post by fade »

0x29a, Beelz, wow, you guys and this forum, you're just the best! Thank you so much! I implemented both your codes and learned a tonn of new stuff I'm gonna be using in other parts of the adventure and with the text here! Thanks a bunch, I really feel like i'm gonna complete my game some day! The Golden Child shall fly!
Rickton
Party member
Posts: 129
Joined: Tue Mar 19, 2013 4:59 pm
Contact:

Re: Clicking through large amounts of text

Post by Rickton »

Other people have given you code that'll probably work, but here's why yours didn't work.

The reason it skips to the end is because you're running all the checks one after another. The code in any given function runs all at once (not literally true but enough for practical purposes), the display doesn't update before the code finishes running, so the display is never updated when inttex is anything other than 3
Comments below to explain:

Code: Select all

function love.keypressed(space, isrepeat )

if kb.isDown("space") then
 inttex1 = true

 --Now inttex1 is true. But the code doesn't stop there, it goes to the next line, which checks to see if inttex1 is true:
 if kb.isDown("space") and inttex1 == true then --And of course, inttex1 is true, we just set it to true, so the game runs the code in this IF and sets inttex2 to true.
  inttex1 = false
  inttex2 = true
 end

--The code then immediately checks to see if inttex2 is true:
 if kb.isDown("space") and kb.isDown ("space") and inttex2 == true then --And again, of course it is, so we run the code in this IF and set inttex3 to true.
  inttex2 = false
  inttex3 = true
 end
end
Could be easily fixed using ELSEIF, because that way once one IF is true, the ones after it won't be checked. (Although some of the other fixes posted are better)

Other unrelated things that popped up that aren't really problems per se, but seemed kind of weird.
First of all, you don't really need to use isDown in keypressed. The first argument contains the key, so you could just do if key == "space". (Technically I guess it's not wrong to do it your way, but it's more typing).
Secondly, why are you checking for "space" twice in the last IF?
Thirdly, the first IF statement doesn't have an END. Unless the whole thing is supposed to be contained inside that IF, in which case, you don't need to check for "space" in the other IFs.
Creator of:
Wizard School Dropout, available free on itch.io
Possession , which is available on itch.io or Steam
User avatar
Beelz
Party member
Posts: 234
Joined: Thu Sep 24, 2015 1:05 pm
Location: New York, USA
Contact:

Re: Clicking through large amounts of text

Post by Beelz »

Sorry I was strapped for time so there was no explanation included in my reply. Now I just want to continue on with theory for a minute.

What I would do (assuming not the entire game is text) is make a returnable table of all the major texts in your game in another file. From there, every time you need one of those texts just call upon the next one in the table... or you could even name the sections and call them arbitrarily if the story isn't so linear.

ex:

Code: Select all

local textTable = {
	mainIntro1 = [[Blah blah blah]],
	mainIntro2 = [[Blah blah blah]],
	mainIntro3 = [[Blah blah blah]],

	plotTwist1 = [[Blah blah blah]],
	plotTwist2 = [[Blah blah blah]],
	plotTwist3 = [[Blah blah blah]],

	mainEnding1 = [[Blah blah blah]],
	mainEnding2 = [[Blah blah blah]],
	mainEnding3 = [[Blah blah blah]]
}

return textTable
Then you need to require it:

Code: Select all

local textTable = require 'path.to.file' -- dont include .lua after filename

Code: Select all

if self:hasBeer() then self:drink()
else self:getBeer() end
GitHub -- Website
fade
Prole
Posts: 9
Joined: Tue Jan 26, 2016 9:47 pm

Re: Clicking through large amounts of text

Post by fade »

Guys, I can't tell how grateful I am! Thank you so much! Now I know tons of new stuff, and also where I was mistaken!
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 3 guests