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

Help with my code please?

Started by MoonPieh, 15 August 2014 - 10:56 PM
MoonPieh #1
Posted 16 August 2014 - 12:56 AM
Just recently started the dive into computercraft. I've been messing around with this program for the last few day trying to get it to work. My goal with it is to be able to use redstone control on different parts of a spawner room.

At6nmKpK thats code so far, as of now the buttons are toggleable on the screen; however it doesn't actually control the redstone signal.

Any help will be greatly appreciated :)/>
theoriginalbit #2
Posted 16 August 2014 - 02:07 AM
it looks like you have your solution commented out on line 25. What's the problem you're having? is it an error?
MoonPieh #3
Posted 16 August 2014 - 03:32 AM
It's kind of a frankenstein piece of code. I took pieces from the touchpoint code sample and one of direwolf's codes. I'm not entirely sure why I thought I should comment it out.

Currently the problem is that when I press the on screen buttons, it doesn't actually toggle the redstone signal.

I tried uncommenting line 25, but then I get "test1:25: invalid side".

I have this currently set up in a test world. Would a picture of the build help?
Bomb Bloke #4
Posted 16 August 2014 - 03:57 AM
To start off, it's worth bearing in mind that under ComputerCraft 1.6, bundled cables are effectively unusable at present. You're not using the redstone bundled cable functions, but you are referring to colours later in the script, so I'm assuming that's your intention?

Your button names are not valid output sides. Those consist of directions relative to your computer's facing - "back", "forward", "left", etc.

I'm pretty sure you're going to have to define your grinder/spawner/etc functions before you try to build buttons out of them.
MoonPieh #5
Posted 16 August 2014 - 04:18 AM
To start off, it's worth bearing in mind that under ComputerCraft 1.6, bundled cables are effectively unusable at present. You're not using the redstone bundled cable functions, but you are referring to colours later in the script, so I'm assuming that's your intention?

Actually, I'm using minefactory reloaded rednet cables, not computercraft bundled cables.

Your button names are not valid output sides. Those consist of directions relative to your computer's facing - "back", "forward", "left", etc.

I think that's why I might have line 25 commented out, I wasn't sure how to fix that.


I'm pretty sure you're going to have to define your grinder/spawner/etc functions before you try to build buttons out of them.

Do you mean just move the functions up above before "making" the buttons like this? I'm still VERY new to this, so my understanding of it is very limited.

Spoiler

--# load the touchpoint API
os.loadAPI("touchpoint")

--# intialize a new button set on the top monitor
local t = touchpoint.new("top")
function grinder()
  rsOut[colors.white] = not rsOut[colors.white]
end

function spawner()
  rsOut[colors.lime] = not rsOut[colors.lime]
end

function melter()
  rsOut[colors.red] = not rsOut[colors.red]
end

function lights()
  rsOut[colors.cyan] = not rsOut[colors.cyan]
end

function doors()
  rsOut[colors.purple] = not rsOut[colors.purple]
end

--# add two buttons
t:add("Grinder", grinder, 2, 2, 9, 4, colors.red, colors.cyan)
t:add("Spawner", spawner,11 ,2,19, 4, colors.red, colors.cyan)
t:add("Melter", melter,21, 2,29, 4, colors.red, colors.cyan)
t:add("Lights", lights,31, 2,39, 4, colors.red, colors.cyan)
t:add("Doors", doors,41, 2,49, 4, colors.red, colors.cyan)

--# draw the buttons
t:draw()

while true do
		--# handleEvents will convert monitor_touch events to button_click if it was on a button
		local event, p1 = t:handleEvents(os.pullEvent())
		if event == "button_click" then
				--# p1 will be "left" or "right", since those are the button labels
				--# toggle the button that was clicked.
				t:toggleButton(p1)
				--# and toggle the redstone output on that side.
				--# rs.setOutput(p1, not rs.getOutput(p1))
		end
end
Edited on 16 August 2014 - 04:42 AM
Lyqyd #6
Posted 16 August 2014 - 06:35 AM
After the t:togglebutton, throw in a:


