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

Help with button clicking program

Started by RobotBubble, 01 November 2017 - 03:54 AM
RobotBubble #1
Posted 01 November 2017 - 04:54 AM
This is a 3 part question, all of which deal with a system I am working on for myself. I'd really appreciate any help or advice you'd be able to give.

1. So I wrote this program as a startup menu where you can click on the option obviously. But it only detects the click when it is in the little box I have written [ ]. Is there something I can do to make it if it is clicked anywhere in the text?

https://pastebin.com/j1tzRi9t

2. So this is a program that works really well for remote control turtles, it just uses basic broadcast rednet and stuff and it's pretty cool (I can link the TurtleInstall if you'd like) but the only problem I am having is it isn't compatible with the step 1 program. When I click on it it I just get an error with a line number that has nothing wrong with it? I'm assuming it is because of the looping system in the first program. I'm not too great at anything past basic computercraft so I haven't made my own APIs or OSs or anything fancy but the program I am working on now is essentially a personal operating system for me to use with a bunch of useful programs.

https://pastebin.com/f39FWPcg

3. Lastly I haven't been able to find this anywhere but how do I load an image that only appears while that specific program is running and then disappears or is only shown temporarily?

Thank you so much for your help I really appreciate it!
Luca_S #2
Posted 02 November 2017 - 02:54 PM
Only going to answer Part one for now:
I'm going to show you how to fix one of your if Statements you should be able to fix the other ones yourself pretty easily.
So this is your current code:
if cx >= x and cx < choice1:len() and cy == y and button == 1 then
The Problem with this is that you are cheking if the clicked X position is between "x" and the lenght of the string. However what you actually want is to check if it's between "x" and "x" + the lenght of the String.
if cx >= x and cx < x+choice1:len() and cy == y and button == 1 then
Also I suggest that you use tables to store your Buttons.
TheOddByte #3
Posted 02 November 2017 - 03:35 PM
1: It works when I try it, can't find anything wrong with it, I have some suggestions though.
Okay, first of all I'd recommend that you use tables to store all your options, something like this for example

local choices = {
	"TurtleMenu", "Chat", "Calculator", "Notes", "Games", "Comp. Info"
	"Options", "Sleep"
}
because it's much easier to manage and looks cleaner, and in your scenario you're using them as buttons, so I'd suggest you declare them as keys in the table and
assign them a function instead so that they do what you want and so that you can easily add new options without having to get the exact coordinates for each entry.

local choices = {
	["TurtleMenu"] = function()
		--# do stuff
	end;
}
then instead of drawing them one by one as you've done you can loop through them and draw them all in one swoop.

--# Set the starting position
local x, y = 1, 4

for key, value in pairs( choices ) do
	 term.setCursorPos( x, y )
	 term.write( "[ ] " .. key )
	 y = y + 2 --# Increase the y coordinate with 2
end
and then you could do the same thing when checking if it has been clicked

local event, button, cx, cy = os.pullEvent()

local x, y = 1, 4
if event == "mouse_click" then
	for key, value in pairs( choices ) do
		if x >= cx and cx < x + #key + 4 and cy == y and button == 1 then
			value() --# Call the function declared in the table, can also be called like this: choices[key]()
			break --# break out of the loop since we've found the entry that has been clicked
		end
		y = y + 2 --# Same thing as before, increase y with 2 so that we get the correct coordinates to check
	end
end

2: What error are you getting?

3: Load an image using paintutils, draw it, sleep, clear the screen and let the other program handle the rest.

--# Clear the screen
term.clear()
term.setCursorPos( 1, 1 )
--# Load the image
local image = paintutils.loadImage( "path here" )
--# Draw the image
paintutils.drawImage( image, x, y )
--# Wait a bit before continuing
sleep( 2 )
--# Clear the screen and run the desired program
term.clear()
term.setCursorPos( 1, 1 )
shell.run( "the program you want to run" )


@Luca_S What you posted is the correct way to check the coordinates, but it still shouldn't be a problem for the OP since he's using the x position 1 for the menu entries, which makes me a bit confused on why he can only click the little box, and I tried it myself without any problems.
Luca_S #4
Posted 02 November 2017 - 06:32 PM
@Luca_S What you posted is the correct way to check the coordinates, but it still shouldn't be a problem for the OP since he's using the x position 1 for the menu entries, which makes me a bit confused on why he can only click the little box, and I tried it myself without any problems.
Yeah just checked also works for me, he should still use x plus the length tho.
RobotBubble #5
Posted 03 November 2017 - 07:46 PM
1.Thank you, my biggest enemy is organization haha I'll definitely use that!

2. Sorry I should have included that but there is no error code anymore it just doesn't work. The remote program doesn't do anything, the way it works is I hit 'w' and it broadcasts "TS Forward" but it doesn't matter what I hit it might as well be a blank screen? I'm not really sure why though.

3. Awesome, thank you!

Again I really appreciate you guys' help!
RobotBubble #6
Posted 03 November 2017 - 09:23 PM
This is the pastebin for the remote I use, I know it works with my usual menu system but after adding the clickable buttons like I said it just doesn't exit the original loop I think is the problem. For example, I wanted to create an option for the use to edit the coding by ust typing in the password "robot". They would click on the Admin Access option and type it in and it would just run "dir" and then nothing, like it would let you type whatever you want as if you had started on a fresh computer.

nonclickable buttons install (original idea): https://pastebin.com/SPaBgXtV
clickable buttons install: https://pastebin.com/3JdVN9Tz
remote: https://pastebin.com/f39FWPcg