Jump to content




Help with Multitasking Redstone Input/Outputs

help computer

  • You cannot reply to this topic
36 replies to this topic

#1 Chloe

  • Members
  • 28 posts

Posted 03 January 2014 - 07:40 PM

I am new and trying to get my Coal/Wood Factory to work with computercraft, and I want it to do multiple things at once. So far, I have only figured out how to do things by making them all in one big loop. But the problem (as I can imagine there are many) is that in some instances, it has to wait for other events to happen before other inputs/outputs can do what they need to do.

I would like help with this code in getting it more functional.

It reads the amount of contents in my chest, then it makes more wood/coal/coke as needed, up to the 6912 limit of the chest (full). I don't want the chest to fill up again until the chest has reach the last stack of 64. Then it should pulse the "makeWood" or "makeCoal" or the "makeCoke". I want it to pulse quickly, but not too fast, because I have a lamp attached to the system and I want to see it flash.

The problem I am running into right now is, once it starts to make COKE, the computer wont do anything else, and I need it to multitask read the other inputs and reacting accordingly, updating inventory amounts and pulsing the other outputs so they can do/make other items at the same time.


I have started the program... so any help you can provided is appreciated.



-- CLEAR & INITIALIZE TERMINAL SCREEN
term.clear()
term.setCursorPos(1,1)
print "Loader Control System v1.0"
term.setCursorPos(1,3)
print "System: Online"

-- CLEAR & INITIALIZE MONITOR SCREEN (TOP)
local mon = peripheral.wrap("top")
mon.clear()
mon.setTextScale(1)
mon.setCursorPos(3,1)
mon.write "Loader Control System v1.0"
mon.setCursorPos(3,3)
mon.write "System: Online"

-- SET BUNDLED CABLE TO "BOTTOM" AND INITIALIZE IN/OUTPUTS (OFF)
		rs.setBundledOutput( "bottom", 0 )

-- SET BUNDLEDCABLE TO "BOTTOM"
   local CablePort = "bottom"

-- FUNCTIONS
-- PULSE
  local function pulse(side, colors, time)
  rs.setBundledOutput(side, colors)
  sleep(time / 2)
  rs.setBundledOutput(side, 0) -- 0 means no colors, so it turns the output off
  sleep(time / 2)
				end

-- SET LOOP
while true do


-- FUNCTIONS
-- PULSE
  local function pulse(side, colors, time)
  rs.setBundledOutput(side, colors)
  sleep(time / 2)
  rs.setBundledOutput(side, 0) -- 0 means no colors, so it turns the output off
  sleep(time / 2)
		   end

-- INITIALIZE COALCOKE CHEST INVENTORY COUNTER WITH CURRENT INVENTORY COUNT
os.loadAPI("ocs/apis/sensor")
local chestCokeCount = sensor.wrap("left")

		cokecount = 0
		details = chestCokeCount.getTargetDetails("-6,0,-1")
		for i = 1, #details.Slots do
		if details.Slots[i].Size then
cokecount = cokecount + details.Slots[i].Size
		  
	end
		end


-- PRINT CURRENT COALCOKE INVENTORY COUNT TO TERMINAL SCREEN
term.setCursorPos(1,5)
print("Coalcoke Inventory:", cokecount)
-- PRINT CURRENT COALCOKE INVENTORY COUNT TO MONITOR SCREEN (TOP)
mon.setCursorPos(3,5)
		mon.write "Coalcoke Inventory:"
		mon.setCursorPos(22,5)
		mon.write(tostring(cokecount))
-- BUNDLED CABLE "INPUTS"	
local rebootSystemButton = redstone.testBundledInput(CablePort, colors.lime)
local resetCounterButton = redstone.testBundledInput(CablePort, colors.yellow)
	
-- BUNDLED CABLE "OUTPUTS"
local makeWood = redstone.testBundledInput(CablePort, colors.brown)
local makeCoal = redstone.testBundledInput(CablePort, colors.black)
local makeCoke = redstone.testBundledInput(CablePort, colors.gray)
local loadwood = redstone.testBundledInput(CablePort, colors.orange)
local loadCoal = redstone.testBundledInput(CablePort, colors.blue)
local loadCoke = redstone.testBundledInput(CablePort, colors.lightBlue)

