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

Waiting program

Started by cory1234567, 19 December 2012 - 07:35 AM
cory1234567 #1
Posted 19 December 2012 - 08:35 AM
im trying to write a code to wait so that it waits untell the program return true but all it does is outputs the table and stops waiting because the condition returned true


m = peripheral.wrap("front")
data = m.get()
function wait()
  for i = 1,2 do
    print("checking Progress")
  for i, j in pairs(data) do
   print(tostring(i)..": "..tostring(j))
  end
  sleep(10)
end
end

--for i, j in pairs(data) do
--print(tostring(i).." : "..tostring(j))
--end

for i,j in pairs(data) do   
  print("data."..i.." is type ",type(j),", value "..tostring(j))
end

while data["Can Store Energy"] == false do
if data["Energy Full"] == true then
  turtle.turnLeft()
  turtle.forward()
  turtle.turnRight()
  turtle.dig()
  print("Im Full")
else
  wait()
  print("Not Full")
end
end


i dont know wht im doing wrong and yes i am a noob at coding but im trying to learn!!
Edited on 22 December 2012 - 03:11 PM
digpoe #2
Posted 19 December 2012 - 08:38 AM
im trying to write a code to wait so that it waits untell the program return true but all it does is outputs the table and stops waiting because the condition returned true


m = peripheral.wrap("front")
data = m.get()
function wait()
  for i = 1,5 do
	print("checking Progress")
	sleep(10)
  end
end
for i, j in pairs(data) do
  print(tostring(i)..": "..tostring(j))
end

while data["Can Store Energy"] do
  if data["Energy Full"] then
	turtle.turnLeft()
	turtle.forward()
	turtle.turnRight()
	turtle.dig()
	print("Im Full")
  else
	wait()
	print("Not Full")
  end
end

i dont know wht im doing wrong and yes i am a noob at coding but im trying to learn!!
I suppose you're using Misc. Peripherals because the default peripherals don't return stuff like that. But anyway, the while statements won't work because the table uses strings. If you want it to work, use somthing like this:

while data["Can Store Energy"] == "true" then
--code
end
cory1234567 #3
Posted 19 December 2012 - 08:46 AM
ya im using Misc. Peripherals but using == "true" made no difference it still just sits there and ends the code
digpoe #4
Posted 19 December 2012 - 08:50 AM
Hmmm.
Let's see:

while data["Can Store Energy"]=="true" do
if data["Energy Full"]=="true" then
turtle.turnLeft()
turtle.forward()
turtle.turnRight()
turtle.dig()
print("Im Full")
else
wait()
print("Not Full")
end
end
I suppose that might work? Wait, i'm typing in blue o.o
cory1234567 #5
Posted 19 December 2012 - 09:08 AM
wait why is it still in blue the while and if statement have been ended so why would the code still think its running
digpoe #6
Posted 19 December 2012 - 09:19 AM
wait why is it still in blue the while and if statement have been ended so why would the code still think its running
You didn't read my post properly, I belive. Or I worded it in a way that made you misunderstand it. If you read it, I made a modification to your while loop, which basically checks if data["Can Store Energy"] is true and then excecutes the code.
cory1234567 #7
Posted 19 December 2012 - 09:46 AM
sorry i made an error in the code the can store energy should be false not true befor it runs the next part of the code but still not working
ChunLing #8
Posted 19 December 2012 - 12:32 PM
Print out data just before the while loop to get a better idea of what's happening. Like:
for i, j in pairs(data) do
    print("data."..i.." is type ",type(j),", value "..tostring(j))
end
This will allow you to see the types and values you're trying to compare.
cory1234567 #9
Posted 22 December 2012 - 11:48 AM
Can Store Energy is type boolean, value false
Energy Full is type boolean, value true

so the program is correctly reading it but will still not work im using true/false statements to deteck if its full and can store energy
ChunLing #10
Posted 22 December 2012 - 12:30 PM
Okay, current version of your code, or is it already posted?
cory1234567 #11
Posted 22 December 2012 - 02:30 PM
how do u get the version of the code or r u talking about the version of computer craft i have?
Dlcruz129 #12
Posted 22 December 2012 - 03:38 PM
how do u get the version of the code or r u talking about the version of computer craft i have?

You copy your code, then paste it into a forum topic.
Luanub #13
Posted 22 December 2012 - 03:44 PM
To start these two statements function the same way.

if data["Energy Full"] == true then

if date["Energy Full"] then

--if checking for false for future reference it can be
if data["Energy Full"] == false then

if not date["Energy Full" then

This one will not work due to true being wrapped in quotes making it a string instead of a bool.

if data["Energy Full"]=="true" then


If your using the last one with quotes it will not work.
cory1234567 #14
Posted 22 December 2012 - 04:11 PM
the first post has been updated to what i have now
Luanub #15
Posted 22 December 2012 - 04:21 PM
I think the problem is the way you are checking the data.

Instead of doing this

while data["Can Store Energy"] do

You will want to compare the table indicy to see if it matches the string "Can Store Energy".

I'm going to assume the table is setup with standard numbered indicies and if so you should be doing something like…

while data[1] == "Can Store Energy" do
Same with the if statements.
cory1234567 #16
Posted 22 December 2012 - 04:31 PM
the thing with this while data[1] == "Can Store Energy" do is your saying that if the first slot in the table = the string Can Store Energy then go ahead to the next section

and what i want is for it to tell me if Can Store Energy is true or false so that it can continue to the next section of code
Luanub #17
Posted 22 December 2012 - 04:51 PM
What does your debug printing print out? I'm not familiar with MiscPeripherals, or what it outputs. I'm assuming you do the m.get() and it returns something like "Can Store Energy" if it has room for engery and will return "Energy Full" it it does not? If that is the case then what I suggested should work.

If it is using different table indicies for different data that it is returning, you will need to know the name of the indicy or its position in the table then check if that indicy is true by doing something like

while data[indicyName] do

Edit: I've taken a look at some of the code that comes with MiscPeripherals and think you want to be checking something like this..

while not data["canStoreEnergy"] do
No spaces, but I'm not 100% sure as I'm not at home and can't install the periph and play with it.
Edited on 22 December 2012 - 04:06 PM
cory1234567 #18
Posted 22 December 2012 - 06:04 PM
when u call for it this is what it displays
Full Energy: false
Can Store Energy: true
Inventory Full: false
Items In Inventory: false
No Energy: false
Space In Invetory: true
Inventory Empty: true
Energy Stored: true

with i being the can store energy and j being the boolean value of true or false
ChunLing #19
Posted 22 December 2012 - 06:20 PM
If data.Can Store Energy == true then of course your while loop won't execute, it requires that to be false.

Do you want the loop to execute while Can Store Energy or while not Can Store Energy?

I think that we need to see the full output of this program, not just excerpts.

One thing I just noticed, I don't see a data.Energy Full, but data.Full Energy. Your code is checking for data.Energy Full, which would be nil if there isn't such a value.

Try serializing and saving data to a file, so that you can examine the exact structure as it appears in Lua code.
Edited on 22 December 2012 - 05:29 PM