Jump to content




Command block peripheral & iterator for loop


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

#1 random_lulz

  • Members
  • 6 posts

Posted 21 April 2013 - 05: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

#2 OmegaVest

  • Members
  • 436 posts

Posted 21 April 2013 - 05: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.

#3 random_lulz

  • Members
  • 6 posts

Posted 21 April 2013 - 06:53 PM

View PostOmegaVest, on 21 April 2013 - 05:25 PM, said:

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.

Quote

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?

#4 Zoinky

  • Members
  • 144 posts
  • LocationWellington, New Zealand

Posted 21 April 2013 - 08: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

#5 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 21 April 2013 - 08: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 by theoriginalbit, 21 April 2013 - 08:53 PM.


#6 random_lulz

  • Members
  • 6 posts

Posted 21 April 2013 - 10: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!

#7 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 21 April 2013 - 10:55 PM

I had a theory it was something like that :)

glad I could help :)

#8 oeed

    Oversimplifier

  • Members
  • 2,095 posts
  • LocationAuckland, New Zealand

Posted 21 April 2013 - 11:09 PM

Woah! Since when could you do this?

#9 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 21 April 2013 - 11:17 PM

View Postoeed, on 21 April 2013 - 11:09 PM, said:

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





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users