-- EVENTS
os.pullEvent("redstone")


-- REBOOT BUTTON CODE
  if rebootSystemButton == true then

--REBOOT
  term.clear()
  mon.clear()
  os.reboot()

-- RESET BUTTON CODE
  elseif resetCounterButton == true then

-- SETS THE SAVE FILE FOR COUNTER BACK TO ZERO
  cokecount = 0

	  
-- CLEAR & INITIALIZE TERMINAL SCREEN
  term.clear()
  term.setCursorPos(1,1)
  print "Loader Control System v1.0"
  term.setCursorPos(1,3)
  print "System: Online"
-- CLEAR & INITIALIZE MONITOR SCREEN (TOP)
  local mon = peripheral.wrap("top")
  mon.clear()
  mon.setTextScale(1)
  mon.setCursorPos(3,1)
  mon.write "Loader Control System v1.0"
  mon.setCursorPos(3,3)
  mon.write "System: Online"

-- PRINT CURRENT COALCOKE INVENTORY COUNT TO TERMINAL SCREEN
  term.setCursorPos(1,5)
  print("Coalcoke Inventory:", cokecount)
-- PRINT CURRENT COALCOKE INVENTORY COUNT TO MONITOR SCREEN (TOP)
  mon.setCursorPos(3,5)
  mon.write "Coalcoke Inventory:"
  mon.setCursorPos(22,5)
  mon.write(tostring(cokecount))

-- COUNT COALCOKE

term.setCursorPos(20,5)
print(cokecount)
mon.setCursorPos(22,5)
mon.write(tostring(cokecount))

-- DUPLICATE & LOAD COKE INTO CHESTS (OUTPUT)
   elseif cokecount <= 512 then
for i = 1, 6400 do
pulse(CablePort, colors.gray, .500) -- make a .5-second pulse and make COKE on Gray wire
		sleep(0.5)

		os.loadAPI("ocs/apis/sensor")
		local chestCokeCount = sensor.wrap("left")
		--# reset chest/item count
		cokecount = 0
		--# get target details for X, Y, Z location.
		details = chestCokeCount.getTargetDetails("-6,0,-1")
		--# iterate the list of slots.
		for i = 1, #details.Slots do
				if details.Slots[i].Size then
						--# add items to count
						cokecount = cokecount + details.Slots[i].Size

term.setCursorPos(1,5)
term.clearLine()
term.setCursorPos(1,5)
		print("Coalcoke Inventory:", cokecount)
mon.setCursorPos(1,5)
mon.clearLine()
		mon.setCursorPos(3,5)
		mon.write "Coalcoke Inventory:"
		mon.setCursorPos(22,5)
		mon.write(tostring(cokecount))
	  end
end
	  end
	end
end

Edited by chloeprince, 03 January 2014 - 07:42 PM.


#2 CometWolf

  • Members
  • 1,283 posts

Posted 03 January 2014 - 10:49 PM

I had to do some serious spaghetti cleanup, couldn't work with that code lol
-- SETTINGS
local tSettings = {
  ["cokeCheckTime"] = 5 --secs
}
-- FUNCTIONS
-- PULSE
local tTimers = {}
local tSide = {}
local function pulse(side, colors, time)
  rs.setBundledOutput(side, colors)
  table.insert(tTimers,os.startTimer(time / 2))
  table.insert(tSide,side)
end
-- CHECKS COALCOKE CHEST INVENTORY AND UPDATES COUNTER WITH CURRENT INVENTORY COUNT
local checkTimer
function updateCoke()
  cokeCount = 0
  details = chestCokeCount.getTargetDetails("-6,0,-1")
	for i = 1, #details.Slots do
	if details.Slots[i].Size then
	  cokeCount = cokeCount + details.Slots[i].Size
	end
  end
  -- PRINT CURRENT COALCOKE INVENTORY COUNT TO TERMINAL SCREEN
  term.setCursorPos(1,5)
  term.clearLine()
  print("Coalcoke Inventory:", cokecount)
  -- PRINT CURRENT COALCOKE INVENTORY COUNT TO MONITOR SCREEN (TOP)
  mon.setCursorPos(3,5)
  mon.clearLine()
  mon.write "Coalcoke Inventory:"
  mon.setCursorPos(22,5)
  mon.write(tostring(cokecount))
  checkTimer = os.startTimer(tSettings.cokeCheckTime))