t.buttonList[p1].func()

That will actually call the function you've assigned to each button.

Of course, you still need to correct those functions to actually do what you want, but that will at least get the functions being called.
MoonPieh #7
Posted 16 August 2014 - 06:59 AM
After the t:togglebutton, throw in a:


t.buttonList[p1].func()

That will actually call the function you've assigned to each button.

Of course, you still need to correct those functions to actually do what you want, but that will at least get the functions being called.

After putting that in, it says <name> expected. What does that mean?

Also, could you give an example of a correct function? I copied that part of the code from a piece of Direwolfs code.

Is rsOut[colors. blank] right? or should I be using rs.setBundledOutput[color.blank]? I am using rednet cables from MFR.

Another less important issue I'm having is that the words on the monitor isn't really centered. Any ideas about that?

Sorry for all the noobish questions, I really do appreciate the help.
Lyqyd #8
Posted 16 August 2014 - 07:03 AM
rsOut[whatever] isn't correct, I've never seen anyone try to use that for output before. You'd need rs.setBundledOutput(color). If you're using CC 1.57 or CC 1.58, the MFR bundled cables should work, but if you're on CC 1.6+, there aren't any compatible bundled cables that I know of.

What line is the <name> expected error on?
MoonPieh #9
Posted 16 August 2014 - 07:54 AM
SpoilerrsOut[whatever] isn't correct, I've never seen anyone try to use that for output before. You'd need rs.setBundledOutput(color). If you're using CC 1.57 or CC 1.58, the MFR bundled cables should work, but if you're on CC 1.6+, there aren't any compatible bundled cables that I know of.

What line is the <name> expected error on?


Well that's definitely part of the problem. I was running CC 1.63. But I want to get this code working so I'll be using an earlier version here on out.

The '<name>' expected error is on line 24, which is the line I just put in.

t.buttonList[p1].func()


Oops! Sorry! I accidently typed function() instead of func. But now after running the program and clicking any button it says attempting to call nil and still has problems on line 24.
Edited on 16 August 2014 - 05:59 AM
Lyqyd #10
Posted 16 August 2014 - 09:53 AM
Hmm. It shouldn't be having problems of that sort. Would you mind posting a copy of the updated code?
MoonPieh #11
Posted 16 August 2014 - 07:43 PM
So far this is the code.

Spoiler

--# load the touchpoint API
os.loadAPI("touchpoint")

--# intialize a new button set on the top monitor
local t = touchpoint.new("top")

--# add two buttons
t:add("Grinder", grinder, 2, 2, 9, 4, colors.red, colors.cyan)
t:add("Spawner", spawner,11 ,2,19, 4, colors.red, colors.cyan)
t:add("Melter", melter,21, 2,29, 4, colors.red, colors.cyan)
t:add("Lights", lights,31, 2,39, 4, colors.red, colors.cyan)
t:add("Doors", doors,41, 2,49, 4, colors.red, colors.cyan)

--# draw the buttons
t:draw()

while true do
		--# handleEvents will convert monitor_touch events to button_click if it was on a button
		local event, p1 = t:handleEvents(os.pullEvent())
		if event == "button_click" then
				--# p1 will be "left" or "right", since those are the button labels
				--# toggle the button that was clicked.
				t:toggleButton(p1)
				t.buttonList[p1].func()
				--# and toggle the redstone output on that side.
				--# rs.setOutput(p1, not rs.getOutput(p1))
		end
end

function grinder()
  rsOut[colors.white] = not rsOut[colors.white]
end

function spawner()
  rsOut[colors.lime] = not rsOut[colors.lime]
end

function melter()
  rsOut[colors.red] = not rsOut[colors.red]
end

function lights()
  rsOut[colors.cyan] = not rsOut[colors.cyan]
end

function doors()
  rsOut[colors.purple] = not rsOut[colors.purple]
end
Lyqyd #12
Posted 16 August 2014 - 08:45 PM
Why did you move the functions back down to the bottom? They need to be above the t:add lines. You'll also want to change the rsOut stuff to this:


