Page 1 of 1

Need help with in game dialog code

Posted: Thu Sep 22, 2016 6:23 pm
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.

Re: Need help with in game dialog code

Posted: Thu Sep 22, 2016 6:39 pm
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

Re: Need help with in game dialog code

Posted: Thu Sep 22, 2016 7:01 pm
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.

Re: Need help with in game dialog code

Posted: Fri Sep 23, 2016 12:49 am
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!

Re: Need help with in game dialog code

Posted: Fri Sep 23, 2016 9:48 pm
by kicknbritt
well I just got a job working on a website so it might take a bit ... lol