File IO Help

Monkey Forums/Monkey Programming/File IO Help

PoliteProgrammer(Posted 2013) [#1]
I'm not sure if I'm being completely stupid here, but I'm having some trouble with File IO and I'm hoping someone can briefly explain how to do it. Basically, I have a text file in my .data folder that I want to read through line-by-line. I'm targeting Flash, by the way, in case that makes a difference.

Would someone be able to knock up a quick self-contained example of how this is supposed to be done in Monkey? Thanks in advance!


Midimaster(Posted 2013) [#2]
does your text file has the Extension "*.TXT"? Thats important

here is a sample code:

Strict
Import mojo

Class Game Extends App

	Global T$,Lines$[]

	Method OnCreate%()
		SetUpdateRate 60
			T=LoadString("MyText.txt")
			Lines=T.Split("~n")
		Return 0
	End	

	Method OnUpdate%()
		If KeyHit(KEY_ESCAPE) Then Error ""
		Return 0
	End	

	Method OnRender%()
		Cls 0,0,0
		SetColor 255,255,255
		DrawText T,100,100
		SetColor 255,255,0
		For Local i%=0 To Lines.Length()-1
			DrawText Lines[i],100,150+i*20
		Next
		Return 0
	End	
	
End

Function Main%()
	New Game
	Return 0
End






PoliteProgrammer(Posted 2013) [#3]
T=LoadString("MyText.txt")
Lines=T.Split("~n")

That... is an incredibly beautiful way to do it! Gosh, I love Monkey! :)

Also, it seems that, despite being a text file, not have a .txt extension was the problem. Thanks very much, this totally solved my problem!