how do you read a single line from a text file
I'm making a 3d engine and I want to read an obj file as text (but that's a seperate problem) the problem i'm having is trying to distinguish the lines. as you may know if you've worked with 3d engines before, a line of obj defining a vertecie looks like:
v 0.0625 0.125 0.1875
v 0.0625 0.125 -0.1875
or something like that.
the code I want to use looks something like:
for i in obj.textline //don't know what is.
if i[0] = "v" then
local yes = 2
local verteciex = ""
local verteciey = ""
local verteciez = ""
while i[yes] != " " //space
verteciex = verteciex + i[yes]
yes += 1
end
yes += 1
while i[yes] != " " //space
verteciey = verteciey + i[yes]
yes += 1
end
yes += 1
while i[yes] != " " //space
verteciez = verteciez + i[yes]
yes += 1
end
vertecies.push([verteciex, vertecie2, vertecie3]) //should push [[0.0625, 0.125, 0.1875], [0.0625, -0.125, 0.1875]]
end
end
so does anyone know how to find: the individual line of a text document?
you could use:
lines = text.split("\n")
to turn your text into a list of lines
You can format source code you show here by surrounding it with tripple ```
in first and last line.
Regarding your question ... as Gilles already said :)
Comments to your code:
if i[0] = "v" then
should be if i[0] == "v" then
, you need to use double ==
to check for equality.
Also:
vertecies.push([verteciex, vertecie2, vertecie3])
, I suspect this should be vertecies.push([verteciex, verteciey, verteciez])
Anyway, I found it interesting, so here is a little example. I saved cube.obj as cube.txt into the asset folder.
The source:
init = function()
asset_manager.loadText("cube",
function(data)
myText = data // here we receive the text file
myArray = myText.split("\n") // and split it into multiple lines (using the newline special character)
myVertices = readVertices(myArray) // a sub routine that looks for lines that start with 'v'
// just for testing, remove the print loop... if not needed
for i in myVertices print(i) end
end)
end
update = function()
end
draw = function()
end
// helper functions
readVertices = function(array)
local dataList = []
for i in array
local data = i.split(" ") // split the line at double SPACE
if data[0]=="v" then
verticeX = data[1]*1 // ..*1 converts a string to a number
verticeY = data[2]*1 // remove it if you want it as string
verticeZ = data[3]*1
dataList.push([verticeX,verticeY,verticeZ])
end
end
return dataList
end
The link:
https://microstudio.io/i/TinkerSmith/exampleparsetext/
Hope this helps :)
Live Long and Tinker
P.S. ... you know that you can use the Babylon 3D extension in microStudio?