Jump to content




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

peripheral help

4 replies to this topic

#1 KnightMiner

  • Members
  • 61 posts
  • Locationattempt to index "location" (a nil value)

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).

#2 SquidDev

    Frickin' laser beams | Resident Necromancer

  • Members
  • 1,427 posts
  • LocationDoes anyone put something serious here?

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).


#3 KnightMiner

  • Members
  • 61 posts
  • Locationattempt to index "location" (a nil value)

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 by KnightMiner, 27 January 2016 - 04:43 AM.


#4 SquidDev

    Frickin' laser beams | Resident Necromancer

  • Members
  • 1,427 posts
  • LocationDoes anyone put something serious here?

Posted 27 January 2016 - 07:48 AM

View PostKnightMiner, on 27 January 2016 - 04:42 AM, said:

ILuaContext.executeTask? I cannot seem to find that, do you mean ILuaContext.executeMainThreadTask or another command?

Sorry, I wrote this from memory.

View PostKnightMiner, on 27 January 2016 - 04:42 AM, said:

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.

#5 KnightMiner

  • Members
  • 61 posts
  • Locationattempt to index "location" (a nil value)

Posted 28 January 2016 - 02:42 AM

View PostSquidDev, on 27 January 2016 - 07:48 AM, said:

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).





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users