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

Command block peripheral & iterator for loop

Started by random_lulz, 21 April 2013 - 04:09 PM
random_lulz #1
Posted 21 April 2013 - 06:09 PM
Hi, I'm trying to use a command block as a peripheral in my code. I used functions from http://computercraft...and_Block_(API). Doesn't work, so I tried writing a simple program to test.


cmd = peripheral.wrap("right")
cmd.setCommand("time set 0")
cmd.runCommand()

And it returns

test:2: attempt to call nil

I've made sure that

	# Enable Command Block support
	B:enableCommandBlock=true

Not sure if I did something wrong, the wiki is wrong, or is this a bug?

And as for my second problem, I tried using an iterator for loop to get rednet messages on computer 0 as according to http://computercraft...ki/Os.pullEvent but it failed.
This is my test program

local event, param1, param2, param3
for event, param1, param2, param3 in os.pullEvent("rednet_message") do
   param1 = tostring(param1)
   param3 = tostring(param3)
   print(event.." "..param1.." "..param2.." "..param3)
end
Okay, it suceeds here.

Next I open up Lua on a wireless turtle

lua>rednet.open("left")
lua>rednet.send(0, "hi")
true

And computer 0 returns

test2:2: attempt to call string

Also unsure if bug, wiki mistake or my mistake.

EDIT: ComputerCraft 1.52, Forge 660
OmegaVest #2
Posted 21 April 2013 - 06:25 PM
Hmm. Well, for the first one, I have no idea. Unless you are playing on a server which does not have the option enabled.
The second one, though, is the use of tostring.
This would be slightly better:

   print(event, " " , param1, " " , param2, " ", param3)

Or just put it into writes.
random_lulz #3
Posted 21 April 2013 - 07:53 PM
Hmm. Well, for the first one, I have no idea. Unless you are playing on a server which does not have the option enabled.
I am playing on SSP.

The second one, though, is the use of tostring.
This would be slightly better:

   print(event, " " , param1, " " , param2, " ", param3)

Or just put it into writes.
My original version did not include tostring, and it still did not work. I will try the comma usage later. Oh and I forgot about term.write. Just one quick question on term.write, do I add /n or \n to get a new line.
And anyways why would the problem be on line 2 even if I did not use tostring?
Zoinky #4
Posted 21 April 2013 - 09:42 PM
For the first one, you need to set enableCommandBlock to true in the config.
No idea. Double check? You might not've saved? Lol, I need to read through more thoroughly :S
theoriginalbit #5
Posted 21 April 2013 - 09:51 PM
Ok as for your first problem try this and tell me what happens

for k,v in pairs(peripheral.getMethods('right')) do
  print(tostring(k)..':'..tostring(v))
end

As for the second problem, you cannot specify the event type in the loop, you must do a manual check for it

for event, p1, p2 in os.pullEvent do
  if event == 'rednet_message' then
	param1 = tostring(param1)
	param3 = tostring(param3)
	print(event.." "..param1.." "..param2.." "..param3)
  end
end
its to do with that the iterators must be a function pointer, which os.pullEvent on its own is, but the minute you specify a filter it gets the first return value (the event, which is a string) and attempts to call the string, assuming it is a function, hence the error.
Edited on 21 April 2013 - 07:53 PM
random_lulz #6
Posted 21 April 2013 - 11:50 PM
I have discovered the problem for the first. Apparently there is something called lateral inversion which I did not notice.
I always edit the computer from the back of the computer and from my view the command block was on the right, so I used right and that's why it does not work.
I.e.
– f –
X Y Z
– m –
X = wireless modem
Y = advanced computer
Z = command block
m = me
f = where the screen of the computer is

So when I tried theoriginalbit's method I got the methods available for the modem not the command block. When I reversed it to left, it gave me the command block methods.
So thanks!

As for the second: thanks for the help!
theoriginalbit #7
Posted 21 April 2013 - 11:55 PM
I had a theory it was something like that :)/>

glad I could help :)/>
oeed #8
Posted 22 April 2013 - 12:09 AM
Woah! Since when could you do this?
theoriginalbit #9
Posted 22 April 2013 - 12:17 AM
Woah! Since when could you do this?
1.3 or 1.4… I think the former… *checks the wiki*

EDIT: Ok so it was in 1.48