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

Creating a New Computercraft Function.

Started by SNWLeader, 26 January 2013 - 08:28 AM
SNWLeader #1
Posted 26 January 2013 - 09:28 AM
I wanted to make make a function whenever I typed in a specific command. Like for say I wanted to do this without typing this in all over again.


rs.setBundledOutput("back", 1)
sleep(1)
rs.setBundledOutput("back", 0)
sleep(1)

I wanted to label it pulse or something of that sort how would I do that?
TheOddByte #2
Posted 26 January 2013 - 09:33 AM
Nevermind..
Got an error..
Please Delete this!
TheOddByte #3
Posted 26 January 2013 - 09:37 AM
I meant to write this:

function rs(side,t1,t2)

rs.setBundledOutput(side, t1)
sleep(1)
rs.setBundledOutput(side, t2)
sleep(1)
end

--Then Call it Like This Or Something
rs("back",1,0)
ikke009 #4
Posted 26 January 2013 - 10:41 AM
in your case this would be sufficient though..

function pulse()
  rs.setBundledOutput("back",1)
  sleep(1)
  rs.setBundledOutput("back",0)
  sleep(1)
end


--run it by simply putting pulse() in the code

click here for more info about functions
remiX #5
Posted 26 January 2013 - 10:52 AM

function pulseCol(col, side, lengthOn, lengthOff, nTimes)
	if type(col) ~= "number" then error("Bad argument #1, number expected - got " .. type(col))
	elseif type(side) ~= "string" then error("Bad argument #2, string expected - got " .. type(side))
	elseif type(lengthOn) ~= "number" then error("Bad argument #3, number expected - got " .. type(lengthOn))
	elseif type(lengthOff) ~= "number" then error("Bad argument #4, number expected - got " .. type(lengthOff))
	elseif type(nTimes) ~= "number" then error("Bad argument #5, number expected - got " .. type(nTimes)) end
	nTimes = nTimes or 1
	lengthOn = lengthOn or 1
	lengthOff = lengthOff or 1
	col = col or colours.white
	for i = 1, #nTimes do
		c = rs.getBundledInput() + col
		rs.setBundledOutput(side, c)
		sleep(lengthOn)
		rs.setBundledOutput(side, c - col)
		sleep(lengthOff)
	end
end

Something like that… not sure if the redstone part is correct though, forgot how to do it as I hardly ever do.. :P/>
ikke009 #6
Posted 27 January 2013 - 05:38 AM
Why do you make it so complicated for someone who doesn't know how a function works xD
Eric #7
Posted 27 January 2013 - 06:27 AM

    elseif type(lengthOn) ~= "string" then error("Bad argument #3, number expected - got " .. type(lengthOn))
	elseif type(lengthOff) ~= "string" then error("Bad argument #4, number expected - got " .. type(lengthOff))
	elseif type(nTimes) ~= "string" then error("Bad argument #5, number expected - got " .. type(nTimes))
NIce… So if you give it a number…

I would say avoid type checking where possible - it violates the principle of duck typing.
remiX #8
Posted 27 January 2013 - 07:16 AM

	elseif type(lengthOn) ~= "string" then error("Bad argument #3, number expected - got " .. type(lengthOn))
	elseif type(lengthOff) ~= "string" then error("Bad argument #4, number expected - got " .. type(lengthOff))
	elseif type(nTimes) ~= "string" then error("Bad argument #5, number expected - got " .. type(nTimes))
NIce… So if you give it a number…

I would say avoid type checking where possible - it violates the principle of duck typing.

Super fail… I copied and pasted and forgot to change "string" to "number" xD

Will fix now…
dan14941 #9
Posted 27 January 2013 - 01:40 PM
Easy its just:
function Pulse()
    rs.setBundledOutput("back", 1)
    sleep(1)
    rs.setBundledOutput("back", 0)
    sleep(1)
end
and when you want the program you put this in just type Pulse() on a new line.

P.S. Put the function at the top of your code and if this help click my please