202 posts
Posted 17 January 2013 - 02:56 PM
I am trying to write a simple refuel function to call in some of my turtle programs to make sure my turtles don't run out of fuel or start without enough fuel. My code looks perfectly fine to me and when I run it I don't get any errors, but it just does nothing. I run the program and nothing happens. Would someone mind telling me what I've done wrong and what I can do to fix it? This is probably some simple mistake on my part since I am very much a beginner with Lua and computercraft. I appreciate all help and thank you in advance!
Thanks! :D/>
function checkFuel()
m = turtle.getFuelLevel()
n = 100000 - m
if m < 100000 then
print("I need ", n ," more fuel")
print("Waiting for Fuel...")
repeat
turtle.refuel()
until m > 100000
print("Got my fuel!")
else
print("I don't need fuel!")
end
end
7508 posts
Location
Australia
Posted 17 January 2013 - 03:01 PM
Ok so your problems:
- you were never rechecking the fuel, so really even if it hit the right level it wouldn't have known
- turtle.refuel only works on the selected slot, you need to check them all with a loop
local function checkFuel()
while turtle.getFuelLevel() < 100000 do
print( "I need "..( 100000 - turtle.getFuelLevel() ).." more fuel" )
print( "Checking for fuel..." )
for i = 1, 16 do
turtle.select( i )
turtle.refuel()
end
print( "Checked for fuel..."
end
print( "I have got enough fuel...")
end
Edited on 18 January 2013 - 01:22 AM
202 posts
Posted 17 January 2013 - 03:14 PM
Ok so your problems:
- you were never rechecking the fuel, so really even if it hit the right level it wouldn't have known
- turtle.refuel only works on the selected slot, you need to check them all with a loop
local function checkFuel()
while turtle.getFuelLevel() < 100000 do
print( "I need "..( 100000 - turtle.getFuelLeve() ).." more fuel" )
print( "Checking for fuel..." )
for i = 1, 16 do
turtle.select( i )
turtle.refuel()
end
print( "Checked for fuel..."
end
print( "I have got enough fuel...")
end
While I do thank you for the help this still has the same problem. When I run the program nothing happens.
7508 posts
Location
Australia
Posted 17 January 2013 - 03:21 PM
you have to call the function with this
checkFuel()
202 posts
Posted 17 January 2013 - 03:26 PM
you have to call the function with this
checkFuel()
I'm soooooo dumb……
1619 posts
Posted 17 January 2013 - 03:48 PM
you have to call the function with this
checkFuel()
I'm soooooo dumb……
Don't worry, you'll catch on. It can be tricky learning a new programming language.
2088 posts
Location
South Africa
Posted 18 January 2013 - 02:21 AM
Ok so your problems:
- you were never rechecking the fuel, so really even if it hit the right level it wouldn't have known
- turtle.refuel only works on the selected slot, you need to check them all with a loop
local function checkFuel()
while turtle.getFuelLevel() < 100000 do
print( "I need "..( 100000 - turtle.getFuelLeve() ).." more fuel" )
print( "Checking for fuel..." )
for i = 1, 16 do
turtle.select( i )
turtle.refuel()
end
print( "Checked for fuel..."
end
print( "I have got enough fuel...")
end
Does selecting or refueling yield?
7508 posts
Location
Australia
Posted 18 January 2013 - 02:23 AM
Does selecting or refueling yield?
I'm not actually too sure about that… although I'm sure if it didn't we would have known since 11 hours have passed…
2447 posts
Posted 18 January 2013 - 02:31 AM
Selecting does.
2088 posts
Location
South Africa
Posted 18 January 2013 - 02:32 AM
Does selecting or refueling yield?
I'm not actually too sure about that… although I'm sure if it didn't we would have known since 11 hours have passed…
Or his turtle had over 100000 fuel ever since ;)/>
7508 posts
Location
Australia
Posted 18 January 2013 - 02:35 AM
And there we go… we now know ;)/>
Cloudy is there a list compiled somewhere that contains all the CC API function calls that yield?
2005 posts
Posted 18 January 2013 - 12:46 PM
It's easier to list the ones that don't. If you look in the turtle API Lua code, only the functions that have been reverted to match the native versions will fail to yield. All the other's are automatically replaced with versions that yield until the native function sends back an event.
turtle["getItemCount"] = native.getItemCount
turtle["getItemSpace"] = native.getItemSpace
turtle["getFuelLevel"] = native.getFuelLevel
Edited on 18 January 2013 - 11:50 AM