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

add a TurtleAPI

Started by tlserver, 27 October 2012 - 02:38 AM
tlserver #1
Posted 27 October 2012 - 04:38 AM
can you add a method "getSelectedSlot" to TurtleAPI. I think this is not hard because java have it.
I am not good at English but I hope you can understand my poor Englise. ~.~
thank you
KelticRaven #2
Posted 27 October 2012 - 08:01 AM
You may want to look at the turtle.select(slotNum) command in the Turtle API.
That command selects the given Slot with with logic (Slot 1 is top left, 16 is bottom right).
Note: 9 is bottom right in version 1.33 or earlier versions.

Enjoy.. I hope I helped you.
tlserver #3
Posted 27 October 2012 - 09:09 AM
Yes, I have looked.
turtle.select(Num)
is input a num (1<=num<=16) but I mean that add a method
turtle.getSelectedSlot()
to output that number to show the slot which is selected now.
For example: a new turtle run the following programms

Print(turtle.getSelectedSlot())
turtle.select(2)
Print(turtle.getSelectedSlot())
turtle.select(4)
Print(turtle.getSelectedSlot())
then we will have :

1
2
4
Cloudy #4
Posted 27 October 2012 - 10:10 AM
When you're coding your program, assume you're the only one setting the slot. I really can't think of a use case of getting the selected slot.
Sebra #5
Posted 27 October 2012 - 10:41 AM
It's use is in api writing. If you want some functions to change some values then return them back. For now to know old values you need to intercept changing functions. Simple functions to return old values would be quite better way. It is not for selected slot only. It is needed to get current color of text and background. It would be good to know letter scale of monitors also. May be other things I cannot remember now.
Simplest way to add it without new functions adding is appending additional value to changing function like this:

success, oldSlotNum = turtle.select( slotNum )
oldTextColor = term.setTextColor(color)
oldBackgroundColor = term.setBackgroundColor(color)
Is it too hard or not wanted?
tlserver #6
Posted 27 October 2012 - 11:05 AM
I argee your point. it is not useful before computercraft 1.4 because turtle did not need fuel. But now, turtle need to add fuel. when a program is working, some time we need to add fuel. Then, the position change to the 'fuel slot' and call the method "turtle.refuel(1)". If no "turtle.getSelectedSlot()", I need to add a var in each programs to save the position before add fuel. ~.~
I want to write a function to add fuel and all programs call this function before moving. So that I do not need to find out where I change the slotNum and use a var to save them.
I think this is useful for some one who write many programs in the old version computer craft. I am one of them.. please help
Cloudy #7
Posted 27 October 2012 - 01:02 PM
Just restructure your code to work round it - save the variable, or make yourself a helper function which refuels then returns to the slot you pass it. It isn't difficult.
ChunLing #8
Posted 27 October 2012 - 02:44 PM
Use a variable "slot" (or whatever) to mean the selected slot, and then always select a slot with turtle.select(slot). If you need to switch away and switch back, then you do that, just make sure that you use slot to move back. To get the selected slot, just get slot. The expression "slot = math.fmod(slot,16)+1" will come in handy (you can change 16 to 15 or less if you want to use the last slots as reserved for fuel/chests/torches).
Sebra #9
Posted 27 October 2012 - 02:52 PM
So it is not wanted and in order to know current settings we need to hook setting function. :D/>/>
Cloudy #10
Posted 27 October 2012 - 03:13 PM
So it is not wanted and in order to know current settings we need to hook setting function. :D/>/>

Or wrap them, yes.
KelticRaven #11
Posted 20 November 2012 - 09:11 AM
It's use is in api writing. If you want some functions to change some values then return them back. For now to know old values you need to intercept changing functions. Simple functions to return old values would be quite better way. It is not for selected slot only. It is needed to get current color of text and background. It would be good to know letter scale of monitors also. May be other things I cannot remember now.
Simplest way to add it without new functions adding is appending additional value to changing function like this:

success, oldSlotNum = turtle.select( slotNum )
oldTextColor = term.setTextColor(color)
oldBackgroundColor = term.setBackgroundColor(color)
Is it too hard or not wanted?

Personally I like your idea to change the text color on the Monitor!
I will be trying that in my startup program for that monitor in the near future… if you do not mind.
(I know this is a touch off topic..but it was in the thread)
Tiin57 #12
Posted 20 November 2012 - 09:18 AM
Any particular reason to revive this? :/
Cloudy #13
Posted 20 November 2012 - 09:28 AM
Nope, closed.