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

[Lua][Question] Issue with if statement

Started by Deadlystrike, 20 August 2012 - 02:13 PM
Deadlystrike #1
Posted 20 August 2012 - 04:13 PM
Maby it would be easyer for you to help if i simply restated what im trying to accomplish

A program that has multiple If statements Like this


if diskispresent then
-does things
if diskisnotpresent then
-does things

That alone is easy to do, and i know i would use a elseif. However what im trying to accomplish is "Assuming that that program is still the one runing" if at anytime the If statement that is true nolonger becomes true and another one does it will go back and do the other instead. (i.e: When program was started a disk was present thus it does the things that the first if tells it to do, then midprogram the disk is taken out the program would stop what its doing and do what the ifdiskisnotpresent says to do.


Here is my code

-- Settings
side = "bottom" --location of diskdrive
pass = "4"
-- Main Program
shell.run ("clear")
print("-------------------------")
print("=   Base Terminal Link  =")
print("-------------------------")

if disk.isPresent(side) == true
if fs.exists("disk/level") then
file = io.open("disk/level", "r")
dlevel = file:read()
if dlevel >= pass then
shell.run ("clear")
shell.run ("menu")

elseif disk.isPresent(side) == false then
input = read()
if input == hack then
shell.run("hack")
end
end
end
end
Currently it checks if the disk is present or not at the time of that program initially launching. And if its not active it will await input as told to and if i put the disk in after launching the program it will stay as false.
However what i want is as long is this program is active it will constantly check if a disk is present or not and if true it does the top thing and if false it does the bottom thing. And as long as this program is still running it will keep checking and updating
Graypup #2
Posted 24 August 2012 - 01:39 AM
rewrite it. lua includes "and" for use in if statements. also, repeat <code> until <if condition> end
Deadlystrike #3
Posted 24 August 2012 - 04:54 PM
any other help?
dual★moon #4
Posted 24 August 2012 - 04:55 PM
Not to undercut Graypup's excellent advice but, what you're wanting to do is something like the following:


-- we will call this our "main loop"
while true do
  if disk.isPresent(side) and fs.exists("disk/level") then
	file = io.open("disk/level", "r")
	dlevel = file:read()
	if dlevel >= pass then
	  shell.run("clear")
	  shell.run("menu")
	end
  else -- if disk.isPresent(side) is not true as per above we know it is false, no need to check condition.
	input = read()
	if input == "hack" -- do not forget that a string outside of quotes is a variable name
	  shell.run("hack")
	end
  end
end

I didn't actually run this script so check on your own for errors, but this general concept should be close to what you want.
Deadlystrike #5
Posted 24 August 2012 - 05:55 PM
Not to undercut Graypup's excellent advice but, what you're wanting to do is something like the following:


-- we will call this our "main loop"
while true do
  if disk.isPresent(side) and fs.exists("disk/level") then
	file = io.open("disk/level", "r")
	dlevel = file:read()
	if dlevel >= pass then
	  shell.run("clear")
	  shell.run("menu")
	end
  else -- if disk.isPresent(side) is not true as per above we know it is false, no need to check condition.
	input = read()
	if input == "hack" -- do not forget that a string outside of quotes is a variable name
	  then shell.run("hack")
	end
  end
end

I didn't actually run this script so check on your own for errors, but this general concept should be close to what you want.
i fixed a issue that came up you forgot a then on line 13
however it still doesn't accomplish the task of constantly checking for a update in the if statement for the disk present or not after the first run
OmegaVest #6
Posted 24 August 2012 - 07:00 PM
Why don't you use an event pull? I know I've suggested it to you before the . . . break.


while true do
   timeStop = os.startTimer(2)  -- Number of seconds to wait until it resets the loop, also, we will be checking if this timer is the one that comes up.

   event, arg1, arg2 = os.pullEvent()

   if event == "disk" and fs.exists("disk/level") then
      file = io.open("disk/level", "r")
      dlevel = file.read()
      dlevel = tonumber(dlevel)  -- Not always needed, but it helps.
      if dlevel >= pass then
         shell.run("clear")
         shell.run("menu")
      end
   elseif event == "char" then
      input = io.read()
      if input == "hack" then
            shell.run("hack")
      end
   elseif event == "timer" and arg1 = timeStop then
      doHeader()   -- I suggest making a function for the header, since you will be calling it repeatedly.
   end
end


That will work. Although, I'd like to know what some of these programs are. If "hack" does what it did before, then you can use break in place of its run line.
Deadlystrike #7
Posted 24 August 2012 - 07:57 PM
Why don't you use an event pull? I know I've suggested it to you before the . . . break.


while true do
   timeStop = os.startTimer(2)  -- Number of seconds to wait until it resets the loop, also, we will be checking if this timer is the one that comes up.

   event, arg1, arg2 = os.pullEvent()

   if event == "disk" and fs.exists("disk/level") then
	  file = io.open("disk/level", "r")
	  dlevel = file.read()
	  dlevel = tonumber(dlevel)  -- Not always needed, but it helps.
	  if dlevel >= pass then
		 shell.run("clear")
		 shell.run("menu")
	  end
   elseif event == "char" then
	  input = io.read()
	  if input == "hack" then
			shell.run("hack")
	  end
   elseif event == "timer" and arg1 = timeStop then
	  doHeader()   -- I suggest making a function for the header, since you will be calling it repeatedly.
   end
end


That will work. Although, I'd like to know what some of these programs are. If "hack" does what it did before, then you can use break in place of its run line.
im geting Line 19 'then' expected, however i see a then on line 19 and i don't see why its complaining about that code
OmegaVest #8
Posted 24 August 2012 - 08:00 PM
Oh.


   elseif event == "timer" and arg1 == timeStop then

Everyone makes mistakes. I more than most, but everyone does.
Deadlystrike #9
Posted 24 August 2012 - 08:30 PM
Oh.


   elseif event == "timer" and arg1 == timeStop then

Everyone makes mistakes. I more than most, but everyone does.
well no more errors, but the disk function doesn't work at all now. weather it starts with disk in or without it still goes to and stays on the char event thingy
OmegaVest #10
Posted 24 August 2012 - 08:44 PM
Okay, I just had about ten minuted to look this over and think it through.

Add this to the top of my code, just below the while loop start.


   if disk.isPresent(side) then
      os.queueEvent("disk")
      sleep(1)
   end

And don't press any letter keys while my code is active unless you are trying to "hack".

Someone will (hopefully) correct me if I have this wrong. I've never had to use this like this, and it is why I hate keycard programs. No single way is actually better than the rest. My code actively checks for the disk to be inserted, and if it does not, then it is not the code I gave you (I think). However, dual*moon's program will always be able to see it at the start.
Deadlystrike #11
Posted 24 August 2012 - 08:55 PM
Okay, I just had about ten minuted to look this over and think it through.

Add this to the top of my code, just below the while loop start.


   if disk.isPresent(side) then
	  os.queueEvent("disk")
	  sleep(1)
   end

And don't press any letter keys while my code is active unless you are trying to "hack".

Someone will (hopefully) correct me if I have this wrong. I've never had to use this like this, and it is why I hate keycard programs. No single way is actually better than the rest. My code actively checks for the disk to be inserted, and if it does not, then it is not the code I gave you (I think). However, dual*moon's program will always be able to see it at the start.

while true do
	  if disk.isPresent(side) then
	  os.queueEvent("disk")
	  sleep(1)
   end
   timeStop = os.startTimer(2)  -- Number of seconds to wait until it resets the loop, also, we will be checking if this timer is the one that comes up.
   event, arg1, arg2 = os.pullEvent()
   if event == "disk" and fs.exists("disk/level") then
	  file = io.open("disk/level", "r")
	  dlevel = file.read()
	  dlevel = tonumber(dlevel)  -- Not always needed, but it helps.
	  if dlevel >= pass then
		 shell.run("clear")
		 shell.run("menu")
	  end
   elseif event == "char" then
	  input = io.read()
	  if input == "hack" then
		    shell.run("hack")
	  end
   elseif event == "timer" and arg1 == timeStop then
	  doHeader()   -- I suggest making a function for the header, since you will be calling it repeatedly.
   end
end

Results:
If computer starts with disk already in: Holds for about 10 seconds then errors with "Line 24 attempt to call nill"
if computer starts without disk in: allows for text input if you enter anything but hack it will freeze then error after a while
if computer starts without disk then adds disk: nothing changes
if computer starts with disk and takes it out: nothing because error happens like result 1
Deadlystrike #12
Posted 24 August 2012 - 11:14 PM
any help?
OmegaVest #13
Posted 24 August 2012 - 11:42 PM
What the-? Okay, I was going off of what you put in. Also, I don't know what hack actually is. Personally I would make it a break, which will quit the program and exit back to the shell (If I remember, that was what you were trying to do in the other topic).

Further, doHeader is not defined. I suggested you make a function like that so that you won't have to type out the "Base Terminal Link" art more than once. So, if you were trying to use it as-is, you need to add a few things.


Now, for some debug. Add this line right after the pullEvent.


print(event)

Then go through each of those tests again.
Deadlystrike #14
Posted 25 August 2012 - 12:28 AM
What the-? Okay, I was going off of what you put in. Also, I don't know what hack actually is. Personally I would make it a break, which will quit the program and exit back to the shell (If I remember, that was what you were trying to do in the other topic).

Further, doHeader is not defined. I suggested you make a function like that so that you won't have to type out the "Base Terminal Link" art more than once. So, if you were trying to use it as-is, you need to add a few things.


Now, for some debug. Add this line right after the pullEvent.


print(event)

Then go through each of those tests again.

without disk in at start: timer spam for like 10 seconds then i can enter somthing
with disk in at stsart: timer once then attempt to call nill line 24
Deadlystrike #15
Posted 25 August 2012 - 07:46 PM
still need some help plox
Deadlystrike #16
Posted 26 August 2012 - 08:50 PM
Maby it would be easyer for you to help if i simply restated what im trying to accomplish

A program that has multiple If statements Like this


if diskispresent then
-does things
if diskisnotpresent then
-does things

That alone is easy to do, and i know i would use a elseif. However what im trying to accomplish is "Assuming that that program is still the one runing" if at anytime the If statement that is true nolonger becomes true and another one does it will go back and do the other instead. (i.e: When program was started a disk was present thus it does the things that the first if tells it to do, then midprogram the disk is taken out the program would stop what its doing and do what the ifdiskisnotpresent says to do.
OmegaVest #17
Posted 27 August 2012 - 04:03 PM
Ah, sorry, I have been not-so-present at my computer over the weekend. I know what you are trying to do, and even with 48 hours of thinking on it, I have no idea what is wrong with the code. And the lack of response from anyone else tells me they have the same problem. I keep working on it, so watch your pm inbox if I should find an answer.
Deadlystrike #18
Posted 27 August 2012 - 07:52 PM
Ah, sorry, I have been not-so-present at my computer over the weekend. I know what you are trying to do, and even with 48 hours of thinking on it, I have no idea what is wrong with the code. And the lack of response from anyone else tells me they have the same problem. I keep working on it, so watch your pm inbox if I should find an answer.
Dan200 sugjested i use the Parellel API along with the os.PullEvent for disk so ill try that out. hopefully it works