whats wrong with my code? NOOB

Discuss using and improving Lua and the Lua Player specific to the PSP.

Moderators: Shine, Insert_witty_name

Post Reply
darklitch
Posts: 11
Joined: Mon Oct 03, 2005 9:55 am
Contact:

whats wrong with my code? NOOB

Post by darklitch »

I just started the LUA thing about a month ago and am still getting the hang of it. When I execute this code: (I know its long)

Code: Select all

black = Color.new(0, 0, 0)
white = Color.new(255, 255, 255)

newcwd = "ms0:/"
oldpad = Controls.read()
number=1

function openfile()

	text = {}
	i=1
	cheatfile = number .. ".txt"
	start=0
	startx=0
	cross=0
	mode1=1
	mode2=0
	exit2=false

	file = io.open(cheatfile,"r")
	line=file:read("*l")

	while line~=nil do
		text[i]=line
		i=i+1
		line=file:read("*l")
	end

	if i>35 then
		k=35
	else
		k=i
	end
end


function textreader()

	screen:clear(Color.new(0, 0, 0))

	openfile()

	while exit2==false do
		pad = Controls.read()
		if pad:select() then
			exit2=true
		end
		if pad:cross() then
			cross=10
		elseif pad:circle() then
			cross=34
		else
			cross=1
		end

		if pad:down() then
				start=start+cross
				if i<k+start then
					start=i-k
				end
		elseif pad&#58;up&#40;&#41; then
				start=start-cross
				if start<0 then
					start=0
				end
		end

		if pad&#58;right&#40;&#41; then
				startx=startx+cross
		elseif pad&#58;left&#40;&#41; then
				startx=startx-cross
				if startx<0 then
					startx=0
				end
		end

		if oldpad ~= pad then
				if pad&#58;r&#40;&#41; then
						number = number + 1
						if number > 3 then
							number=1
						end
						openfile&#40;&#41;
				elseif pad&#58;l&#40;&#41; then
						number = number - 1
							if number < 1 then
								number=3
							end
						openfile&#40;&#41;
				end
		oldpad = pad
		end

		screen&#58;clear&#40;white&#41;

		for j = 1,k-1 do
			if mode2==0 then
				screen&#58;print&#40;0, 8*&#40;j-1&#41;, string.format&#40;"%s",string.sub&#40;text&#91;j+start&#93;,startx,&#40;string.len&#40;text&#91;j+start&#93;&#41;-mode1&#41;&#41;&#41;, black&#41;
			else
				screen&#58;print&#40;0, 8*&#40;j-1&#41;, string.format&#40;"%04d",j+start&#41;, black&#41;
				screen&#58;print&#40;36, 8*&#40;j-1&#41;, string.format&#40;"%s",string.sub&#40;text&#91;j+start&#93;,startx,&#40;string.len&#40;text&#91;j+start&#93;&#41;-mode1&#41;&#41;&#41;, black&#41;
			end
		end

		screen.waitVblankStart&#40;&#41;
		screen.flip&#40;&#41;
		screen.waitVblankStart&#40;3&#41;

	end
end

while true do
	textreader&#40;&#41;
end
It works fine at first. Once I press the r or l trigger 10 times, i get an error. The error is ":21: attempt to index global 'file' (a nil value)" It is always 10 times. This makes no since. If you execute it, you must make 3 files name 1.txt 2.txt and 3.txt and put random text in it for a test.

thanks in advance
darklitch
chaos
Posts: 135
Joined: Sun Apr 10, 2005 5:05 pm

Post by chaos »

this is probably the 4th topic on this error.. basically, you need to close your files when you're done reading from them. if you have more than 10 files open, lua will crash..
Chaosmachine Studios: High Quality Homebrew.
darklitch
Posts: 11
Joined: Mon Oct 03, 2005 9:55 am
Contact:

Post by darklitch »

wow I would never have thought of that.
Post Reply