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

Automatic Wrapping

Started by Noobular, 17 July 2015 - 02:41 PM
Noobular #1
Posted 17 July 2015 - 04:41 PM
How do you connect something through a modem and have it auto connect, like a monitor or reactor ect?

I've seen it done but I have no idea how it's done.

Essentially How my program is going to work is it's going to connect then i'll have it make another line with that blocks name.
KingofGamesYami #2
Posted 17 July 2015 - 05:35 PM
You can find all the connected peripherals by using peripheral.getNames(), then search for the type you want.

For example,

local mon
for k, v in pairs( peripheral.getNames() ) do
  if peripheral.getType( v ) == "monitor" then
    mon = peripheral.wrap( v )
    break
  end
end
if not mon then
  error( "No monitors found", 0 )
end

Also, if you are using a recent version of CC, you can use peripheral.find.
Noobular #3
Posted 19 July 2015 - 02:25 AM
Thank you! peripheral.find is sooo much better then peripheral.wrap atleast more helpful lol, i'm really.. bad with pair's would there be a way to… say I have multiple … reactors connected can i make it so they would be like IDed? so reactor 1 reactor 2 ect so i can call them specifically?
Bomb Bloke #4
Posted 19 July 2015 - 02:46 AM
Sure, you can have perpheral.find() dump them all into a table.

local reactors = {peripheral.find("reactorType")}

for i = 1, #reactors do
  print(reactors[i].getEnergyOrWhatever())
end
Noobular #5
Posted 19 July 2015 - 03:29 AM
I love you bloke <3 , Would you happen to know if Minefactory reloaded, it's blocks connect with modems but i can't find out if they have any function to check their Activity on/off or working or anything …
Bomb Bloke #6
Posted 19 July 2015 - 03:37 AM
Beats me if they have a function for that (call peripheral.getMethods() on one, see what that tells you), but if you have access to Buildcraft's gates, you might be able to get those to emit redstone signals when the machines next to them are doing stuff.
Noobular #7
Posted 19 July 2015 - 03:42 AM
Yea, Problem is Minefactory reloaded doesn't have any redstone activation what-so-ever , and the only methods were the basic ones (i believe i was told they were added from openperipherals..
Bomb Bloke #8
Posted 19 July 2015 - 03:49 AM
I'm saying you'd use the gates to provide a redstone signal to your computer, in the presumed absence of a function to get the activity state.
Noobular #9
Posted 19 July 2015 - 05:10 AM
Ehh Not sure how you'd do that to be honest seeing as they have no effect with redstone at all but then again i haven't messed with the gates other then autartic … I'm having an issue now where when listing the Table of reactors it list them but (this is in a while true do) when i clear the screen and re-write it keeps the other one after i disconnect it from the computer.

Pastebin Link
(monitor size)
Edited on 19 July 2015 - 06:29 PM
Bomb Bloke #10
Posted 19 July 2015 - 05:57 AM
Rather than using sleep()-based loop, use a timer; that opens you up to listen for peripheral / peripheral_detach events, which are your que to re-call peripheral.find().
Noobular #11
Posted 19 July 2015 - 08:35 PM
Now i'm having a problem with this where I'm not sure exactly how to check and see if the one in the table that it's currently going over is still attached i tried something but it doesn't work if you wouldn't mind tweaking it it's still their under ListAll() oh and for some reason after it tells me when a specific one is disconnected / reconnected it won't do any other one it will only work with one being messed with after it's been triggered.

Pastebin Link

Oh also the time is so low because i assumed when it tried to pull the event it wasn't fast enough but its just straight up not pulling the other one.
Noobular #12
Posted 20 July 2015 - 01:45 AM
Ok, so since the last post i figured out that when you do local Event, Eventtype = os.pullEvent() it's a lot easier to do conditions rather then specifically pullEvent("peripheral_detach")

So now it's working with multiple things being attached and detached without any problems my next thing would be , it's still .. i'm not sure if doing r = {} is resetting the table so i can re peripheral.wrap("BigReactors-Reactor") to put them back in the r table so when one is detached it will remove it on the screen but even when doing r = {} or r = {nil} it's not working and keeps the data saved from what i can tell.
Bomb Bloke #13
Posted 20 July 2015 - 02:18 AM
The loop at the bottom of your paste won't do at all; try it like this:

