title: Capture Variable Changes In Lua
body: <p>I'm unsure where to look as far as what I could use to capture a variable change in a program loaded within another program.</p>
  
  <p>Here's my code, as messy as it is: </p>
  
  <pre><code>function launch()
      shell.run("clear")
      print("Preparing for first time run.")
      sleep(1)
      print("")
      local program = netTest()
      local file = loadstring(program)
      file()
      sleep(3)
      shell.run("clear")
  end
  
  function netTest()
    local output = http.get("http://pastebin.com/raw.php?i=hzZv3YH2")
    if output then
          local contents = output.readAll()
          output.close()
          return contents
    else
          print("Empty response")
          return false
    end
  end
  
  local program = netTest()
  local file = loadstring(program)
  
  launch()
  </code></pre>
  
  <p>Here's the code it's calling on:</p>
  
  <pre><code>function ping()
  fails = 0
  pingResult = 0
  print("Testing Internet Connection.")
  print("")
  oldx, oldy = term.getCursorPos()
  print("Connection Test 1")
  http.request("http://www.google.com/")
          if os.pullEvent() == "http_success" then
                  local oldx, oldy = term.getCursorPos()
                  term.setCursorPos(46,oldy-1)
                  io.write("Passed")
          else
                  local oldx, oldy = term.getCursorPos()
                  term.setCursorPos(46,oldy-1)
                  io.write("Failed")
                  fails = fails+1
          end
  sleep(1)
  print("Connection Test 2")
  http.request("http://www.microsoft.com/")
          if os.pullEvent() == "http_success" then
                  local oldx, oldy = term.getCursorPos()
                  term.setCursorPos(46,oldy-1)
                  io.write("Passed")
          else
                  local oldx, oldy = term.getCursorPos()
                  term.setCursorPos(46,oldy-1)
                  io.write("Failed")
                  fails = fails+1
          end
  sleep(1)
  print("Connection Test 3")
  http.request("http://www.example-failure.com/")
          if os.pullEvent() == "http_success" then
                  local oldx, oldy = term.getCursorPos()
                  term.setCursorPos(46,oldy-1)
                  io.write("Passed")
          else
                  local oldx, oldy = term.getCursorPos()
                  term.setCursorPos(46,oldy-1)
                  io.write("Failed")
                  fails = fails+1
          end
  sleep(1)
          if fails == 0 then
                  print("")
                  print("")
                  print("Test Complete, no failures detected.")
                  sleep(1.5)
          elseif fails == 1 then
                  print("")
                  print("")
                  print("1 connection failures detected. A Website Host might be down however connectivity is still there.")
                  print("")
                  print("Test Complete.")
                  sleep(1.5)
          elseif fails == 2 then
                  print("")
                  print("")
                  print("2 connection failures detected. Possible limited web connectivity.")
                  print("")
                  print("Test Complete.")
                  sleep(1.5)
          elseif fails == 3 then
                  print("")
                  print("")
                  print("Catastrophic connection failure detected. A firewall or improper internet settings may be the problem.")
                  print("")
                  print("Test Complete.")
                  pingResult = __pingResult + 3
                  sleep(1.5)
          end
  end
  
  ping()
  </code></pre>
  
  <p>What it's doing as you can see is running a program externally which will test the connection by making http requests to a couple pages to make sure there's connection to the internet. (I know, it's a bit lame, but I'm still learning).</p>
  
  <p>Basically when and if the connection reads failure on all 3 stages, it'll make my pingResult variable = 3. What I'm wanting to do from the first program that called on my internet utility is record what that variable is set to. If it's set to 3 upon closing, to record that variable being set, and then for simplicity's sake, print that variable so I can see it's either 0 or 3. I'll be doing something with it later but you get the gist of it.</p>
  
  <p>I've tried a couple other things but can't seem to figure out on how I go about doing this. Seeing as I'm still new I tried random stuff but none seemed to work, or I just did them wrong so I don't know what to do with it. Been trying at this one for a couple days to no success.</p>