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

Attempt to index a Boolean value...?

Started by OmegaVest, 22 February 2013 - 03:33 AM
OmegaVest #1
Posted 22 February 2013 - 04:33 AM
Okay, so I'm trying to make a silly API. I call it block, and it is meant to make a peripheral out of any block, including turning redstone on and off, and throwing an activation event if the block activates without computer intervention.

But! In actually using the thing, anytime I try to wrap a block, it tells me the subject line. Wha? Isn't that the whole point of a boolean value?

Anyway, I've spent an hour on this and cannot figure out my mistake. I'm hoping you guys will lend me some fresh eyes.

The API:
Spoiler



taber = {
   activate = function(bOn) rs.setOutput(this.side, bOn) end
}

function wrap(side)
   if side == "top" or side == "right" or side == "left" or side == "front" or side == "back" or side == "bottom" then
	  bobble = taber
	  bobble.activate = function(bOn) rs.setOutput(side, bOn) return true end
	  print("Block created")
	  coroutine.create(blockEvent(side))
	  return bobble
   else
	  error("not a side")
   end
end


function blockEvent(side)
   local bOn = false
   while true do
	  if rs.getInput(side) and not bOn then
		 os.queueEvent("block_activate", side)
		 bOn = not bOn
	  elseif not rs.getInput(side) and not bOn then
		 bOn = not bOn
	  end
	  sleep(0.05)
   end
end

The Test Program: (I was in the process of testing the eventQueue system)
Spoiler



b = block.wrap("top")

if b then
   print("Got a block")
else
   print("Nothing!")
end

b.activate(true)
sleep(2.0)
b.activate(false)

while b do
   print("Well")
   evt, arg1, arg2, arg3 = os.pullEvent()
   print(evt, ": ", arg1, ", ", arg2, ", ", arg3)
   sleep(0.5)
end

I left in all debug prints, so work them out as you wish. And thank you in advance.



QUICK EDIT: The error show up on line 1 of the test program, but I'm certain it has something to do with the API.
LBPHacker #2
Posted 22 February 2013 - 04:39 AM
Have you ever used coroutines before? Nevermind, I'll help with that later if I still have to. But why is block.wrap a boolean? Let me think…
OmegaVest #3
Posted 22 February 2013 - 04:47 AM
Not really, this is kinda my experiment with them, and I'm betting I've done something wrong.




!
Oh. Hang on.

EDIT: Nope, still no idea. Going to dig through forums again. (Didn't help the first time, but eh.)
LBPHacker #4
Posted 22 February 2013 - 04:53 AM
Very odd… Have you reloaded the API? Was there an older version of the API where .wrap was only a bool?
Lyqyd #5
Posted 22 February 2013 - 04:55 AM
Well, this definitely won't do what you want it to do regardless. You can create coroutines all day, but there's nothing in that code that causes it to run (except you passing the function to coroutine.create incorrectly). I'm not really sure where the attempt to index boolean is coming from. Are you running this on a fresh computer, loading the API after a clean boot and immediately running the program? If not, you may be dirtying your environment, since you seem to have a propensity for not using local.
LBPHacker #6
Posted 22 February 2013 - 04:58 AM
I don't know what's wrong with indexing .wrap, but I know that the first time you successfully call it, it'll make the computer freeze. coroutine.create requires a function for the first parameter, and Lua won't pass .blockEvent but call it, and there's an infinite loop inside .blockEvent. Gimme a sec and I post a code that REALLY DOES something with coroutines :D/>
OmegaVest #7
Posted 22 February 2013 - 05:00 AM
@Lyqyd Ha, yeah I don't like local very much. I'm getting better at it. And I tested it on a fresh computer, but it still gives me that.

And, okay, what line am I missing that is supposed to help with that?




Very odd… Have you reloaded the API? Was there an older version of the API where .wrap was only a bool?

I actually wrote wrap sans coroutine in my head before I ever put it to paper, so to speak.




By the way, this was meant as pre-alpha code. I wasn't sure it would work, since it is kinda outside my comfort zone.
Also, this whole thing came about as a way to end a debate with immibis that has since lapsed into nothing anyway.
LBPHacker #8
Posted 22 February 2013 - 05:04 AM
Oh, wait a minute. I've got used to my own multitask OS and I thought that this would be easy… You'd need to make a separate "thread" (ok, coroutine) for .blockEvent (just like ComputerCraft does for rednet.run), but that's hard if you can't add coroutines to the main parallel.waitForAny's coroutine list in bois.lua…
OmegaVest #9
Posted 22 February 2013 - 05:07 AM
…(just like ComputerCraft does for rednet.run)…


. . . I'm not above stealing code until I can figure it out. That one parenthetical is actually much more useful than the entire coroutine section on the wiki.
LBPHacker #10
Posted 22 February 2013 - 05:11 AM
Let me tell you something. Coroutines are about time-sharing. When you create a coroutine (coroutine.create), you get an identifier (tostring calls it a thread), which you can use to resume (and of course, to start) the coroutine you've previously created. (coroutine.resume(cr)) But when you resume a coroutine, the code where you've called coroutine.resume from won't continue running, but will wait until the resumed coroutine returns or yields. (coroutine.yield()) os.pullEventRaw is a big coroutine.yield, therefore so are os.pullEvent and sleep.
LBPHacker #11
Posted 22 February 2013 - 05:19 AM
That one parenthetical is actually much more useful than the entire coroutine section on the wiki.

BTW :D/> Have you ever seen this bunch of code before?



-- Run the shell
local ok, err = pcall( function()
	parallel.waitForAny(
		function()
			os.run( {}, "rom/programs/shell" )
		end,
		function()
			rednet.run()
		end )
end )

Yep, this is from bios.lua…
OmegaVest #12
Posted 22 February 2013 - 05:24 AM
Hmm. This will take a bit more planning than normal. Just as much research, but I haven't had to write an outline for a program since high school. That was nearly a decade ago. Crap.




EDIT: Alright, this doesn't need a new post, so I'm updating this one. I figured out what went wrong with the program. It wasn't coroutine, but a small mistake elsewhere. (forgot what part, it was late at night when I fixed it).