26 posts
Posted 24 July 2014 - 09:59 PM
Is using a blank
while loop frowned upon? Here's the bit I have in my code:
for slot=1, 16 do
print("Place a block in slot"..slot..".")
while turtle.getItemCount(slot) == 0 do
--Waiting for block to be placed in [slot]
end
end
I don't think there should be a problem with this, but it occurred to me that this may be some sort of "bad practice" or something.
227 posts
Location
Germany
Posted 24 July 2014 - 10:06 PM
The program will eventually error with "too long without yielding" if there hasn't been put an item into the slot; put a sleep in there and you're good to go.
If you want to go even fancier, use
os.pullEvent("turtle_inventory")
so it would only check when the inventory has actually changed.
Edited on 24 July 2014 - 08:12 PM
3057 posts
Location
United States of America
Posted 24 July 2014 - 10:07 PM
In this particular case, you can, but only because turtle.getItemCount( slot ) yields internally. If it did not, your code would be terminated after 10 seconds.
227 posts
Location
Germany
Posted 24 July 2014 - 10:11 PM
In this particular case, you can, but only because turtle.getItemCount( slot ) yields internally. If it did not, your code would be terminated after 10 seconds.
False, turtle.getItemCount() doesn't yield, so the code would error
26 posts
Posted 24 July 2014 - 10:24 PM
If you want to go even fancier, use
os.pullEvent("turtle_inventory")
so it would only check when the inventory has actually changed.
How exactly would I implement this? I'm confused on what context to use it in.
227 posts
Location
Germany
Posted 24 July 2014 - 10:25 PM
Just put that in your while loop:
while turtle.getItemCount( slot ) == 0 do
os.pullEvent("turtle_inventory")
end
That would yield at that point and wait until ComputerCraft triggers the specific event.
3057 posts
Location
United States of America
Posted 24 July 2014 - 10:27 PM
@wieselkatze: All the turtle functions yield internally, to my knowledge.
@ReconTurtle: Pretty much like this…
for slot=1, 16 do
print("Place a block in slot"..slot..".")
while turtle.getItemCount(slot) == 0 do
os.pullEvent( "turtle_inventory" ) --#wait for an item to enter/leave inventory
--Waiting for block to be placed in [slot]
end
end
Edit: Ninja'd
Edited on 24 July 2014 - 08:28 PM
26 posts
Posted 24 July 2014 - 10:31 PM
Thanks to both you guys for your help, I appreciate it :D/>
Edit: Where can I find a list of these events, for future reference?
Edited on 24 July 2014 - 08:32 PM
227 posts
Location
Germany
Posted 24 July 2014 - 10:39 PM
@wieselkatze: All the turtle functions yield internally, to my knowledge.
- snip -
Apparently not. Try that loop without any yield inside it.
@ReconTurtle:
Look at the
wiki page, there is everything you will need ;)/>
3790 posts
Location
Lincoln, Nebraska
Posted 24 July 2014 - 10:41 PM
Alternatively, you can always use sleep(0). Less elegant, but effective nonetheless.
7083 posts
Location
Tasmania (AU)
Posted 25 July 2014 - 04:35 AM
turtle.getItemCount(), turtle.getItemSpace() and turtle.getFuelLevel() do not pull events. 1.6 might've changed that, but probably not. I would assume turtle.getSelectedSlot() and turtle.getFuelLimit() (both added by 1.6) also don't pull events. These information-gathering functions should be the only exceptions.
I've also noticed the "all turtle commands pull events" dis-information around the place, and must admit I've failed to correct it lately.
Forget about using sleep. It has a much higher overhead than just waiting for a turtle inventory event. It'll work, yeah, but that's no excuse to use it. :P/>
Edit: Well, I suppose it's only fair to point out that the turtle_inventory event was only added to ComputerCraft recently-ish; if you're on a build prior to CC 1.55, then your options boil down to using a sleep/timer solution, or asking the user to press a key when ready and waiting for the key event.
Edited on 25 July 2014 - 02:39 AM