Need help with in game dialog code

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
kicknbritt
Citizen
Posts: 96
Joined: Sat May 30, 2015 2:15 am
Location: Chicago, IL/Anchorage,AK

Need help with in game dialog code

Post by kicknbritt »

So, before I give my problem or anything I just want to say this community is awesome and I really enjoy working with love and reading the awesome and helpful replies from members of this forum. I've never posted on a forum before, so if i've done anything wrong or incorrect I apologize and you can just call me out on it.

Recently ive been working on a way to implement dialog into a game ive been working on.... and after alot of searching i've found that io.lines reading through lines of a text file for each part of a dialog as a solid way to implement this. After alot of testing and modifying code all night, I've come up with a solution... but when I try to change the line number to read the dialog I want to diplay, the variable lineno changes, but the printed dialog wont..... can anybody tell me what i'm doing wrong or give me a better way to do it???

This is the dialog code using io.lines

Code: Select all


lineno = 1



local n=0


for Lr in io.lines("C:\\Program Files\\LOVE\\games\\points\\dialog.txt") do-------UPDATE 9/21/16 lineno wont change the displayed dialog when it chanes.... DX

   n=n+1

   if n==lineno then       print(Lr) LineRead = Lr

 ; break end
end
and the .txt file is just

Code: Select all

line1
line2
line3
line4
line5
Thanks for your time.
"I AM THE ARBITER!!!" *Pulls out Energy sword and kills everything*
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Need help with in game dialog code

Post by zorg »

Hi!

Can't see the issue with your snippet at the moment, but let me give you another approach:

Assuming you will eventually have more than just five lines of dialogue, you could do this in two steps:

1. Use a [wiki]File[/wiki] object to open the file, then use :lines to iterate over the lines, save the current line's starting position, and its length, to a table, then add the line's character count (hopefully it includes the line break character(s) as well, i didn't test this) to a variable where you keep a running total.

2. Whenever you need to display a line, you can open the file the same way, but you can seek to the correct position, and read only as many characters as there were in that line.

If the implementation is correct, it should always give you back the correct line.

Downside is that this needs O(n) or linear time to pre-process, which you can do just once if you don't modify the dialogue file, but the upside is that accesssing lines will take O(1) or constant time, which is of course faster, and my logic is that you want to show dialogue more times than modify it anyway. :3
Last edited by zorg on Thu Sep 22, 2016 7:03 pm, edited 1 time in total.
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
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: Need help with in game dialog code

Post by Positive07 »

I haven't read the code but a suggestion if you are using LÖVE and not bare Lua is that you use [wiki]love.filesystem.lines[/wiki] over [manual]io.lines[/manual] because [manual]io[/manual] can't read from inside .love files and that is probably where your dialog files will be.
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
User avatar
kicknbritt
Citizen
Posts: 96
Joined: Sat May 30, 2015 2:15 am
Location: Chicago, IL/Anchorage,AK

Re: Need help with in game dialog code

Post by kicknbritt »

Oh! Good point positive07 I appreciate your help!!! :D
And zorg thanks much for the alternate approach, I'll rewrite it how you said and repost what happens!
"I AM THE ARBITER!!!" *Pulls out Energy sword and kills everything*
User avatar
kicknbritt
Citizen
Posts: 96
Joined: Sat May 30, 2015 2:15 am
Location: Chicago, IL/Anchorage,AK

Re: Need help with in game dialog code

Post by kicknbritt »

well I just got a job working on a website so it might take a bit ... lol
"I AM THE ARBITER!!!" *Pulls out Energy sword and kills everything*
Post Reply

Who is online

Users browsing this forum: Amazon [Bot] and 2 guests