This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Blue's profile picture

Exits program (without an error) [solved]

Started by Blue, 11 May 2014 - 05:57 AM
Blue #1
Posted 11 May 2014 - 07:57 AM
http://pastebin.com/gQqHpW5m

It exits the program(at line 78 and 97) and returns to the shell.I'm guessing it's a problem with the variables.
It does not error it just stops because of the if statements.
Edited on 12 May 2014 - 02:10 PM
CometWolf #2
Posted 11 May 2014 - 11:15 AM
Atleast tell us where it exits…
Blue #3
Posted 11 May 2014 - 03:43 PM
Atleast tell us where it exits…
It exits near the if statements.Sorry about that (line 78 and 97).
Edited on 11 May 2014 - 01:45 PM
Blue #4
Posted 12 May 2014 - 05:53 AM
I tried turning the local variables into global variables and it worked.Do i have to have global variables?
CometWolf #5
Posted 12 May 2014 - 07:32 AM
Provided the variable is defined in another script and you use shell.run to execute this one, then yes you do.
TheOddByte #6
Posted 12 May 2014 - 02:50 PM
Learn how to use locals properly and also learn to use elseif/else

local select -- Make this local here instead so you can use it later
while true do
	local event, button, X, Y = os.pullEvent("mouse_click")
	local XY = X..","..Y
	if XY=="1,8" then
		select="add" -- You can't localize this here if you want to use it elsewhere, because then it will be local to this 'if' statement only
		break

	elseif XY=="1,9" then -- Use elseif instead
		select="remove" -- Same here, if you localize it here you can only use it in this 'if' statement
		break
	end
end

Looking on how you use locals I'll give a short explanation with some code

while true do
	local foo = "bar"
	break
end
print( foo ) -- foo = nil since it was localized in the 'while' loop

local foo = true
if foo then
	local bar = "Hello World!"
end
print( bar ) -- bar = nil since it was localized in the 'if' statement
Edited on 12 May 2014 - 12:50 PM
Blue #7
Posted 12 May 2014 - 04:09 PM
Learn how to use locals properly and also learn to use elseif/else

local select -- Make this local here instead so you can use it later
while true do
	local event, button, X, Y = os.pullEvent("mouse_click")
	local XY = X..","..Y
	if XY=="1,8" then
		select="add" -- You can't localize this here if you want to use it elsewhere, because then it will be local to this 'if' statement only
		break

	elseif XY=="1,9" then -- Use elseif instead
		select="remove" -- Same here, if you localize it here you can only use it in this 'if' statement
		break
	end
end

Looking on how you use locals I'll give a short explanation with some code

while true do
	local foo = "bar"
	break
end
print( foo ) -- foo = nil since it was localized in the 'while' loop

local foo = true
if foo then
	local bar = "Hello World!"
end
print( bar ) -- bar = nil since it was localized in the 'if' statement
Thanks :)/>