end
-- CLEAR & INITIALIZE TERMINAL
term.clear()
term.setCursorPos(1,1)
print "Loader Control System v1.0"
term.setCursorPos(1,3)
print "System: Online"
-- CLEAR & INITIALIZE MONITOR SCREEN (TOP)
local mon = peripheral.wrap("top")
mon.clear()
mon.setTextScale(1)
mon.setCursorPos(3,1)
mon.write "Loader Control System v1.0"
mon.setCursorPos(3,3)
mon.write "System: Online"
-- SET BUNDLED CABLE TO "BOTTOM" AND INITIALIZE IN/OUTPUTS (OFF)
local cablePort = "bottom"
rs.setBundledOutput( cablePort, 0 )
-- SENSOR SETUP
os.loadAPI("ocs/apis/sensor")
local chestCokeCount = sensor.wrap("left")
-- PROGRAM BEGINS
local cokeCount = 0
updateCoke()
-- SET LOOP
while true do
  -- EVENTS
  local tEvent = {os.pullEvent()}
  if tEvent[1] == "redstone" then
	-- REDSTONE
	-- BUNDLED CABLE "INPUTS"	  
	local rebootSystemButton = redstone.testBundledInput(cablePort, colors.lime)
	local resetCounterButton = redstone.testBundledInput(cablePort, colors.yellow
	-- REBOOT BUTTON CODE
	if rebootSystemButton == true then
	  --REBOOT
	  mon.clear()
	  os.reboot()
	-- RESET BUTTON CODE
	elseif resetCounterButton == true then
	  -- SETS THE VARIABLE FOR COUNTER BACK TO ZERO
	  cokecount = 0
	  -- PRINT CURRENT COALCOKE INVENTORY COUNT TO TERMINAL SCREEN
	  term.setCursorPos(1,5)
	  term.clearLine()
	  print("Coalcoke Inventory:", cokecount)
	  -- PRINT CURRENT COALCOKE INVENTORY COUNT TO MONITOR SCREEN (TOP)
	  mon.setCursorPos(3,5)
	  term.clearLine()
	  mon.write "Coalcoke Inventory:"
	  mon.setCursorPos(22,5)
	  mon.write(tostring(cokecount))
   -- DUPLICATE & LOAD COKE INTO CHESTS (OUTPUT)
  elseif tEvent[1] == "timer" then
	-- TIMERS
	if tEvent[2] == checkTimer then
	  updateCoke()
	  if cokecount <= 512 then
		for i = 1, 6400 do
		pulse(CablePort, colors.gray, .500) -- make a .5-second pulse and make COKE on Gray wire
	  end
	elseif #tTimers > 0 then
	  for i=1,#tTimers do
		if tEvent[2] == tTimers[i] then
		  rs.setBundledOutput(tSide[i],0)
		  table.remove(tTimers,i)
		  table.remove(tSide,i)
		  break
		end
	  end
	end
  end
end
I haven't tested this, but it should do the trick, just configure the check timer however you like.
What i've done is, instead of sleep, i use os.startTimer(). Then i use the os.pullEvent() to pull all events, not just redstone. When a timer event fires, besides the recheck timer, it will set the redstone side saved in tSide with the same number as the timer has in the tTimer table, to 0.

Edited by CometWolf, 04 January 2014 - 12:43 AM.


#3 Chloe

  • Members
  • 28 posts

Posted 04 January 2014 - 09:21 AM

Thanks for the help. I really appreciate it.

The code is not working - there were some issues with unexpected symbol on this line:

checkTimer = os.startTimer(tSettings.cokeCheckTime))

changed it to this:

checkTimer = os.startTimer(tSettings.cokeCheckTime)

also, there were more than a couple "end's" missing at the end to close if statements, etc. Not sure if I got that right were to add them, but I placed them at the end and it resolved the computer to stop giving me the error.

Additionally, you took out some needed things for the Sensor on the Left to actually get the information from the chest - I was getting an error that said it was attempting to index a nil value. I added the API back in, however, instead of the NIL value, now I get nothing counting, and it says NIL on the monitor up top.

