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

Death's Programs!

Started by Alice, 11 November 2013 - 11:42 AM
Alice #1
Posted 11 November 2013 - 12:42 PM
This is going to be a post where I post all of my programs until I get a website hosted for them.
All of my programs are going to be random programs that you can use for any random reason.
Just send me a message or reply to this post if you have a suggestion/bug for me.
Peripherals program
Detects peripherals
SpoilerUsage: peripherals [side1, side2, …] <–optional arguments

local a = { ... }
local s = rs.getSides()
local api -- Make this variable TRUE if you want to use it as an API.

--This overwrites print ONLY if the variable above is true.
oldPrint = print
if api==true then
print = function() end
end

if #a<1 then
for i=1, #s do
  print("Searching for peripheral on " .. s[i] .. ".")
  if peripheral.isPresent(s[i]) then print("Peripheral " .. peripheral.getType(s[i]) .. " is present on " .. s[i] .. ".")
   else print("No peripheral present on " .. s[i] .. ".")
  end
end
end

if a~=nil and not api == true then
for q=1, #a do
  for i=1, #s do
   if a[i] == s[q] then
	print("Searching for peripheral on " .. a[i] .. ".")
	if peripheral.isPresent(a[i]) then print("Peripheral " .. peripheral.getType(a[i]) .. " is present on " .. a[i] .. ".")
	 else print("No peripheral present on " .. a[i] .. ".")
	end
   end
  end
end
end

print = oldPrint
More to come! A lot of them will be misc. programs I randomly thought of!
No pastebin, sorry!
SpoilerAll of this code is fully available to anyone and everyone who does not claim this work as theirs. Thanks to Dan200 for rs.getSides(), ardera for my print() workaround!
Edited on 13 December 2013 - 09:08 AM
TechMasterGeneral #2
Posted 11 November 2013 - 03:43 PM
Pretty Cool… I may decide to use this in the program i'm working on… if your okay with that.. I'll put you in the credits.
Alice #3
Posted 21 November 2013 - 12:11 PM
No problem. I plan on making a bunch more once I'm ungrounded.
Alice #4
Posted 13 December 2013 - 08:58 AM
Bump for making Peripherals into an API.
Just change local api to local api = true!
Also, could someone test this please?
Csstform #5
Posted 13 December 2013 - 09:29 AM
I have one that works just as well, but only 6 lines of code. :P/>
ardera #6
Posted 13 December 2013 - 09:30 AM
I think your "Detects Peripherals" program will crash when you launch it with api = true, because you set print to nil if api is true, and when you use print then, it will throw an "attempt to call nil" error, I think. Instead, use

print = function() end
It will set print to a clear function, that does nothing.
Alice #7
Posted 13 December 2013 - 09:48 AM
Thanks; I can't test it right now, but I'm fixing the post.
Fixed, can someone test both the normal program using peripherals <someside> and peripherals, and try the API?
Edited on 13 December 2013 - 08:49 AM
Csstform #8
Posted 13 December 2013 - 09:54 AM
Does this do the same thing, or am I missing the point of yours? Yours IS more user freindly.

http://pastebin.com/2FbbdJur
Edited on 13 December 2013 - 08:55 AM
Cranium #9
Posted 13 December 2013 - 10:00 AM
your code won't work. You redefine print as
print = function() end
, and then call back to that print function you just overwrote. I don't see why you are doing that…there's no point with the code you have shown.
Alice #10
Posted 13 December 2013 - 10:02 AM
The program is able to be used as an API and as a program.
It's only overwritten if api is true, where you wouldn't want to have the text print to the screen.

Also, castform, I'm not able to use pastebin with proxy :D/> Please use hastebin or
Spoiler
Put code in these :D/>/>
Currently trying to find a way to implement into an API, but class is over so I will work on it later.
Edited on 13 December 2013 - 09:08 AM
Csstform #11
Posted 13 December 2013 - 10:38 AM
Spoiler

function wrapPeripherals()
        peripheral.wrap("left")
        peripheral.wrap("right")
        peripheral.wrap("front")
        peripheral.wrap("back")
        peripheral.wrap("top")
        peripheral.wrap("bottom")
end
Lyqyd #12
Posted 13 December 2013 - 10:57 AM

function wrapPeripherals()
        peripheral.wrap("left")
        peripheral.wrap("right")
        peripheral.wrap("front")
        peripheral.wrap("back")
        peripheral.wrap("top")
        peripheral.wrap("bottom")
end

That accomplishes literally nothing at all.
Csstform #13
Posted 13 December 2013 - 12:42 PM

function wrapPeripherals()
        peripheral.wrap("left")
        peripheral.wrap("right")
        peripheral.wrap("front")
        peripheral.wrap("back")
        peripheral.wrap("top")
        peripheral.wrap("bottom")
end

That accomplishes literally nothing at all.
I thought that's how you wrap peripherals. Hmm. Silly me, I thought I found something easy about programming.
Lyqyd #14
Posted 13 December 2013 - 03:41 PM
You have to do something with the value it returns. Otherwise, you're just creating and discarding six tables for no reason.
Alice #15
Posted 13 December 2013 - 05:43 PM
This would be a better version of wrapping the peripherals anyway

local sides = rs.getSides()
for i=1, #sides do
peripheral.wrap(sides[i])
end
:D/>
Bomb Bloke #16
Posted 13 December 2013 - 06:46 PM
If it didn't have exactly the same problem as Castform's code, maybe. ;) I'm also not sure what you're doing with the redstone API there…

To be clear, "peripheral.wrap()" returns something. If you don't store that result somewhere, it simply gets discarded.

For example, let's say you wanted to recycle the "sides" table to hold the wrapped peripherals instead of just their names:

local sides = peripheral.getNames()
for i=1, #sides do
  sides[i] = peripheral.wrap(sides[i])
end
Alice #17
Posted 13 December 2013 - 09:32 PM
I use the rs.getSides() function just to get a table with the sides.
theoriginalbit #18
Posted 13 December 2013 - 09:49 PM
You're better off using peripheral.getNames since it gives you a table of the sides and network names of present peripherals, removing the need for presence checking (which is also redundant when type checking since if it is not present it returns nil, and nil never equals the type string you're checking)

Take a look at this example that I've made earlier. More info can be found on my thread under code snippets > peripheral search
Edited on 14 December 2013 - 12:37 AM
Alice #19
Posted 13 December 2013 - 10:52 PM
Cool, when I feel up to it I'll add it, but I'm just barely able to pop on and off Forums / IRC