Jump to content




conditions help


  • You cannot reply to this topic
3 replies to this topic

#1 gril002

  • Members
  • 38 posts

Posted 01 June 2016 - 01: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 by gril002, 01 June 2016 - 01:27 PM.


#2 gril002

  • Members
  • 38 posts

Posted 01 June 2016 - 01: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

#3 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 01 June 2016 - 02: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


#4 gril002

  • Members
  • 38 posts

Posted 01 June 2016 - 02:39 PM

ah thanks for the help





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users