local OSTimer = os.startTimer(0)

while true do
        local event, par1 = os.pullEvent()
	if event == "timer" and par1 == OSTimer then
		m.clear()
		Header()
		ListAll()
		m.setBackgroundColor(colors.black)
		OSTimer = os.startTimer(1)
	elseif event == "peripheral_detach" or event == "peripheral" then
		m = peripheral.find("monitor")
		r = {peripheral.find("BigReactors-Reactor")}
		h = {peripheral.find("harvester")}
		g = {peripheral.find("grinder")}
	end
end
Noobular #14
Posted 20 July 2015 - 03:43 AM
Thank you, yea i couldn't figure out why the tables were so screwy i tried to do that and it didn't work but now it's working for some reason….
Noobular #15
Posted 21 July 2015 - 03:47 PM
Welp having an issue where, i'm trying to make a function that will just essentially be a builder so i can quickly add different types of machines but for some reason it's not looking at some conditions it's passing over them so i assume something with them isn't working right and won't do anything

line 55 is the function and line 136 is where it's being used.
[Running should turn the screen background blue but it stays orange and not running turns the screen red also turns the screen orange.
http://pastebin.com/tseFJvTe
Bomb Bloke #16
Posted 22 July 2015 - 02:53 AM
The function at line 55 is ListMachine(), but the function called at line 136 is FindPeripherals()…?

The calls are on lines 122/123:

        ListMachine("Harvester",#h,r[i],nil)
        ListMachine("Grinder",#g,g[i],nil)

… but what's "i" supposed to be? You aren't in one of your "for" loops here!

I guess you intended to do something like this:

        ListMachine("Harvester",h)
        ListMachine("Grinder",g)

… with a function like this:

function ListMachine(Name,Peripherals)
	comparison2 = comparison1
	for i = 1, #Peripherals do
		m.setBackgroundColor(colors.orange)

		if comparison2 == 1 then
			if Peripherals[i].getActive() then
				Running()
			else
				NotRunning()
			end
		end
		
		m.setCursorPos(1,YPos)
		m.write(Long)
		m.setCursorPos(1,YPos)
		m.write(Name.." #"..i)
		YPos = YPos + 1
	end
end
Balthamel #17
Posted 22 July 2015 - 03:43 AM
personally i prefer eventQueue = {os.pullEvent()} since that allows for all os events. Also for dynamic/on the fly detection you can listen for the event type "peripheral" which fires when a peripheral is attached. You already have that.
Edited on 22 July 2015 - 01:44 AM
flaghacker #18
Posted 22 July 2015 - 08:56 AM

eventQueue = {os.pullEvent()}

'eventQueue' is a really bad name, as that statement doesn't return a list of all events, it returns all parameters of one event. 'event' or 'eventData' would be a better name.
Edited on 22 July 2015 - 06:56 AM
Balthamel #19
Posted 22 July 2015 - 09:12 AM

eventQueue = {os.pullEvent()}

'eventQueue' is a really bad name, as that statement doesn't return a list of all events, it returns all parameters of one event. 'event' or 'eventData' would be a better name.
Good point, i'll change it in future code. eventTable maybe since it's a table not a variable
Edited on 22 July 2015 - 07:13 AM
flaghacker #20
Posted 22 July 2015 - 12:37 PM

eventQueue = {os.pullEvent()}

'eventQueue' is a really bad name, as that statement doesn't return a list of all events, it returns all parameters of one event. 'event' or 'eventData' would be a better name.
Good point, i'll change it in future code. eventTable maybe since it's a table not a variable

Actually it's a variable pointing to a table, but o well…
Noobular #21
Posted 23 July 2015 - 01:25 AM
The function at line 55 is ListMachine(), but the function called at line 136 is FindPeripherals()…?

The calls are on lines 122/123:

		ListMachine("Harvester",#h,r[i],nil)
		ListMachine("Grinder",#g,g[i],nil)

… but what's "i" supposed to be? You aren't in one of your "for" loops here!

I guess you intended to do something like this:

		ListMachine("Harvester",h)
		ListMachine("Grinder",g)

… with a function like this:

function ListMachine(Name,Peripherals)
	comparison2 = comparison1
	for i = 1, #Peripherals do
		m.setBackgroundColor(colors.orange)

		if comparison2 == 1 then
			if Peripherals[i].getActive() then
				Running()
			else
				NotRunning()
			end
		end
		
		m.setCursorPos(1,YPos)
		m.write(Long)
		m.setCursorPos(1,YPos)
		m.write(Name.." #"..i)
		YPos = YPos + 1
	end
end


Yea, that's what i was trying to do it was being a twat when i tried it last, and also you looked at the code late compared to when i posted that it was updated previously from you seeeing it i changed it so i could atleast make it work in the server i've been in. I'll ask if i need more help (hint: i'll need more help)
Noobular #22
Posted 23 July 2015 - 04:50 AM
Sigh, New problem is when it goes over the minefactory reloaded machines are taking so long to go through that they are very obvious when they write again..
This URL will stay the same going on so if you'd like me to put it on the original post i can rather then reposting this 400 times.


https://www.youtube....eature=youtu.be

I don't know if there is an actual fix for this because i believe this is probably because of the conditional i put in for the minefactory machines but if not i don't really know if i should use this as you see the reactors are instantly their and have no flicker of any kind.
Edited on 23 July 2015 - 02:54 AM
Balthamel #23
Posted 23 July 2015 - 04:57 AM
The big reactor information calls definately do not yield, i can only assume the mfr ones do yield which is causing the lag. unless i am mistaken. I don't have this mfr functionality installed on my minecraft, try using

a=os.clock()
for i=1,3
b=os.clock()
print(b-a)
Peripherals.getEnergyStored()
a=os.clock()
print(a - B)/>
Peripherals.getEnergyStored()
end
and see if it takes ticks a noticable time to run
if it does then the only solution i can think of is coroutines.
Edited on 23 July 2015 - 03:40 AM
Noobular #24
Posted 23 July 2015 - 05:15 AM
The big reactor information calls definately do not yield, i can only assume the mfr ones do yield which is causing the lag. unless i am mistaken. I don't have this mfr functionality installed on my minecraft, try using

a=os.clock()
for i=1,3
b=os.clock()
print(b-a)
Peripherals.getEnergyStored()
a=os.clock()
print(a- B)/>/>
Peripherals.getEnergyStored()
end
and see if it takes ticks a noticable time to run
if it does then the only solution i can think of is coroutines.



local g = {peripheral.find("grinder")}
function ListMachine(Peripherals)
  a=os.clock()
	    for i = 1, 3 do
    b=os.clock()
   print(b-a)
   Peripherals.getEnergyStored()
   a=os.clock()
   print(a- B)/>
   Peripherals.getEnergyStored()
	    end
end
ListMachine(g)

line 8 apparently is calling a nil

By the way i'm sorry for how the code looks in
 and pastebin for some reason when i copy it doesn't copy exactly how mine looks vs how it ends up.
Bomb Bloke #25
Posted 23 July 2015 - 05:35 AM
A couple of ways around it; either perform all the calls, save the results to a table, then render them all in one go - or just don't clear the whole display, do it a line at a time.

The latter's faster and easier to code (you'd be modifying just a few lines), though the former would do a more thorough job of eliminating the problem. Either way you choose, done correctly, there's no need to have any flicker at all.
Balthamel #26
Posted 23 July 2015 - 05:40 AM
you capitalised a-B
B is totally different to b
what the hell, i TYPE (a- b )and it saves (a- B )/>
Edited on 23 July 2015 - 03:43 AM
Noobular #27
Posted 23 July 2015 - 06:03 AM
you capitalised a-B
B is totally different to b
what the hell, i TYPE (a- b )and it saves (a- B )/>
Yea there were a few problems and i removed them but it still was giving me
A couple of ways around it; either perform all the calls, save the results to a table, then render them all in one go - or just don't clear the whole display, do it a line at a time.

The latter's faster and easier to code (you'd be modifying just a few lines), though the former would do a more thorough job of eliminating the problem. Either way you choose, done correctly, there's no need to have any flicker at all.

I mean i could remove the m.clear() but then again it would cause old machines not connected to be displayed when they shouldn't be.
Noobular #28
Posted 23 July 2015 - 06:14 AM
Ahh also it seems tables can only hold 9 items, that is without going into subtables?
Balthamel #29
Posted 23 July 2015 - 06:52 AM
tables can hold as many values as they have keys.
you can clear individual pixels on a monitor by typing a space " " to that pixel. if you do that whenever a machine disconnects it should keep the monitor clean.
Edited on 23 July 2015 - 04:52 AM
Bomb Bloke #30
Posted 23 July 2015 - 07:48 AM
Well, I was thinking term.clearLine(), but yeah. Point is not to clear the whole screen before attempting to redraw everything. Just clear each part immediately before you're wanting to draw over it.
Balthamel #31
Posted 23 July 2015 - 08:06 AM
Or clear the line when the machine diconnects/the table entry is removed.
Noobular #32
Posted 23 July 2015 - 08:35 AM
Can you use clearLine() on monitors?
Balthamel #33
Posted 23 July 2015 - 08:52 AM
Can you use clearLine() on monitors?
Yes
http://computercraft.info/wiki/Term.clearLine
Edited on 23 July 2015 - 06:52 AM
Noobular #34
Posted 23 July 2015 - 08:58 AM
Actually I'm now, just clearing on detach and attaching of machines but the problem now is that for some reason it still won't write other then when it's cleared which shouldn't be happening since the clearing and writing are seperate anyways.

[It's updated on the paste]
Edited on 23 July 2015 - 06:59 AM
Balthamel #35
Posted 23 July 2015 - 09:00 AM
I remember similar issues, your loop will be structured wrong so that it draws after clearing instead of drawing after a change.
Noobular #36
Posted 23 July 2015 - 09:07 AM
It doesn't draw after a change it just keeps drawing , atleast that's how it's made , but for some reason it won't do it.
RunThrough() is the draw function.

while true do
     m.setBackgroundColor(colors.black)
     local Event, EventType = os.pullEvent()
     if Event == "peripheral_detach"  then
          term.setTextColor(colors.orange)
          print(EventType.." has been detached.")
          print("Screen has been reset")
          FindPeripherals()
          m.clear()
       elseif Event == "peripheral" then
          term.setTextColor(colors.blue)
          print(EventType.." has been attached.")
          print("Screen has been reset")
          FindPeripherals()
          m.clear()
       elseif Event == "timer" then
          OSTimer = os.startTimer(1)
     end

  if CONNECTED2 == 1 then
	Connected2()
	CONNECTED2 = 0
  end

  RunThrough()
end
Edited on 23 July 2015 - 07:08 AM
Balthamel #37
Posted 23 July 2015 - 09:50 AM
First thing i see
os.pullEvent() needs to be in {}


Second glance
you m.clear() right after you FindPeripherals()? won't that clear the moment you write to it?

Third thing
no os.pull does NOT have to be in {} but you will drop results if you just have two variables to hold the results it can return up to 6 depending on the event.
Edited on 23 July 2015 - 07:55 AM
flaghacker #38
Posted 23 July 2015 - 10:49 AM
Third thing
no os.pull does NOT have to be in {} but you will drop results if you just have two variables to hold the results it can return up to 6 depending on the event.

Not if you don't need 6 parameters for the events you're using.

Second glance
you m.clear() right after you FindPeripherals()? won't that clear the moment you write to it?

No, RunThrough is his render function.
Edited on 23 July 2015 - 08:53 AM
Balthamel #39
Posted 23 July 2015 - 11:52 AM
Does it print that a peripheral is attached or detached?
Noobular #40
Posted 23 July 2015 - 10:32 PM
Does it print that a peripheral is attached or detached?

Yes.

Does it print that a peripheral is attached or detached?

Also, if you click any of the "Pastebin here" links they all lead to the same link and it's updated.
Balthamel #41
Posted 24 July 2015 - 02:16 PM
Okay now i'm actually awake, are you still having issues or have you solved it?
Noobular #42
Posted 24 July 2015 - 10:27 PM
I'm still having the same issue.