My setup for the computercraft computer is a Monitor ontop, a bundled cable hooked to the bottom, an "openCCsensor" radar dish on the left side with an "Inventory Card x4" in the radar dish.... a Chest with some coke in it nearby (run /ocs/programs/sensorview to find target location) and a gray jacketed wire coming out of the bundle for pulsing a filter that extracts coke from it out of a duplicator when pulsed. A lime jacketed wire hooked to a button and a yellow jacketed wire hooked to another button. I need to eventually put in the other 2 chest for the wood and coal. Wood fires on the brown jacketed wire and the coal fires on the black jacketed wire.



Here is what I am working with now, based on what you gave me, and what I had to add in at the top to get things to work, some...



-- CLEAR & INITIALIZE TERMINAL SCREEN
term.clear()
term.setCursorPos(1,1)
print "Loader Control System v1.0"
term.setCursorPos(1,3)
print "System: Online"

-- CLEAR & INITIALIZE MONITOR SCREEN (TOP)
local mon = peripheral.wrap("top")
mon.clear()
mon.setTextScale(1)
mon.setCursorPos(3,1)
mon.write "Loader Control System v1.0"
mon.setCursorPos(3,3)
mon.write "System: Online"

-- SET BUNDLED CABLE TO "BOTTOM" AND INITIALIZE IN/OUTPUTS (OFF)
	    rs.setBundledOutput( "bottom", 0 )

-- SET BUNDLEDCABLE TO "BOTTOM"
	    local CablePort = "bottom"



-- SETTINGS
local tSettings = {
  ["cokeCheckTime"] = 5 --secs
}
-- FUNCTIONS
-- PULSE
local tTimers = {}
local tSide = {}
local function pulse(side, colors, time)
  rs.setBundledOutput(side, colors)
  table.insert(tTimers,os.startTimer(time / 2))
  table.insert(tSide,side)
end

-- CHECKS COALCOKE CHEST INVENTORY AND UPDATES COUNTER WITH CURRENT INVENTORY COUNT
local checkTimer
function updateCoke()
  os.loadAPI("ocs/apis/sensor")
  local chestCokeCount = sensor.wrap("left")
  cokeCount = 0
  details = chestCokeCount.getTargetDetails("-6,0,-1")
	    for i = 1, #details.Slots do
	    if details.Slots[i].Size then
		  cokeCount = cokeCount + details.Slots[i].Size
	    end
  end

  -- PRINT CURRENT COALCOKE INVENTORY COUNT TO TERMINAL SCREEN
  term.setCursorPos(1,5)
  term.clearLine()
  print("Coalcoke Inventory:", cokecount)


  -- PRINT CURRENT COALCOKE INVENTORY COUNT TO MONITOR SCREEN (TOP)
  mon.setCursorPos(3,5)
  mon.clearLine()
  mon.write "Coalcoke Inventory:"
  mon.setCursorPos(22,5)
  mon.write(tostring(cokecount))
  checkTimer = os.startTimer(tSettings.cokeCheckTime)
end

-- CLEAR & INITIALIZE TERMINAL
term.clear()
term.setCursorPos(1,1)
print "Loader Control System v1.0"
term.setCursorPos(1,3)
print "System: Online"

-- CLEAR & INITIALIZE MONITOR SCREEN (TOP)
local mon = peripheral.wrap("top")
mon.clear()
mon.setTextScale(1)
mon.setCursorPos(3,1)
mon.write "Loader Control System v1.0"
mon.setCursorPos(3,3)
mon.write "System: Online"

