Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Can't solve infinite loop

Author
Message
Neco
18
Years of Service
User Offline
Joined: 13th Jul 2008
Location: Waterloo, Wisconsin USA
Posted: 22nd Sep 2010 21:37 Edited at: 22nd Sep 2010 23:19
Edit: I just included the function code.. But I can provide more if needed - this also uses a Styx function. (INSTR) I think it can be substituted with Matrix Utilities function of a similar (or same?) name. But I was hoping the problem was in the syntax itself and not so much the code, and more code wasn't needed. (apoligies if it is - I didn't want to paste the whole project)

This pertains to my scrolling text box function.. Been studying TDK's example code for quite a while.. However I went a different route when it actually came to splitting a line longer than the width of the text box.

I tested the individual commands for splitting and displaying both lines and they seemed to work fine.. But when I try to wrap it into a function I get an infite loop. I don't know why and I can't solve the problem. I've been trying for 3 days, and rewritten it various different ways.

My routine uses a lot less code, and doesn't loop through every single letter (which were the goals) but I'm obviously doing something wrong that I cannot see for myself.

It's definitely something inbetween the Repeat claus, as I stepped through the program and thats where it seemed to be hanging up.

Note: It's not because the pixels space it starts searching for spaces from, is too short (and no spaces occur) increased it drastically and it still kept looping.. I swapped it out for a standard Columns = XX integer, and compared it against Len(NewLine$) instead of Text Width and the problem still persisted.




Sven B
21
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 22nd Sep 2010 22:47
Why do you use INSTR(NewString$,chr$(32),Box_W-10)? I doubt a string command will use pixels instead of characters...

I'll see if this changes anything. You're code isn't that easy to read. I get quickly tired of looking to non-uniformly indented code (no offense).

Cheers!
Sven B

Neco
18
Years of Service
User Offline
Joined: 13th Jul 2008
Location: Waterloo, Wisconsin USA
Posted: 22nd Sep 2010 23:16 Edited at: 22nd Sep 2010 23:22
Thank you for pointing out that (seemingly) error.
I will also see if playing with that does anything.


However, I think my code indentation is consistent and pretty well readable, thank you very much. It's not like I do this for a living

Perhaps you are talking about line spacing. in which case, I won't argue I should perhaps put some white space in there.. Or perhaps you mean the comment lines? I did this on the fly so it wasn't really my highest priority. I will fix those.

edit: I replaced that Bow_W variable with a plain integer - nothing changed

Sven B
21
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 22nd Sep 2010 23:47 Edited at: 22nd Sep 2010 23:51
Quote: "However, I think my code indentation is consistent and pretty well readable, thank you very much. It's not like I do this for a living"


Sorry, I meant something like this


But it's just personal preference. I'm well aware of that ^^. I'm pretty picky on my code layout compared to other programmers.

Looking at my personal preference indented code , I still think that this line
SplitLine = INSTR(NewString$,chr$(32),Box_W-10)
is the part making trouble. The rest seems perfectly logical to me. I would suggest something along the lines of (improvised):


Cheers!
Sven B

sladeiw
17
Years of Service
User Offline
Joined: 16th May 2009
Location: UK
Posted: 22nd Sep 2010 23:51 Edited at: 22nd Sep 2010 23:53
I would trace all the variables that affect the exit condition, mainly splitline, text width of newstring$ and box_w. See how each loop affects these variables with a test string.

What happens if no spaces are found in newstring? splitline will be zero and newstring$ will never reduce, and therefore the until will never return true.

I think sven was commenting that some parts of your code are indented by multiple spaces, some only by 1 space and some not at all. When you read through consistently indented code you can instantly see basic code logic and structure. It helps a lot!

edit: As sven has just demonstrated lol
Neco
18
Years of Service
User Offline
Joined: 13th Jul 2008
Location: Waterloo, Wisconsin USA
Posted: 22nd Sep 2010 23:57
I solved the problem!

First, the problem had to do with creating the right half of the string.. Don't quite understand fully why the changes I made worked (I was just changing stuff rapidly as ideas came) but I'm sure I will after I sit there and look at it..

I also switched around some string variables as well.. not sure how that affected it.. would like to get it all in the same variable (or at least a maximum of two) if possible..

and I also forgot to put another scroll/print loop underneath the UNTIL to print the final line if there was any leftover that didn't trigger the splitting routine.

Here is the changed code.. Most of the changes involve using a static "Columns" declaration to control the end of line length. I may try to rewrite it to go off the Scroll Box Width, like TDK's does (I probably screwed up a process he used and that led to my problem) so that it takes the guess work out when it comes to column length..

After that I just have to stick in a quick routine to strip any white space off the beginning of new lines (where it splits as a new sentence begins... because I double space on a new sentence in the middle of a paragraph like most people should :p )



Neco
18
Years of Service
User Offline
Joined: 13th Jul 2008
Location: Waterloo, Wisconsin USA
Posted: 23rd Sep 2010 00:02
Looks like we all replied at near the same time

I see what you are talking about Sven.. I had thought it wasn't necesarry to go that deep with it all, as its executed for the most part, in logical line order.. But I see what you mean either way.


