Here's a function and a couple of usage examples for how to parse a CSV entirely in DBCode.
rem Setup environment:
autocam off
sync rate 0
sync on
main()
end
function main()
teststr as string : teststr = "the,boy,went,to,the,store,to,buy,a,basketball"
tempstr as string : tempstr = teststr
tstr as string
pstr as string
`start iterating through the csv
while tempstr <> ""
tstr = nextcsv(tempstr) `set tstr to the first variable in the csv
if tstr <> "" `check to see if we are at the end of the CSV
` we didn't get a null, so remove that variable
` from tempstr to try for the next one
tempstr = right$(tempstr, len(tempstr) - len(tstr) - 1)
else
`we hit the end, so set tstr to tempstr and wipe tempstr
`so that the while loop ends
tstr = tempstr
tempstr = ""
endif
`now put tstr into a string to be printed out to print out a sentence
pstr = pstr + " " + tstr
endwhile
do
cls
`print the sentence we put together from the CSV
print pstr
sync
loop
`wait key
endfunction
function nextcsv(str as string)
`declare our variables
returnstr as string: returnstr = "" `default of returnstr should be null incase we dont' find anything
`start a loop
for i = 1 to len(str) `we're going to be checking every character in the string until we find a comma
if mid$(str, i) = "," `did we find a comma?
returnstr = left$(str, i - 1) `set the return string to everything up to that comma we just found
i = len(str) `a little hack to ensure that the for loop exits properly
endif
next i
endfunction returnstr
Next is how you might use a CSV to specify loading an object. This could be useful for producing a primitive, linear script, or used with Unity's message queue from lua scripts.
Have to warn you though, I wrote this next one on the editor for posting this thread, so it's untested. If it doesn't work, it shouldn't take too much work to get it working.
objnum as dword
moveamount as float
if nextcsv(str) = "loadobject" `is the command 'moveobject'?
`it was loadobject, so now we go and parse the rest accordingly
str = right$(str, len("loadobject") - 1) `get rid of loadobject from the csv
objfilename$ = nextcsv(str) `the next variable should be the file name
str = right$(str, len(objfilename$) - 1)
objnumber# = int(val(nextcsv(str))) `next should be object number
load object objfilename$, objnumber#
endif
Nothing I say is intended to be rude. My autism means that I do not know what is rude and what isn't rude. I apologize if I seem rude. It is not my intention.