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

[MC 1.7.10] Add a delay to a peripheral method internally

Started by KnightMiner, 26 January 2016 - 07:55 PM
KnightMiner #1
Posted 26 January 2016 - 08:55 PM
How would I go about adding a delay after a command is executed within the actual peripheral command? My clicking turtle currently rapidly fires off clicks, which is a little inconsistent with other turtle functions as they all have a delay. (I know I can run sleep(n) in lua to add a delay, but I want the delay to prevent people who don't do that from abusing it) I tried using turtle.playAnimation(TurtleAnimation.Wait), but it did not seem to work (which may be due to me using a peripheral type rather than a tool type).
SquidDev #2
Posted 26 January 2016 - 09:15 PM
If you just want a one tick delay, then you can use ILuaContext.executeTask. Otherwise you'll need to wait for an event, and queue it after x ticks. If you subscribe to TickEvent.ServerTickEvent you can then count down ticks until you should resume. You might want to have a look at CCTweaks' delayed tasks implementation (though there is a bit of a wacky linked list implementation there).
KnightMiner #3
Posted 27 January 2016 - 04:42 AM
ILuaContext.executeTask? I cannot seem to find that, do you mean ILuaContext.executeMainThreadTask or another command?

In either case, how would I use that function? The java docs coverage on that and tasks is very lacking, and the code in the API just has an interface there (actual code is in the main computercraft code). I'd assume I just write my final function code in a new ILuaTask object, but there might be something more that I'm missing.
Edited on 27 January 2016 - 03:43 AM
SquidDev #4
Posted 27 January 2016 - 07:48 AM
ILuaContext.executeTask? I cannot seem to find that, do you mean ILuaContext.executeMainThreadTask or another command?

Sorry, I wrote this from memory.

In either case, how would I use that function? The java docs coverage on that and tasks is very lacking, and the code in the API just has an interface there (actual code is in the main computercraft code). I'd assume I just write my final function code in a new ILuaTask object, but there might be something more that I'm missing.

You can call it with an empty task, this should delay it one tick. However, if you are interacting with the world, you should put all code inside the task function so your code is thread safe.
KnightMiner #5
Posted 28 January 2016 - 02:42 AM
You can call it with an empty task, this should delay it one tick. However, if you are interacting with the world, you should put all code inside the task function so your code is thread safe.

Thanks, it worked perfectly. I ended up deciding to move all of the code for interacting with the block to the task, and leaving the basic checks outside the task (so it fails without delay if it cannot interact).