Hi,
I haven't posted here in a while. Truth is, I'm an avid player of Lord of The Rings Online, and most of my free time has been spent on that instead of trying to wrap my head around DarkBasic (!)
Now that that is out of the way.. I am running into the problem in the thread title (Array does not exist or out of bounds....) when I compile (successfully) and then run the code I am testing out.
This is TDK's scrolling text box code, he so kindly provided to me with detailed comments. Since I was in the mood to try and fuss with DB and see if I could accomplish my goal of getting the basic text rendering of my game complete, I copied his code by hand, only changing some variable names and writing in my own personalized comments.
As far as I know, I don't have any naming errors, and changed everything over completely, nor do I have any naming conflicts? But I get this error when I run the program. It points to line #113 in my dedicated include file for custom Functions, but the code on that line is exactly the same except for the changes in variables, and also my removal of specifying a "Window Number" in every piece of code I could find it (since I'm only working with 1 text window realistically).
I don't understand why the error is occuring, but was hoping someone (or TDK himself) might be able to spot an easily overlooked error on my part.
As I was just testing things and playing around with ideas, I also split the original function into 2 separate functions - but I am pretty sure I did a quick re-combine of my original code into a single function, and still got the same error... so I'm not sure that splitting the function in half was the problem (or maybe the way I did it?).
edit: sorry the post is messed up..I am adding the appropriate code now
Here is TDK's original commented code he gave to me.
Dim Display$(100,6): Rem Array required for function
Dim ArrayPointer(0,6): Rem Array required for function
Dim SplitLine$(10,6): Rem Array required for function
WindowNumber = 1
Ink RGB(0,255,0),0
For N = 0 To 11
Text 5,N*15+122,"Display$("+Str$(N)+",1) >>"
Next N
For N = 1 To 15
NewLine$ = "This is line "+Str$(N)+"."
ScrollPrint(WindowNumber,150,120,320,200,NewLine$,RGB(155,0,0),RGB(255,255,255),1)
Wait 500
Next N
Text 0,0,"ArrayPointer() Now Pointing to line: "+Str$(ArrayPointer(0,WindowNumber))
Wait Key
End
Function ScrollPrint(WindowNum,XPos,YPos,ScrollBoxWidth,ScrollBoxHeight,NewLine$,BColour,TColour,Border)
Rem ******************************************************************************************
Rem Note: The following 3 Dim lines MUST be included at the start of your program
Rem before calling the function or it will result in an eror.
Rem
Rem Dim Display$(100,6): Rem Array required for function
Rem Dim ArrayPointer(0,6): Rem Array required for function
Rem Dim SplitLine$(10,6): Rem Array required for function
Rem
Rem ******************************************************************************************
Rem Basic Principle:
Rem NewLine$ is scanned and each character added to ThisLine$ one at a time. When ThisLine$ is wider than the size
Rem of the scrolling box, it is trimmed back to the last space position and placed into an array. The text placed into
Rem the array is then removed from the beginning of NewLine$ and scanning continues.
Rem Each lines which fits into the scrolling box is stored in the array SplitLine$(L,W) where L is the line number
Rem and W is the window number. LinesToAdd is the number of resulting lines (and array elements) to display.
Rem XPos, YPos - Top left screen co-ord of required scrolling area
Rem ScrollBoxWidth, ScrollBoxHeight - Width and height of equired scrolling area box
Rem NewLine$ - Line of text sent to function
Rem BColour, TColour - Required background and text colour
Rem Border - Border flag, 0 = No border, 1 = Border
TH = Text Height("A"): Rem Get height of text in current font
Numlines = (ScrollBoxHeight/TH)-2: Rem Calculate how many lines will fit inside the required scrolling box
LinesToAdd = 1: Rem default number of lines to add (changed later if required)
If NewLine$ <> "": Rem If string contains something
LineWidth = Text Width(NewLine$): NumChars = Len(NewLine$): Rem Get length of string in pixels and characters
If LineWidth > ScrollBoxWidth-10: Rem If the pixel width of the text string is wider than the right margin
Rem String to output to screen is ThisLine$, so set it initialise it to empty ("") along with counting variables
ThisLine$ = "": CharCount = 0: TotalCharCount = 0
Repeat: Rem Loop through NewLine$
Inc CharCount: Inc TotalCharCount: Rem Set the current char number in NewLine$
ThisChar$ = Mid$(NewLine$,CharCount): Rem Grab the next char in NewLine$
If ThisChar$ = " " Then LastSpace = CharCount: Rem If it's a space then store character number
ThisLine$ = ThisLine$+ThisChar$: Rem Add the current char to the end of ThisLine$
ThisLineLen = Text Width(ThisLine$): Rem Get the current length of the string we are building
If ThisLineLen > ScrollBoxWidth-10: Rem If it's wider than the right margin, then split the line
SplitLine$(LinesToAdd,WindowNum) = Left$(ThisLine$,LastSpace): Rem Place current line into array SplitLine$()
NewLine$ = Right$(NewLine$, Len(NewLine$) - Len( SplitLine$(LinesToAdd,WindowNum)))
Rem NewLine$ now contains all characters *after* the split point
Inc LinesToAdd: Rem Increment the number of lines to add to the text box
CharCount = 0: ThisLine$ = "": ThisLineLen = 0: Rem Reset the variables to build the next line
Endif
Until Text Width(NewLine$) < ScrollBoxWidth-10
Rem We've now finished running through NewLine$ so whatever left gets put into the array SplitLine$(
SplitLine$(LinesToAdd,WindowNum) = NewLine$
Endif
Rem At this point, if NewLine$ was split into three lines, for window 1 they would be in SplitLine$(1,1),
Rem SplitLine$(2,1) and SplitLine$(3,1)
Rem *** Sorting The Lines Into Arrays ***
Rem Each line of the scrolling box is in the array Display$() and ArrayPointer() points to the next empty line
Rem Numlines is the number of lines that will fit into the box
If LinesToAdd = 1: Rem Only one line to add
If ArrayPointer(0,WindowNum) < Numlines+1: Rem If adding this line will stay inside box
Display$(ArrayPointer(0,WindowNum),WindowNum) = NewLine$: Rem Set line being pointed at to NewLine$
ArrayPointer(0,WindowNum) = ArrayPointer(0,WindowNum)+1: Rem Increment the pointer to next line
Else: Rem Not enough room to add another line so scroll all lines up one
Display$(ArrayPointer(0,WindowNum),WindowNum) = NewLine$: Rem Set line being pointed at to NewLine$
For N = 0 To Numlines
Display$(N,WindowNum) = Display$(N+1,WindowNum): Rem Scroll all lines up one so last line fits at bottom
Next N
Endif
Else: Rem More than one line to add
If ArrayPointer(0,WindowNum)+LinesToAdd < Numlines+1: Rem If adding 'LinesToAdd' lines will stay inside box
For N=0 To LinesToAdd-1
Display$(ArrayPointer(0,WindowNum)+N,WindowNum) = SplitLine$(N+1,WindowNum): Rem Add Lines To End Of Display$() Array
Next N
ArrayPointer(0,WindowNum) = ArrayPointer(0,WindowNum)+LinesToAdd: Rem Increment the pointer to next line
Else: Rem Adding 'LinesToAdd' lines will NOT stay inside box, so scrolling required
For N=0 To LinesToAdd-1
Display$(ArrayPointer(0,WindowNum)+N,WindowNum) = SplitLine$(N+1,WindowNum): Rem Add Lines To End Of Display$() Array
Next N
LinesToScroll = LinesToAdd-(NumLines-ArrayPointer(0,WindowNum))-1: Rem Calculate number of lines to scroll up
For N = 0 To Numlines+LinesToAdd
Display$(N,WindowNum) = Display$(N+LinesToScroll,WindowNum): Rem Scroll them
Next N
ArrayPointer(0,WindowNum) = ArrayPointer(0,WindowNum)+LinesToAdd: Rem Set the pointer to next usable line
If ArrayPointer(0,WindowNum) > Numlines+1 Then ArrayPointer(0,WindowNum) = Numlines+1: Rem stop pointer going too far
Endif
Endif
Rem *** Printing The Text In The Display$() Array To The Box ***
If Border = 1
Rem If Border Flag Turned On, Then Draw A White Border Around Text Area
Ink RGB(255,255,255),0
Box XPos,YPos,XPos+ScrollBoxWidth-1,YPos+ScrollBoxHeight-1
Endif
Ink BColour,0: Rem Set Box Background Colour
Box XPos+1,YPos+1,XPos+ScrollBoxWidth-2,YPos+ScrollBoxHeight-2: Rem Draw Box
Ink TColour,0: Rem Set Text Colour
For Y = 0 To NumLines: Rem Print Lines Of Text - The Contents Of Display$()
Text XPos+4,Y*TH+YPos+2,Display$(Y,WindowNum)
Next Y
Endif
EndFunction
Here is my main source file
Rem ***** Main Source File *****
`Declare Textbox variables
Dim ScrnBuffer$(100)
Dim ScrnBuffer_Pointer(0)
Dim SplitLines$(10)
BGColor = RGB(150,0,0)
TXColor = RGB(255,255,0)
XPos=0
YPos=0
BoxWidth=640
BoxHeight=320
`Initialize Display Window
Set Display Mode 1024,768,32
Set Window On : Sync On : Sync Rate 60 : Sync
`Center the main window on the users dekstop
Desktop_W = DESKTOP WIDTH() : Desktop_H = DESKTOP HEIGHT()
Window_W = 1024 : Window_H = 768
Set Window Position (Desktop_W/2)-(Window_W/2),(Desktop_H/2)-(Window_H/2)
Display(XPos,YPos,BoxWidth,BoxHeight,BGColor,TXColor,1)
sync
For N = 1 To 15
NewText$ = "This is linefdfdfdfafaf fdfdffdfd fasdfasdfasfsadf sfasfasf safsafsafasfasf sfsfsf sfsfsafa "+Str$(N)+"."
Display_Update(NewText$)
sync
Wait 500
Next N
Sync
wait key
end
Here is my functions include file
Rem ***** Included Source File *****
``````````````````````````
` Screen Buffer Function `
``````````````````````````
`This function displays all the text that is held
`In the ScrnBuffer$ Array and will work with an
`Update function to scroll the buffer as needed
`Once the initial lines are filled and the display
`Area needs to be scrolled
Function Display(XPos,YPos,BoxWidth,BoxHeight,BGColor,TXColor,Border)
TH = Text Height("A") `Set the line height based on current font.
Numlines = (BoxHeight/TH)-2: `Number of lines that can fit in box, based on each lines height
If Border = 1
Ink RGB(255,255,255),0
Box XPos,YPos,Xpos+BoxWidth-1,YPos+BoxHeight-1
Endif
Ink BGColor,0
Box XPos+1,YPos+1,XPos+BoxWidth-2,YPos+BoxHeight-2
Ink TXColor,0
For Y = 0 To NumLines:
Text XPos+4,Y*TH+YPos,ScrnBuffer$(Y)
Next Y
EndFunction
```````````````````````````````
`Screen Buffer Update Function`
```````````````````````````````
`This function will push updates to the screen buffer
`It will have to check for line width constraints and
`Split incoming strings accordingly, then print them
`Out neatly in the correct order, as well as pop items
`Into and out of the queue as needed and appropriately.
Function Display_Update(NewText$)
LinesToAdd=1: `Default number of lines to add when splitting
`If NewText$ contains data to be printed
If NewText$ <> "":
`Get length of the line of text
LineWidth = Text Width(NewLine$): NumChars = Len(NewLine$):
`If the line won't fit within the text box margins
If LineWidth > BoxWidth-10:
`Line to send to box is CurrentLine$, so initialize it as empty for now
`and initialize counting variables to help with splitting the line
CurrentLine$ = "": CharacterCount = 0: TotalCharacterCount = 0:
`Now we are going to loop through NewText$ to count its characters
`and split it on the last possible space between two words.
Repeat: `Start of looping process
`Set the starting character (1 character)
Inc CharacterCount: Inc TotalCharacterCount:
`Move to the next character in the input line
CurrentCharacter$ = Mid$(NewText$,CharacterCount):
`If the current character is a space, then store that character number
If CurrentCharacter$ = " " Then LastSpace = CharacterCount:
`Add this character to the end of CurrentLine$
CurrentLine$ = CurrentLine$+CurrentCharacter$:
`Get the width of the current line of text we're working on
CurrentLineLength = Text Width(CurrentLine$):
`Check to see if the line we are working on is too big to fit in the box
If CurrentLineLength > BoxWidth-10:
`If the current line we're working on won't fit; split it
`and put it into an array to hold our split lines
SplitLines$(LinesToAdd) = Left$(CurrentLine$,LastSpace):
`Now set NewText$ to contain all characters AFTER the split point
NewText$ = Right$(NewText$, Len(NewText$) - Len( SplitLines$(LinesToAdd)))
`Increment LinesToAdd
Inc LinesToAdd:
`Reset our counting variables to deal with the next line
CharCount = 0: CurrentLine$ = "": CurrentLineLength = 0:
EndIf
Until Text Width(NewLine$) < ScrollBoxWidth-10
`We're finished with the new line, so whatever is left gets put into SplitLines$
SplitLines$(LinesToAdd) = NewText$
EndIf
`Now we begins the process of moving the lines into the ScrnBuffer$ array
If LinesToAdd = 1: `If there is only 1 line to add
`If adding this line will stay within the box
If ScrnBuffer_Pointer(0) < Numlines+1:
`Set the line bing pointed at to NewText$
ScrnBuffer$(ScrnBuffer_Pointer(0)) = NewText$:
`Increment pointer to the next line
ScrnBuffer_Pointer(0) = ScrnBuffer_Pointer(0)+1:
Else:
`If not enough room to add a line, scroll the buffer to make room
ScrnBuffer$(ScrnBuffer_Pointer(0)) = NewText$:
For N = 0 to Numlines:
ScrnBuffer$(N) = ScrnBuffer$(N+1):
Next N
EndIf
Else: `If more than one line to add
`If adding 'LinesToAdd' lines will stay inside the box
If ScrnBuffer_Pointer(0)+LinesToAdd < NumLines+1:
For N = 0 to LinesToAdd-1
`Add the lines to the end of the ScrnBuffer$ Array
ScrnBuffer$(ScrnBuffer_Pointer(0)+N) = SplitLines$(N+1):
Next N
`Increment pointer to next line
ScrnBuffer_Pointer(0) = ScrnBuffer_Pointer(0)+LinesToAdd:
Else: `If adding 'LinesToAdd' will NOT stay in the box, scrolling required.
For N = 0 to LinesToAdd - 1
ScrnBuffer$(ScrnBuffer_Pointer(0)+N) = SplitLines$(N+1):
Next N
`Calculate lines to scroll up
LinesToScroll = LinesToAdd-(NumLines-ScrnBuffer_Pointer(0))-1:
For N = 0 To Numlines+LinesToAdd
`Scroll them
ScrnBuffer$(N) = ScrnBuffer$(N+LinesToScroll):
Next N
`Set the pointer to the next useable line
ScrnBuffer_Pointer(0) = ScrnBuffer_Pointer(0)+LinesToAdd:
`Stop the pointer from going too far
If ScrnBuffer_Pointer(0) > Numlines+1 Then ScrnBuffer_Pointer(0) = Numlines+1:
EndIf
EndIf
EndIf
EndFunction
The board screwed up a little of the formatting (indentations) but that's about it.