Also.. I'm using the "["code"]" board tag and it doesn't highlight like yours did? What are you doing differently?

Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 23rd Sep 2010 00:20
I would have joined in but I got stuck in the infinite loop.

And now it's all over.
Grog Grueslayer
Valued Member
21
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 23rd Sep 2010 00:29 Edited at: 23rd Sep 2010 00:43
Quote: "Thank you for pointing out that (seemingly) error.
I will also see if playing with that does anything."


Yeah that part didn't help because you were looking at the text length which is the number of characters in the string not the text width which is the horizontal pixel size of the text.

I tried to work with that code but ultimately I deemed it flawed and made a new one from scratch. Instead of finding out where the space is and extracting the word I just used IanMs SPLIT STRING command to extract all of the words in the string using space as a delimiter. It goes though each split word checking if it adds the TEXT WIDTH() of the next word to the current TEXT WIDTH() of the end buffer string if it would go over Box_W. If it doesn't it adds the next word to the end buffer string. If it would go over it moves the array up and makes the end buffer string the word that wouldn't fit in the last line.

I used a different method to scroll the array instead of a FOR/NEXT loop redefining the strings. The easiest way to do it is to use ARRAY DELETE ELEMENT to remove element zero in the array and thus the rest of the array moves up naturally. Add an ARRAY INSERT AT BOTTOM and you've done the same thing as a FOR/NEXT loop looping 20 times but in a much faster way.



Edit: Woah, 5 posts since I started writing this message!

Sven B
21
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 23rd Sep 2010 00:37 Edited at: 23rd Sep 2010 00:38
Quote: "Also.. I'm using the "["code"]" board tag and it doesn't highlight like yours did? What are you doing differently?"


Use "["code lang=dbp"]" ^^ (I think dbp has to be lower case and no quotes around it). I read about it once in a newsletter.

Cheers!
Sven B

IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 23rd Sep 2010 01:17
@Grog,
My biggest fan!

I would suggest doing something to the input string to remove duplicate spaces, unless you don't mind duplicate spaces in the output.


Also, you might want to replace the delete/insert pair on the array with the ROTATE ARRAY command (the first form of the command does what you want). This avoids unnecessary memory allocation caused by the insert command, and scrolls the array in place.

Utility plug-ins (26-JUL-2010)
I'm applying terms of use that require you to wear a red nose and honk a horn whenever you use the Internet
Grog Grueslayer
Valued Member
21
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 23rd Sep 2010 03:37
Quote: "My biggest fan!"


That I am.

I'm ok with double spaces but Neco may not like them. I looked at your array commands to do just that but somehow missed that one. Thanks for showing me the better way.

Here's the modification:


Neco
18
Years of Service
User Offline
Joined: 13th Jul 2008
Location: Waterloo, Wisconsin USA
Posted: 23rd Sep 2010 16:21 Edited at: 23rd Sep 2010 16:28
Those are some nice ideas. I am actually thinking about adding a feature in the future that would actually scroll back up (like a history you get in most game windows) so out and out deleting the items is not possible at the moment. My Screen Buffer is only 50 lines currently (20 of which are shown), but I'll probably increase that to a few hundred and allow scrollback

I do recall in my very first attempt a long time ago, I was using a queue array or something like that... I had considered using a string splitter as well, but this is what came to me -and it works now so I can't complain about that!

IanM: I don't want to remove double spaces. As a writer I am very keen on my commas and periods being spaced correctly. I'm just horrible that way I think it will be easy enough for me to figure out how to chop some white space only after a line-wrap, at least I hope

Grog Grueslayer
Valued Member
21
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 23rd Sep 2010 19:19
Quote: "Those are some nice ideas. I am actually thinking about adding a feature in the future that would actually scroll back up (like a history you get in most game windows) so out and out deleting the items is not possible at the moment. My Screen Buffer is only 50 lines currently (20 of which are shown), but I'll probably increase that to a few hundred and allow scrollback"


You could use IanMs database commands to create a buffer file that can store all the previous text. You read from the file when the user wants to scroll back and when you want to show just the last 20 lines. You can also add that file to the players data for saves allowing them to scroll back their entire adventure and not just their current game session. If you just use the file and not memory you'd have an seemingly unlimited buffer.


Quote: "IanM: I don't want to remove double spaces. As a writer I am very keen on my commas and periods being spaced correctly."


I'm the same way. When I first got to the Darkbasic forums I was annoyed that the forums automatically remove the second space after a period. I still write (like in this message) two spaces after every period but nobody knew that because of the filter.

Neco
18
Years of Service
User Offline
Joined: 13th Jul 2008
Location: Waterloo, Wisconsin USA
Posted: 23rd Sep 2010 20:01 Edited at: 23rd Sep 2010 20:03
Haha, I hadn't even noticed the forum does that. I always double tap my space bar on new sentences anyway, so it's just second nature.

Yes I had considered using his saving and loading commands like you described as well.

For now, I am just gonna keep it simple.. Once I have a working game engine, I can worry about minor/fancy improvements to it.

Login to post a reply

Server time is: 2026-07-22 22:07:06
Your offset time is: 2026-07-22 22:07:06