Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal

DeBrates

Members
  • Content Count

    5
  • Joined

  • Last visited

Posts posted by DeBrates

  1. 3 hours ago, Fingercomp said:

    The bug is really simple. To find it, follow through the code line-by-line, analyzing what happens at each line. (This is something I do quite often when debugging an obscure problem.)

    First, skip the first few dozens of lines until we get to the L65 (short for line 65), where the loop is defined. We then encounter the if block at L68, of which the first branch is actually run (page == "page1" as it was set to this value on L63), resulting in the pageOne call.

    The next line to examine is L79, where the program stops until it pulls the touch event. Here, I check that the event parameters weren't mixed up. All right, let's go on and press the button.

    The first condition, on L82, is satisfied (page == "page1", set on L63), so we get the page variable updated to the new value, "page2". But there's more code to run before we loop back! The second condition, on L89. The coodinate mess after the first and evaluates to true. What about page? Well, we've just set it to "page2", so... it, too, yields true. Therefore, the whole condition is satisfied. And the variable is set back to "page1". We loop back, and redraw the first page.

    This is how we've just spotted the bug. In case you wonder why Event 2 isn't written to the screen: well, it is written. At position (4, 45). You've just mistaken x for y (and vice versa): 4 is the column, 45 is the row. This is clearly beyond the viewport 58×18, so you see nothing actually printed.

    OK. How should you fix it? It's simple: use elseifs.

    
    if (x >= w / 2 - 5 and
        x <= w / 2 - 5 + 11 and
        y >= h / 2 - 1 and
        y <= h / 2 + 1) then
      if page == "page1" then
        mon.set(4, 10, "Event 1")
        table.insert(a, "X")
        page = "page2"
      elseif page == "page2" then
        mon.set(45, 10, "Event 2")
        page = "page1"
      end
    end

    Wow! Great analysis and explanation. This problem has stumped me for days on a larger project, and the solution was that simple. You have certainly helped me here and given me some debugging advice for the future! Thanks!

  2. This is a very hard issue to explain, but hopefully someone knows what I'm talking about. This is either a major bug or a simple flaw in my code.

    If you want to try this for yourself, or view the code

    pastebin get 7UHGc1tu demo

    I have a program that has multiple pages, and a button that is in the exact same position on both pages. The issue is when I press the button on one page, which triggers an event then switches to the second page, it triggers the first event but also part of the second event. I have setup a sample program to demonstrate this issue. The program is supposed to display "Event 1" if the button on Page 1 is pressed, and display "Event 2" if the button on page 2 is pressed. Pressing the button on page 1 will also switch the page to page 2 where an identical button is. The button on page 2 will trigger the second event and switch back to page 1.

    What ends up happening is "Event 1" is triggered, but when the program switches to page 2, it detects the press for a second time even though it was only clicked once. The odd part is that it doesn't display "Event 2" but it does switch the page back to page 1.

    I feel like I'm not being followed, so here is a gif of the sample program and explanation of the program.

    The X represents the amount of times page 1 has been drawn. The program should switch to page 2, instead it acts like it switched to page 2, switched back to page 1 because of the button press, but didn't display "Event 2".

     

    What Page 2 would look like after 1 button press:

    https://imgur.com/a/wbIMjmF

    The basic conclusion is that it toggles the buttons on both pages, but only sets the variable on page 2 and doesn't display "Event 2". Ideally it would only display "Event 1" and switch to page 2 on a single press.

    I know this is basically impossible to follow but if you have any insight about anything, I would appreciate the help! If you would like me to explain anything further or clarify anything, let me know! Thanks!

  3. I'm having trouble with the redstone card. Whatever I do I can't seem to get any redstone output. My main goal is bundled output in a separate program, but I wrote this test program to see if any redstone output would function. From what I've seen this looks valid to me. Is there any reason this code wouldn't work? Program is just intended to output on the left side and write the strength on the monitor.

    I've tried both tiers of redstone cards, and tried all side aliases such as sides.left and sides.east.

    Edit: Redstone i/o works but I don't see a reason the redstone card wouldn't unless it/something is bugged.

    local component = require("component")
    local sides = require("sides")
    local rs = component.redstone
    local mon = component.gpu
    mon.setResolution(58,18)
    
    rs.setOutput(sides.left, 15)
    
    local strength = rs.getInput(sides.left)
    mon.set(1,7, tostring(strength))

     

    Any help is appreciated! Thanks!

  4. 3 hours ago, Molinko said:

    event.pull has an optional first argument, a timeout. 

    
    local name, _, x, y = event.pull(0.1, "touch")
    
    if name then -- # name will be nil if event.pull(0.1, ...) times out after 0.1 sec
      if y == 1 and x < 30 and x > 18 and reactorOnline then
        r.setActive(false)
      elseif y == 1 and x < 30 and x > 21 then
        r.setActive(true)
      elseif y == 8 and x == 21 or x == 22 then
        r.setAllControlRodLevels(rodLevel - 10)
      elseif y == 8 and x == 28 or x == 29 then
        r.setAllControlRodLevels(rodLevel + 10) 
      end
    end

     

    Did not know about that extra argument! Works like a charm! Thanks!

  5. Hi, I'm recently new to OC and am working on porting some of my older programs from CC. I have this simple reactor program that has a few pull events. Issue I'm having is that the pull event freezes the loop until an event happens. I tried to get around this using a timer, but it doesn't seem to work the same as CC. How can I use pull events without freezing my loop? 

    Full Code (Events at bottom)

     

    Pull Event

      local name, _, x, y = event.pull("touch")
      if y == 1 and x < 30 and x > 18 and reactorOnline then
        r.setActive(false)
      elseif y == 1 and x < 30 and x > 21 then
        r.setActive(true)
      elseif y == 8 and x == 21 or x == 22 then
        r.setAllControlRodLevels(rodLevel - 10)
      elseif y == 8 and x == 28 or x == 29 then
        r.setAllControlRodLevels(rodLevel + 10) 
      end
      os.sleep(0.1)
    end

    Any help and/or OC advice is appreciated! Thanks!

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.