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

Engine Management - Buttons not working correctly

Started by crd1987, 19 May 2015 - 08:24 PM
crd1987 #1
Posted 19 May 2015 - 10:24 PM
Good Afternoon,

I am new to LUA. So Please forgive me if I have failed to do something simple :)/>

I am trying to make a multi page Engine management program which can turn on and off 6 banks of engines and eventually add how much liquid is in my tanks.

Currently I am working on the Engine Management but it is working not how I hoped. I need to turn "bank 1" on then off before i can move to bank 2 etc. Once I have turned bank 6 on ( the hit box for the button is misaligned) I am unable to turn any of the engines on or off.

What I was hoping to do was be able to turn any combination of these buttons on and off as required but I just don't know what I have done wrong. I wondered if someone would mind having a quick look and seeing what I have done wrong.

I have added my code to pastebin please see the link below.

http://pastebin.com/q6AvJfVL

Any Help would be appreciated.
KingofGamesYami #2
Posted 20 May 2015 - 01:51 AM
I find it strange you are using multiple os.pullEvent calls within a single while loop, call it once and compare the same data repeatedly. Otherwise, it won't check every option for every event, instead checking a single option per event, which changes as the loop progresses.
Bomb Bloke #3
Posted 20 May 2015 - 02:06 AM
end
end
end
end
  end
   end
end
end
  end	

:huh:/>

If you apply proper indentation to that lot, you'll notice you've got a bunch of indefinitely-repeating while loops, none of which you ever break out of, all nested inside each other. Each click leads to another loop starting. The only loop that ever actually gets to repeat is the one in the very center of the nested structure.

For example:

while true do  -- This loop can never repeat...
  while true do
    -- Because this second loop never ends!
  end
end

As KoGY points out, you should be pulling ONE event per click, and checking all buttons against that. Your enginemanagement() function is already looping the action of pulling an event. Pass that event data on to the engine1on() function, and remove all further loops and event pulls from there.

Also consider reading up on tables. They do wonders for reducing the length of scripts that otherwise need to use lots of repetitive chunks of code.
Edited on 20 May 2015 - 12:09 AM
crd1987 #4
Posted 20 May 2015 - 06:25 PM
Thank you for the response Like I say this is my first proper computercraft program maybe I am biting more than I can chew lol.

are you suggesting that each loop should be a seperate function?
crd1987 #5
Posted 20 May 2015 - 06:37 PM
Thank you both so much I removed all the loops and left just the one around all of them and it worked Thank you both for your time. I will look into tables thank you for the link.

:D/>