-- SET BUNDLED CABLE TO "BOTTOM" AND INITIALIZE IN/OUTPUTS (OFF)
local cablePort = "bottom"
rs.setBundledOutput( cablePort, 0 )
-- SENSOR SETUP
os.loadAPI("ocs/apis/sensor")
local chestCokeCount = sensor.wrap("left")
-- PROGRAM BEGINS
local cokeCount = 0
updateCoke()
-- SET LOOP
while true do
  -- EVENTS
  local tEvent = {os.pullEvent()}
  if tEvent[1] == "redstone" then
	    -- REDSTONE
	    -- BUNDLED CABLE "INPUTS"		
	    local rebootSystemButton = redstone.testBundledInput(cablePort, colors.lime)
	    local resetCounterButton = redstone.testBundledInput(cablePort, colors.yellow)
	    -- REBOOT BUTTON CODE
	    if rebootSystemButton == true then
		  --REBOOT
		  mon.clear()
		  os.reboot()
	    -- RESET BUTTON CODE
	    elseif resetCounterButton == true then
		  -- SETS THE VARIABLE FOR COUNTER BACK TO ZERO
		  cokecount = 0
		  -- PRINT CURRENT COALCOKE INVENTORY COUNT TO TERMINAL SCREEN
		  term.setCursorPos(1,5)
		  term.clearLine()
		  print("Coalcoke Inventory:", cokecount)
		  -- PRINT CURRENT COALCOKE INVENTORY COUNT TO MONITOR SCREEN (TOP)
		  mon.setCursorPos(3,5)
		  term.clearLine()
		  mon.write "Coalcoke Inventory:"
		  mon.setCursorPos(22,5)
		  mon.write(tostring(cokecount))
   -- DUPLICATE & LOAD COKE INTO CHESTS (OUTPUT)
  elseif tEvent[1] == "timer" then
	    -- TIMERS
	    if tEvent[2] == checkTimer then
		  updateCoke()
		  if cokecount <= 512 then
			    for i = 1, 6400 do
			    pulse(CablePort, colors.gray, .500) -- make a .5-second pulse and make COKE on Gray wire
		  end
	    elseif #tTimers > 0 then
		  for i=1,#tTimers do
			    if tEvent[2] == tTimers[i] then
				  rs.setBundledOutput(tSide[i],0)
				  table.remove(tTimers,i)
				  table.remove(tSide,i)
				  break
			    end
end
end
		  end
	    end
  end
end

Edited by chloeprince, 04 January 2014 - 09:23 AM.


#4 CometWolf

  • Members
  • 1,283 posts

Posted 04 January 2014 - 10:07 AM

View Postchloeprince, on 04 January 2014 - 09:21 AM, said:

The code is not working - there were some issues with unexpected symbol on this line:

checkTimer = os.startTimer(tSettings.cokeCheckTime))

changed it to this:

checkTimer = os.startTimer(tSettings.cokeCheckTime)

also, there were more than a couple "end's" missing at the end to close if statements, etc. Not sure if I got that right were to add them, but I placed them at the end and it resolved the computer to stop giving me the error.

Yeah, that was bound to happen since i didn't test it. anyways, the part about just placing the ends at the end.... sounds like a really bad idea.

View Postchloeprince, on 04 January 2014 - 09:21 AM, said:

Additionally, you took out some needed things for the Sensor on the Left to actually get the information from the chest - I was getting an error that said it was attempting to index a nil value. I added the API back in, however, instead of the NIL value, now I get nothing counting, and it says NIL on the monitor up top.
probably related to the ends you added, as i did not remove the wrap or os.loadAPI, i simply placed these before the loop, as they are only needed once. No reason to wrap the same sensor to the same side again, or load the same api again.

Gimme a few minutes and i'll have a look into it

#5 Chloe

  • Members
  • 28 posts

Posted 04 January 2014 - 10:13 AM

I'm learning... lol

I really appreciate your help, and any notes you include in the code that explain what is going on. I really want to learn, and a few weeks ago, I didn't know anything. I have been picking things up along the way from this forum and people that have helped me. The code I wrote, was a conglomerate of other lessons that I have pieced together. I know I need to learn the basics - so, your feedback is very appreciated... and your patients with me. :)

Edited by chloeprince, 04 January 2014 - 10:13 AM.


#6 CometWolf

  • Members
  • 1,283 posts

Posted 04 January 2014 - 10:34 AM

No worries, i enjoy anything related to coding anyways.
Give this one a go http://pastebin.com/nn3ivk50, i couldn't test it cause the openCCSensors included in ftb monster is derped. My computer dosen't even have the ocs folder lol.
Also, you might have to make some changes once you get more redstone outputs in there. Cause when you set a redstone cable to a specific color or 0, that is the only color it will power on. Using the colors.combine and colors.subtract functions is probably the best way to go about doing this. http://computercraft...olors_%28API%29

#7 Chloe

  • Members
  • 28 posts

Posted 04 January 2014 - 11:21 AM

Same problem.

