Jump to content




Creating a New Computercraft Function.


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

#1 SNWLeader

  • Members
  • 71 posts
  • LocationTampa Bay, Florida

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?

#2 TheOddByte

    Lazy Coder

  • Members
  • 1,607 posts
  • LocationSweden

Posted 26 January 2013 - 09:33 AM

Nevermind..
Got an error..
Please Delete this!

#3 TheOddByte

    Lazy Coder

  • Members
  • 1,607 posts
  • LocationSweden

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)


#4 ikke009

  • Members
  • 224 posts
  • LocationSliding between sunbeams

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

#5 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

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

#6 ikke009

  • Members
  • 224 posts
  • LocationSliding between sunbeams

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

#7 Eric

  • Members
  • 92 posts
  • LocationUK

Posted 27 January 2013 - 06:27 AM

View PostremiX, on 26 January 2013 - 10:52 AM, said:

    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.

#8 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 27 January 2013 - 07:16 AM

View PostEric, on 27 January 2013 - 06:27 AM, said:

View PostremiX, on 26 January 2013 - 10:52 AM, said:

	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...

#9 dan14941

  • Members
  • 45 posts

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 Posted Image please





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users