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

problems with my first program

Started by memnas, 12 November 2014 - 09:18 AM
memnas #1
Posted 12 November 2014 - 10:18 AM
Hi guys,
i've only been playing minecraft for a month and have decided to try my first coding XD, along with someone who been playing awhile. you prob will find loads of mistake with it :P/>, but here goes. This program should read the energy inside the resonant energy cell to the left. If the cell has more then 2,000,000 energy inside it, then it will output redstone the the right. if less then nothing should happen. so far the program will turn the redstone on when the energy is greater, however when the energy is less then 2,000,000 the redstone signal doesn't turn off. We are unsure if it becuase the program isn't repeating, which we tried to stick a repeat in however it just errored.



cell = peripheral.wrap("left")
energy = cell.getEnergystored("left")
while true do
if energy > 2000000
then sleep(1)
redstone.setOutput("right", true)
else sleep(1)
redstone.setOutput("right", false)
end
end

Thanks in advance.
valithor #2
Posted 12 November 2014 - 05:04 PM
Move the energy = cell.getEnergystored("left") into the while true loop. As you have it the energy level is only being read once, so it will only ever be equal to one value. If you put it inside the loop then it will be read every time the loop is ran. You will want to move it right before the if statement.
KingofGamesYami #3
Posted 12 November 2014 - 05:15 PM
Also, indentation is nice. This isn't causing the problem, but it will help you later on.


while true do
  --#indent
  if <statement> then
    --#indent
  else
    --#indent
  end
end
(valithor is correct, you are never updating your energy level, causing your current problem.)
martijnenco #4
Posted 13 November 2014 - 07:07 PM
And! getEnergyStored() is the correct method and not getEnergystored() .. (The case on the word: 'Stored')
memnas #5
Posted 13 November 2014 - 08:07 PM
ahh thank guys!
valithor #6
Posted 13 November 2014 - 11:19 PM
And! getEnergyStored() is the correct method and not getEnergystored() .. (The case on the word: 'Stored')

He obviously had it correct in the program that he had on the server, or he would have gotten an error. He probably wrote it dirrectly here on the topic instead of copying and pasting.