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

conditions help

Started by gril002, 01 June 2016 - 12:11 PM
gril002 #1
Posted 01 June 2016 - 02:12 PM
So I have a E-Mail GUI. Each mail is 2 lines so I want it so that when you click on eihter of these lines it opens the same email. My problem is that it always opens the first mail. I get the Y line in a seperate function and is returned as "linePos" and openMail is the number of the mail that should be opened. Now as you can see I am printing them both on at the bottom and the outcome is: linePos as is should be (1-20) but openMail is always 1

Heres the code:


while true do
returnButton()
nextButton()
openMail = nil
if linePos == 2 then
sendMail()
elseif linePos == 3 or 4 then
openMail = 1
elseif linePos == 5 or 6 then
openMail = 2
elseif linePos == 7 or 8 then
openMail = 3
elseif linePos == 9 or 10 then
openMail = 4
elseif linePos == 11 or 12 then
openMail = 5
elseif linePos == 13 or 14 then
openMail = 6
elseif linePos == 15 or 16 then
openMail = 7
elseif linePos == 17 or 18 then
openMail = 8
elseif pressedButton == 7 then
pressedButton = nil
interface()
return
else openMail = nil
end
term.setCursorPos(1,16)
print(linePos)
print(openMail)
sleep(1)
OpenMail()
end
Edited on 01 June 2016 - 12:27 PM
gril002 #2
Posted 01 June 2016 - 02:28 PM
The check for line 2 is the only one that works. Even thogh I have: else openMail = nil, it still sets it to 1 even when the line is 20 for example
Bomb Bloke #3
Posted 01 June 2016 - 03:04 PM
When writing conditional statements, everything boils down to true or false. If you stick a value in there that isn't specifically true or false, then nil counts as false and everything else counts as true.

With that in mind, this sort of thing:

elseif linePos == 3 or 4 then

… is read as:

elseif (linePos == 3) or (4 is not nil) then

… which is always going to be true, so linePos is always going to be set to 1.

You meant:

elseif linePos == 3 or linePos == 4 then

But really it'd be a bit better to apply a little math:

while true do
	returnButton()
	nextButton()
	openMail = nil
	
	if linePos == 2 then
		sendMail()
	elseif linePos > 2 and linePos < 19 then
		openMail = math.floor((linePos - 1) / 2)
	elseif pressedButton == 7 then
		pressedButton = nil
		interface()
		return
	else openMail = nil end
	
	term.setCursorPos(1,16)
	print(linePos)
	print(openMail)
	sleep(1)
	OpenMail()
end
gril002 #4
Posted 01 June 2016 - 03:39 PM
ah thanks for the help