These items must be at the top first - otherwise, it still gives me an error that its trying to index a nil value. Once I put them at the top of the code, then that error goes away. However, it still prints nothing to the screen, and I can't even force it to print a value. I know the chest target location is correct. When I run my original code that I had first posted, I can at least get the chest inventory count.

local mon = peripheral.wrap("top")
os.loadAPI("ocs/apis/sensor")
local chestCokeCount = sensor.wrap("left")

It still is not counting or acquiring anything.
If I press the reset or Reboot button, it ends the program with an error.


What I think maybe the problem, is the Tables


Also, I believe they fixed the issue with openCCsensors. There is a new version you can download, and place the file in your FTB mod pack folder. It fixes the issue.

Edited by Chloe, 04 January 2014 - 11:26 AM.


#8 Moody

  • Members
  • 28 posts

Posted 04 January 2014 - 11:52 AM

Hey there!
Regarding your original code:
You should really try to use more functions, makes your life easier. (e.g. use a function to print your Startupscreen, so you dont have to do it all over again)
I found it pretty important to just to try to make a function out of everything - otherwise my code just tends to clump up, repeats or just take much time to understand. (especially if your code grows bigger)
Also: Dont load an API to a subfunction as this will load it everytime you call the subfunction - instead load it once at the start of your mainprogram. (In CometWolf's code - probably a mistake?)

To the topic:
With this code you cant really conveniently handle inputs WHILE it is processing a task, because the event listener will block everything.
Try the Parallel API ( http://computercraft.../Parallel_(API) ) and run a job processing queue (i would advice to create a real jobqueue, e.g. with a table - you can save & load after a restart of minecraft) und run an eventlistener and handler parallel to it.

Edited by Moody, 04 January 2014 - 11:56 AM.


#9 CometWolf

  • Members
  • 1,283 posts

Posted 04 January 2014 - 11:53 AM

turns out monster dosen't include openCCS, so i just installed it lol.
Anyways, after some testing, i have fixed it.
http://pastebin.com/mqb06tc2
for some reason wrapping stuff to local variables and then calling them within a function later dosen't work. I suspect this is because the function was global and the variable was local, but anyhow i just changed the variables to global.
And the text was printing wrong cause i wrote cokecount instead of cokeCount, derp.

However, lookin at the code a bit more closely... i noticed you want to make 6400 pulses in a row, so we'll need to do some modifications to account for that aswell. Like preventing it from triggering more pulses while it's still making the original set, aswell as making it pause inbetween each pulse without using sleep. Right now it will jsut make all the pulses at the same time.

#10 Chloe

  • Members
  • 28 posts

Posted 04 January 2014 - 12:15 PM

Ok, that is working better.

However, once it sees the chest is less that 512, it send one solid pulse of red stone, instead of oscillating the count of pulses.

Let me future explain. The point of the reset button was to eventually CLEAR the inventory completely of all items and rest the count. The reboot button is just to reboot the computer. I know there is more code needed to clear the inventory - however, I am not aware of it yet....

I need to study how functions work and tables work. This is my weakness.

Anyhow, that is where I am at.

I'm off to Chicago now, so chat in 24hrs. I have access to the forum on my iphone, but wont be able to test until I get back.

THANK YOU! *hugs*

Edited by Chloe, 04 January 2014 - 12:17 PM.


#11 CometWolf

  • Members
  • 1,283 posts

Posted 04 January 2014 - 12:22 PM

Yeah that happens because the previous code was meant for single pulses each time the function was called. I've fixed this now, so give this one a try.
http://pastebin.com/pHm0RRVr
i added some comments so it's easier to understand what im doing. I can't test it myself btw, as ftb monster does not have cc supported bundled cables.

#12 Moody

  • Members
  • 28 posts

Posted 04 January 2014 - 12:35 PM

View PostCometWolf, on 04 January 2014 - 11:53 AM, said:

for some reason wrapping stuff to local variables and then calling them within a function later dosen't work. I suspect this is because the function was global and the variable was local, but anyhow i just changed the variables to global.

actually, local variables are just defined in the block they were declared - you can even do the following:

  local x, y = 1, 10
	if x<y then
	  print(x)   -- prints  "1"
	  local x   = nil
	  print(x)   -- prints "nil"
	end		  -- ends the block started with 'then'
	print(x,y)   -- prints  1   10

so changes made to local variables dont even persist after their block is finished

Edited by Moody, 04 January 2014 - 12:37 PM.


#13 CometWolf

  • Members
  • 1,283 posts

Posted 04 January 2014 - 12:48 PM

Indeed, but if your read the program, you would see that the variables are declared in the main block of the program, same as the functions which used them. Ergo it should have worked.

#14 Moody

  • Members
  • 28 posts

Posted 04 January 2014 - 04:00 PM

View PostCometWolf, on 04 January 2014 - 12:48 PM, said:

Indeed, but if your read the program, you would see that the variables are declared in the main block of the program, same as the functions which used them. Ergo it should have worked.

Well i think the matter lies with the order of declaration - for example:
If you do this:
local function f()
  print(x)
end
f()
x = 6
f()

It will print "5 \n 6"
but if you do

local function f()
  print(x)
end
local x = 5
f()
x = 6
f()

it will print "nil \n nil"

because it compiles the code from top to bottom, it sets all not defined variables environments to global - in this case, there doesnt exist any global x! so it return Nil.
To understand that better: this is the way the compiler works:
local x = 5  -- Declare variable x as local -> create new Environment _ENV
local function f()
  print(x) -- x translates to _ENV["x"], because it was declared physically beforehead
end

vs

local function f()
  print(x) -- "Was it declared beforehand?" -> No -> x translates to _G["x"]
end
local x = 5 -- -- Declare variable x as local -> create new Environment _ENV
(note that there is no actual "current" Environment, it rather accesses the register directly, but for simplicities sake i left it at that)

now you have 2 totally different Environment references:
In the first case, the variable X in the function has the Environment _ENV.
In the second case, the variable X has the global Environment _G

So if you would declare a variable "local chestcokecount" before you declare the function it would be fine. (you can just declare without a value at all)


To give another example:
local function f()
  print(x)
end
local x = 8
_G["x"] = 5
f() --> 5
x = 6
print("Local:"..x)
f() --> 6
returns this:
5
Local:6
5

Edited by Moody, 04 January 2014 - 04:31 PM.


#15 CometWolf

  • Members
  • 1,283 posts

Posted 04 January 2014 - 04:32 PM

You are indeed correct. I've never had the problem before as i always declare the variable prior to using it, so it never crossed my mind. I usually do my wrapping at the start.

#16 Chloe

  • Members
  • 28 posts

Posted 06 January 2014 - 03:46 PM

It doesn't pulse the Gray wire at all now. It should pulse when the chest is lower than 512. I can get it to do anything to go. Something is wrong.

#17 CometWolf

  • Members
  • 1,283 posts

Posted 06 January 2014 - 04:12 PM

Im a derp, i mixed up the subtraction and addition of redstone colors, so it was removing gray when it was supposed to add it.
Also realized i could use rednet cables to test it, so i know it runs now :P
http://pastebin.com/pHm0RRVr

#18 Chloe

  • Members
  • 28 posts

Posted 06 January 2014 - 04:26 PM

well, it pulses now, but only makes a few pieces, then it stops making them. It keeps printing "Got Timer!" on the screen too?! The count on the screen only updates every few seconds - it doesn't show in real time 1 by 1 by 1, etc... not sure. I think it may be the order things are in?

#19 OReezy

  • Members
  • 91 posts

Posted 06 January 2014 - 05:06 PM

What are you using to move items?

#20 CometWolf

  • Members
  • 1,283 posts

Posted 06 January 2014 - 05:19 PM

Like i already said, you have to set the update timer for how often to check the coke. Come on, atleast look through the code, it's literally the first thing in it... I've set it to 5 secs to be on the safe side. as for the got timer thing, that was me doing some testing, forgot to remove it :P

View PostCometWolf, on 03 January 2014 - 10:49 PM, said:

I haven't tested this, but it should do the trick, just configure the check timer however you like.
From what i can tell it stops pulsing if the sensor is in use at the same time the timer is supposed to fire. My guess would be it's a bug with the sensor really. Anyhows, i did a quick work around to restart the timer whenever the sensor is used. And i set the coke update to 2 secs.
http://pastebin.com/pHm0RRVr





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users