rs.setBundledOutput("back", (colors.test(rs.getBundledOutput("back"), colors.black) and colors.subtract(rs.getBundledOutput("back"), colors.black) or colors.combine(rs.getBundledOutput("back"), colors.black)))
MoonPieh #13
Posted 16 August 2014 - 11:21 PM

rs.setBundledOutput("back", (colors.test(rs.getBundledOutput("back"), colors.black) and colors.subtract(rs.getBundledOutput("back"), colors.black) or colors.combine(rs.getBundledOutput("back"), colors.black)))

could you clarify please? are those 3 separate options that I could put or is that whole thing what I should replace it with? Also, could you double check the parenthesis, to my noobish eye it looks a little unbalanced after the "or"
Lyqyd #14
Posted 17 August 2014 - 01:32 AM
You'd either put that in its own function and call it from your five button action functions, or replace the guts of each of the five functions with that line. This would be the first option:


local function toggleBundledColor(side, color)
  rs.setBundledOutput(side, (colors.test(rs.getBundledOutput(side), color) and colors.subtract(rs.getBundledOutput(side), color) or colors.combine(rs.getBundledOutput(side), color)))
end

local function grinder()
  toggleBundledColor("back", colors.white)
end

t:add("Grinder", grinder, 2, 2, 9, 4, colors.red, colors.cyan)

The parentheses are correctly balanced.
MoonPieh #15
Posted 17 August 2014 - 02:46 AM
So this is the current code now

Spoiler

--# load the touchpoint API
os.loadAPI("touchpoint")

--# intialize a new button set on the top monitor
local t = touchpoint.new("top")

local function toggleBundledColor(side, color)
  rs.setBundledOutput(side, (colors.test(rs.getBundledOutput(side), color) and colors.subtract(rs.getBundledOutput(side), color) or colors.combine(rs.getBundledOutput(side), color)))
end

local function grinder()
  toggleBundledColors("back", colors.white)
end

local function spawner()
  toggleBundledColors("back", colors.lime)
end

local function melter()
  toggleBundledColors("back", colors.red)
end

local function lights()
  toggleBundledColors("back", colors.cyan)
end

local function doors()
  toggleBundledColor("back", colors.purple)
end

--# add two buttons
t:add("Grinder", grinder, 2, 2, 9, 4, colors.red, colors.cyan)
t:add("Spawner", spawner,11 ,2,19, 4, colors.red, colors.cyan)
t:add("Melter", melter,21, 2,29, 4, colors.red, colors.cyan)
t:add("Lights", lights,31, 2,39, 4, colors.red, colors.cyan)
t:add("Doors", doors,41, 2,49, 4, colors.red, colors.cyan)

--# draw the buttons
t:draw()

while true do
		--# handleEvents will convert monitor_touch events to button_click if it was on a button
		local event, p1 = t:handleEvents(os.pullEvent())
		if event == "button_click" then
				--# p1 will be "left" or "right", since those are the button labels
				--# toggle the button that was clicked.
				t:toggleButton(p1)
				--# and toggle the redstone output on that side.
				--# rs.setOutput(p1, not rs.getOutput(p1))
		end
end

