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

API.native

Started by natedogith1, 14 August 2014 - 07:48 PM
natedogith1 #1
Posted 14 August 2014 - 09:48 PM
Can someone explain to me why turtle has a turtle.native, and yet peripheral doesn't have a peripheral.native?
Lyqyd #2
Posted 14 August 2014 - 09:57 PM
Probably because there's no real reason to expose the native peripheral methods (exposed by Java), but the native turtle methods can be useful in certain circumstances.
theoriginalbit #3
Posted 15 August 2014 - 12:37 AM
A Turtle's native code is the actual Java implementation of the functionality, it returns an ID upon calling, and once the Turtle has actually completed executing its code it will fire an event to Lua. The Lua-side Turtle API is just simply a wrapper upon these native methods so that it waits for this completion event before allowing your script to continue. The same isn't needed for APIs such as the peripheral API.
Lyqyd #4
Posted 15 August 2014 - 12:51 AM
Actually, peripheral does have a native set of calls that it replaces some/all of when it's loaded. It just localizes the copy of the native ones.
natedogith1 #5
Posted 15 August 2014 - 01:01 AM
Looking at the source of rom/apis/turtle/turtle, it seems to me that turtle is almost the exact same as turtle.native. And while I remember that the lua turtle api used to wait for the event, it seems it no longer does, so I don't see the use of the few different native functions. (And for me I'd think peripheral.native would be useful if you're overriding peripheral stuff so you can run stuff faster (speaking of, imagine loading the peripheral API multiple times))
Edited on 14 August 2014 - 11:05 PM
Lyqyd #6
Posted 15 August 2014 - 01:08 AM
The turtle functions still yield, they just do so internally. As you can see, the equipLeft and equipRight functions are made to modify the turtle API on the fly, depending on the presence or lack of crafting table peripheral.

You would gain no significant speed by using the native versions of the peripheral API.
natedogith1 #7
Posted 15 August 2014 - 01:28 AM
The turtle functions still yield, they just do so internally. As you can see, the equipLeft and equipRight functions are made to modify the turtle API on the fly, depending on the presence or lack of crafting table peripheral.

You would gain no significant speed by using the native versions of the peripheral API.
Given that the only changes that the turtle lua api seems to do are similar in scope to the changes of the peripheral API, I'd expect their natives to be equally visible. As for the speed factor, maybe not if you're not calling the stuff too often, but if you're calling it frequently I'd imagine the difference could be huge (I've had issues where the string methods are too slow, so such speed improvements can actually mean something to me)