This function cuts a long string into slices.
I would really like to post this in the Snippet area, but this thread seems to be closed.
longstring$="This is a very long text. I hope you can find this text useful to check this useful and crazy function. - www.online-arts.de"
do
Print(longstring$)
print(partstring(longstring$,35,1))
print(partstring(longstring$,35,2))
print(partstring(longstring$,35,3))
print(partstring(longstring$,35,4))
rem print(partstring(longstring$,10,2))
rem print(partstring(longstring$,10,3))
Sync()
loop
function partstring(string$,chars,line)
rem add a last space in the string for the function to check
string$=string$+" "
instring$=string$
for i=1 to line
rem if its not the first run through the text...
if slength>0
slengthold=slengthold+slength
instring$=right(string$,len(string$)-slengthold)
endif
rem first cut the string to the chars
cutstring$=left(instring$,chars)
slength=len(cutstring$)+1
rem find last " "
repeat
dec slength
if slength=0
exit
endif
until mid(cutstring$,slength,1)=" "
next i
rem outstring is
endstring$=left(cutstring$,slength)
endfunction endstring$
EDIT:
This code will give you a RPG style Text output, if you press the spacekey
(AGK1)
rem
rem AGK Application
rem
rem A Wizard Did It!
setVirtualResolution(1280,720)
textname$="This is a very long text. Maybe some lines at the end? For shure, this is an annoying thing. I have even more to say! Maybe this dialogue box is enough? I don't think so! Maybe this line is enough?"
text=createtext("")
settextsize(text,30)
settextcolor(text,255,255,255,255)
settextposition(text,10,10)
rem settextvisible(text,1)
allstring$=format_string(textname$,68,4)
alllen=len(allstring$)
do
if play_text=1
if charpos<alllen
charpos=charpos+1
endif
buildstring$=buildstring$+mid(allstring$,charpos,1)
settextstring(text,buildstring$)
endif
if getrawkeystate(32)=1
play_text=1
endif
Sync()
loop
rem this function can format your string on chars and lines
function format_string(instring$,maxchars,maxlines)
fullstring$=""
ls$=""
for lines=0 to maxlines
ls$=partstring(instring$,maxchars,lines)
if ls$<>""
fullstring$=fullstring$+ls$+chr(10)
endif
next lines
fullstring$=left(fullstring$,len(fullstring$)-1)
endfunction fullstring$
function partstring(string$,chars,line)
rem add a last space in the string for the function to check
string$=string$+" "
instring$=string$
for i=1 to line
rem if its not the first run through the text...
if slength>0
slengthold=slengthold+slength
instring$=right(string$,len(string$)-slengthold)
endif
rem first cut the string to the chars
cutstring$=left(instring$,chars)
slength=len(cutstring$)+1
rem find last " "
repeat
dec slength
if slength=0
exit
endif
until mid(cutstring$,slength,1)=" "
next i
rem outstring is
endstring$=left(cutstring$,slength)
endfunction endstring$
EDIT2: Full dialoge engine (spacekey to move)
rem created by alex Schmidt , online arts
rem use the spacekey to go through a random dialogue
setVirtualResolution(1280,720)
type dialog_def
name$ as string
dialogline$ as string
endtype
dim dialog[64] as dialog_def
global dialog_start as integer
type dialogbox_def
on as integer
text as integer
background as integer
button as integer
dialog$ as string
parts as integer
charpos as integer
partpos as integer
buildstring$ as string
linestring$ as string
endtype
dim dialogbox[0] as dialogbox_def
add_dialogue("firstdialogue","Dialogue 1:|Welcome to this text tester! This will allow you to test all the dialogues made inside the engine. This can help you to create own dialogues. Awesome, isn't it? |This is the first part|This is the second part|And this is the last part|...|Yes, it is!|end of dialogue 1.")
add_dialogue("anotherdialogue","Dialogue 2:|Wow! So game!|This looks quite amazing|And cruel... for the others!|...|Yes, it is!|End of dialogue 2.")
add_dialogue("third","Dialogue 3:|This is still going on. I can't remember how much time I had for this idea. Maybe a smoothie could help me out of my hole? I will try a smoothie for shure!|Do you hear me?|Fun fact: Where are you?|Answer:|In front of you computer!|Hahaha!|Haha|ha.|End of dialogue 3.")
create_dialogbox("",10,10)
do
if getrawkeystate(32)=1 and spacetilt=0
if dialogbox[0].dialog$=""
spacetilt=1
dial=random(0,2)
if dial=0 then di$="firstdialogue"
if dial=1 then di$="anotherdialogue"
if dial=2 then di$="third"
set_dialog(di$)
dialogbox[0].on=1
dialogbox[0].charpos=0
dialogbox[0].partpos=0
dialogbox[0].buildstring$=""
rem dialogbox[0].linestring$=""
else
if dialogbox[0].partpos<dialogbox[0].parts
spacetilt=1
dialogbox[0].partpos=dialogbox[0].partpos+1
dialogbox[0].on=1
dialogbox[0].charpos=0
dialogbox[0].buildstring$=""
rem dialogbox[0].linestring$=""
rem print(dialogbox[0].partpos)
else
dialogbox[0].on=0
endif
endif
endif
if spacetilt=1
if getrawkeystate(32)=0
if dialogbox[0].charpos=len(dialogbox[0].linestring$)
spacetilt=0
endif
endif
endif
handle_dialogue()
Sync()
loop
function set_dialog(name$)
for i=1 to dialog_start
if dialog[i].name$=name$
dialogbox[0].dialog$=dialog[i].dialogline$
parts=-1
repeat
inc parts
dialogbox[0].parts=parts-1
outp$=SplitString(dialogbox[0].dialog$,"|", parts)
until len(outp$)=len(dialogbox[0].dialog$)
dialogbox[0].parts=parts-1
dialogbox[0].charpos=0
dialogbox[0].partpos=0
dialogbox[0].buildstring$=""
dialogbox[0].linestring$=format_string(SplitString(dialogbox[0].dialog$,"|", dialogbox[0].partpos),68,4)
exit
endif
next i
endfunction
function handle_dialogue()
if dialogbox[0].on=0
settextvisible(dialogbox[0].text,0)
dialogbox[0].dialog$=""
endif
if dialogbox[0].on=1
settextvisible(dialogbox[0].text,1)
allstring$=format_string(SplitString(dialogbox[0].dialog$,"|", dialogbox[0].partpos),68,4)
dialogbox[0].linestring$=allstring$
alllen=len(allstring$)
if dialogbox[0].charpos<alllen
dialogbox[0].charpos=dialogbox[0].charpos+1
else
dialogbox[0].on=2
endif
dialogbox[0].buildstring$=dialogbox[0].buildstring$+mid(allstring$,dialogbox[0].charpos,1)
settextstring(dialogbox[0].text,dialogbox[0].buildstring$)
rem settextstring(dialogbox[0].text,allstring$)
rem sleep(200)
endif
endfunction
function create_dialogbox(bgname$,x,y)
text=createtext("")
settextsize(text,30)
settextcolor(text,255,255,255,255)
settextposition(text,x,y)
dialogbox[0].text=text
dialogbox[0].background=0
dialogbox[0].button=0
endfunction
function add_dialogue(dianame$,diatext$)
inc dialog_start
dialog[dialog_start].name$=dianame$
dialog[dialog_start].dialogline$=diatext$
endfunction
rem this function can format your string on chars and lines
function format_string(instring$,maxchars,maxlines)
fullstring$=""
ls$=""
for lines=0 to maxlines
ls$=partstring(instring$,maxchars,lines)
if ls$<>""
fullstring$=fullstring$+ls$+chr(10)
endif
next lines
fullstring$=left(fullstring$,len(fullstring$)-1)
endfunction fullstring$
function partstring(string$,chars,line)
rem add a last space in the string for the function to check
string$=string$+" "
instring$=string$
for i=1 to line
rem if its not the first run through the text...
if slength>0
slengthold=slengthold+slength
instring$=right(string$,len(string$)-slengthold)
endif
rem first cut the string to the chars
cutstring$=left(instring$,chars)
slength=len(cutstring$)+1
rem find last " "
repeat
dec slength
if slength=0
exit
endif
until mid(cutstring$,slength,1)=" "
next i
rem outstring is
endstring$=left(cutstring$,slength)
endfunction endstring$
rem not my function but really useful
rem this gets the amount of 'segments' that a string can be split into
rem this is based off of 1, not 0. so, if the string and delimiter you pass in
rem returns a counted valueof 3, you can get segments 0-2 from it.
function SplitStringCount(pString as string, pDelimiter as string)
delimitCount as integer
i as integer
rem get length of string
pStringLength as integer
pStringLength = len(pString)
rem parse through the string and count how many delimited chars there are
for i = 0 to pStringLength - 1
if mid(pString, i + 1, 1) = pDelimiter
delimitCount = delimitCount + 1
endif
next i
delimitCount = delimitCount + 1
endfunction delimitCount
rem this splits a string by its delimiter and will return one segment of it.
rem to see how many segments there are, use SplitStringCount.
rem NOTE: SplitString is 0-based, whereas SplitStringCount is 1-based.
rem This is so that SplitStringCount will return a total value of segments (e.g., 3)
rem and to access them in SplitString, you'll use values 0-2.
REM NOTE, if this function 'errors' out - e.g., invalid segment (negative number or too a high a number)
REM this function will return the ENTIRE string back to you.
REM if the function was successful, it will only send back a segment as the return value.
function SplitString(pString as string, pDelimiter as string, pSegment)
delimitCount as integer
myReturn as string
myReturn = ""
charCount as integer
segmentCount as integer
exitSearch as integer
i as integer
rem get the length of the original source string
pStringLength as integer
pStringLength = len(pString)
rem count how many delimiters there are in the string
for i = 0 to pStringLength - 1
if mid(pString, i + 1, 1) = pDelimiter
delimitCount = delimitCount + 1
endif
next i
rem if there are no delimiters found in the original source string
rem or there was an invalid segment number passed in (negative, too high a number)
rem return the original source string
if delimitCount = 0 or pSegment < 0 or pSegment > delimitCount
exitfunction pString
endif
rem this is only applicable to the non-first segment.
rem this finds the first character after a selected delimiter.
rem if we wanted to split up 'foo,bar' and get the second segment, this would return the position of 'b'
rem since the first letter of the first segment starts at 0, we can ignore it
if pSegment > 0
for i = 0 to pStringLength - 1
charCount = charCount + 1
if mid(pString, i + 1, 1) = pDelimiter
segmentCount = segmentCount + 1
if segmentCount = pSegment
exit
endif
endif
next i
endif
rem return an empty string
rem if the last character in the original source string was a delimiter: foo,bar,
if charCount >= pStringLength
myReturn = ""
exitfunction myReturn
endif
rem now we search through and add characters that we need to our return variable.
rem keep adding eltters until we hit a delimiter or the string ends
while exitSearch = 0 and charCount < pStringLength
if mid(pString, charCount + 1, 1) = pDelimiter
exitSearch = 1
else
myReturn = myReturn + mid(pString, charCount + 1, 1)
charCount = charCount + 1
endif
endwhile
rem return value
endfunction myReturn