It runs without giving any errors, but it still wont actually toggle the redstone signal :(/> I'm using CC1.57 and MFR 2.7.9
Lyqyd #16
Posted 17 August 2014 - 03:18 AM
… You managed to remove the t.buttonList[p1].func() line that actually calls those functions.
MoonPieh #17
Posted 17 August 2014 - 03:42 AM
Sorry about that. I put it back in, but when I click a button on the monitor it says "touchpoint:108: attempt to index ? (a nil value).
Lyqyd #18
Posted 17 August 2014 - 06:05 AM
Huh, that's weird. Does it happen on every button?

Did you change anything else besides adding that line? If not, just before the t:toggleButton line, can you put a print(p1) and make sure it prints the label of the button you've clicked?
MoonPieh #19
Posted 17 August 2014 - 06:51 AM
I was messing with it, I must have left something in there. I reverted back to the last code that I posted and put t.buttonList[p1].func() back in. However, when I try to toggle a button with that line in it stops with the error message attempt to call nil pointing to the function related to the button ex. if I press melter button its says line 20: attempt to call nil.

I've tried commenting "t.buttonList[p1].func()" out and putting print(p1) after t:toggleButton and it does print the corresponding button pressed.

Here's the code again, incase I derped something else up that I can't see.
Spoiler

--# load the touchpoint API
os.loadAPI("touchpoint")

--# intialize a new button set on the top monitor
local t = touchpoint.new("top")

local function toggleBundledColor(side, color)
  rs.setBundledOutput(side, (colors.test(rs.getBundledOutput(side), color) and colors.subtract(rs.getBundledOutput(side), color) or colors.combine(rs.getBundledOutput(side), color)))
end

local function grinder()
  toggleBundledColors("back", colors.white)
end

local function spawner()
  toggleBundledColors("back", colors.lime)
end

local function melter()
  toggleBundledColors("back", colors.red)
end

local function lights()
  toggleBundledColors("back", colors.cyan)
end

local function doors()
  toggleBundledColor("back", colors.purple)
end

--# add two buttons
t:add("Grinder", grinder, 2, 2, 9, 4, colors.red, colors.cyan)
t:add("Spawner", spawner,11 ,2,19, 4, colors.red, colors.cyan)
t:add("Melter", melter,21, 2,29, 4, colors.red, colors.cyan)
t:add("Lights", lights,31, 2,39, 4, colors.red, colors.cyan)
t:add("Doors", doors,41, 2,49, 4, colors.red, colors.cyan)

--# draw the buttons
t:draw()

while true do
  --# handleEvents will convert monitor_touch events to button_click if it was on a button
  local event, p1 = t:handleEvents(os.pullEvent())
  if event == "button_click" then
	--# p1 will be "left" or "right", since those are the button labels
	--# toggle the button that was clicked.
	t:toggleButton(p1)
	print(p1)
	t.buttonList[p1].func()
	--# and toggle the redstone output on that side.
	--# rs.setOutput(p1, not rs.getOutput(p1))
  end
end

Thank you for your patience, as I take 2 steps back with every step forward :I
Edited on 17 August 2014 - 04:57 AM
Dragon53535 #20
Posted 17 August 2014 - 07:46 AM

local function grinder()
  toggleBundledColors("back", colors.white)
end
should be

local function grinder()
  toggleBundledColor("back", colors.white)
end
for spawner(), melter(), and lights() as well. That's what got you nil as toggleBundledColors() is not a defined function, but toggleBundledColor() is.
Edited on 17 August 2014 - 05:48 AM
MoonPieh #21
Posted 17 August 2014 - 08:29 AM
OMG! yay! its working!! Thanks a million, you guys are great!
MoonPieh #22
Posted 17 August 2014 - 08:55 AM
Is there anyway I could maybe center the text in the buttons? Its not a huge deal, but its kind of annoying as is.
Dragon53535 #23
Posted 18 August 2014 - 04:50 AM
That would probably be something in your button API, can you send a link to it?
MoonPieh #24
Posted 18 August 2014 - 06:26 AM
Actually it's the touchpoint api by lyqyd. Here's the pastebin for it anyways pFHeia96. I didn't change anything on that.
Lyqyd #25
Posted 18 August 2014 - 07:01 AM
I just updated the code on the pastebin. Try downloading a fresh copy of it and see if it fixes the vertical centering.
MoonPieh #26
Posted 19 August 2014 - 05:18 AM
Vertical centering is perfect now. Horizontal is a little iffy. Works on some and left aligns others its looks like.
Lyqyd #27
Posted 19 August 2014 - 05:42 PM
The horizontal centering looks fine. Notice that the six-letter words have one column to their left and two to the right. They can't get any more centered. The five-letter label is centered. The horizontal centering leans a column to the left when it can't perfectly align them.