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

Help with "touchpoint"

Started by Obsidia, 14 August 2015 - 06:41 AM
Obsidia #1
Posted 14 August 2015 - 08:41 AM
Hey, if been trying to play around with Lyqyd's touchpoint API. But i still dont understand it as much as id like to.

There is an example code to turn on redstone with a button..
This one right here:
os.loadAPI("touchpoint")

local t = touchpoint.new("top")

t:add("left", nil, 2, 2, 14, 11, colors.red, colors.lime)
t:add("right", nil, 16, 2, 28, 11, colors.red, colors.lime)

t:draw()

while true do
local event, p1 = t:handleEvents(os.pullEvent())
if event == "button_click" then
t:toggleButton(p1)
rs.setOutput(p1, not rs.getOutput(p1))
end
end

So I gave it a try. Heres mine:


os.loadAPI("touchpoint")
local t = touchpoint.new("top")

t:add("left", nil, 2, 2, 16, 19, colors.red, colors.lime)
t:add("left", nil, 21,14, 37, 19, colors.red, colors.lime)

t:draw()

while true do
local even, left = t:handleEvents(os.pullEvent())
if event == "buttons_click" then
t:toggleButton("left")
rs.setOutput("right", true)
end
end


but now.. as you can already read from the code.. no matter where I press on the screen the left button will be toggled and the redstone Output will bet set to true.

How can I get it to just accept a specific button and deactivate the redstone again with the deactivating of the button?

Oh.. and what exactly does the "nil" space in the add button line does? and what can he replaced with? I saw it could be replaced with any function.. is that the point to let the button do something specific?

Sorry if this sounds like a dumb question but well.. everyone started somewhere :3

Have a nice day:)
Balthamel #2
Posted 14 August 2015 - 09:16 AM
You want the same button to toggle the redstone on and off?
rs.setOutput("right",(not rs.getOutput("right")))
Or is that not what you want?
Edited on 14 August 2015 - 07:23 AM
Obsidia #3
Posted 14 August 2015 - 09:31 AM
You want the same button to toggle the redstone on and off?
rs.setOutput("right",(not rs.getOutput("right")))
Or is that not what you want?

thanks <3
still one question remains… how do I tell the computer which button does what?

for example the top left button switches redstone on the right on and off and the right button switches redstone on the back on and off? can you tell me how to do that? :)/>
thanks so far!
Balthamel #4
Posted 14 August 2015 - 09:39 AM
Okay I have never used touchAPI before. it looks like there are two ways to use this. either use a function call for what you want the button to do.
For example


local function toggleRedstoneOutputRight()
  rs.setOutput("right",(not rs.getOutput("right")))
end
t:add("left", toggleRedstoneOutputRight , 2, 2, 14, 11, colors.red, colors.lime)

That's what the nil is by the way, it's a bit of code the button should run on being pressed.

ALTERNATIVLY

local event, left = t:handleEvents(os.pullEvent())

should be changed to

event, p1, p2, p3, p4, p5 = t:handleEvents(os.pullEvent())

as described in the api. I'm not sure what values would be assigned to p1 through 5 but I assume one of those values can be used to filter which button was pressed.

reading the examples i realise what you were doing.
p1 is a value provided by the event call! You don't replace p1 with left to toggle the left one you check if the variable p1 == left in your if statement!
Edited on 14 August 2015 - 07:48 AM
Obsidia #5
Posted 14 August 2015 - 10:28 AM
Okay I have never used touchAPI before. it looks like there are two ways to use this. either use a function call for what you want the button to do.
For example


local function toggleRedstoneOutputRight()
  rs.setOutput("right",(not rs.getOutput("right")))
end
t:add("left", toggleRedstoneOutputRight , 2, 2, 14, 11, colors.red, colors.lime)

That's what the nil is by the way, it's a bit of code the button should run on being pressed.

ALTERNATIVLY

local event, left = t:handleEvents(os.pullEvent())

should be changed to

event, p1, p2, p3, p4, p5 = t:handleEvents(os.pullEvent())

as described in the api. I'm not sure what values would be assigned to p1 through 5 but I assume one of those values can be used to filter which button was pressed.

reading the examples i realise what you were doing.
p1 is a value provided by the event call! You don't replace p1 with left to toggle the left one you check if the variable p1 == left in your if statement!



Still .. Im stuck.. cant manage to get it to work how I want. I tried a shitload of stuff already in the last 2 hours
and to "p1 is a value provided by the event call! You don't replace p1 with left to toggle the left one you check if the variable p1 == left in your if statement!"
… well. What exactly does this tell me? My english is at a decent level but not good enought to understand code I guess. :I
I tried it with another function for each button.. didnt work out
I tried it with 2 seperate "if ….. then.." after the while true do . didnt work
I tried it with 2 seperate "while true do…" .. didnt work out

Would anyone with some spare time mind giving me an example code with 2 buttons which controll redstone independent from each other?

I guess im still to new to handle touchscreens.. :c
Edited on 14 August 2015 - 08:30 AM
Balthamel #6
Posted 14 August 2015 - 10:43 AM
if event == "button_click" then
if p1 == "left" then

elseif p1 == "right" then

end
end





this line in your first post

local even, left = t:handleEvents(os.pullEvent())

even should be event

left should be p1

in that line of code left is a variable!
this is not what you want!
you want to find out what the string is inside the event button_click
is the string left or right?
so you use
if p1 == "left" then
<do things>


As for free code that does what you ask? this exists in a spoiler on the post you linked to!

if you alter the line in the example script from
t:add("right", nil, 16, 2, 28, 11, colors.red, colors.lime)
and change it to
t:add("back", nil, 16, 2, 28, 11, colors.red, colors.lime)
it should do EXACTLY what you want
Edited on 14 August 2015 - 08:49 AM
Obsidia #7
Posted 14 August 2015 - 11:09 AM
if event == "button_click" then
if p1 == "left" then

elseif p1 == "right" then

end
end





this line in your first post

local even, left = t:handleEvents(os.pullEvent())

even should be event

left should be p1

in that line of code left is a variable!
this is not what you want!
you want to find out what the string is inside the event button_click
is the string left or right?
so you use
if p1 == "left" then
<do things>


As for free code that does what you ask? this exists in a spoiler on the post you linked to!

if you alter the line in the example script from
t:add("right", nil, 16, 2, 28, 11, colors.red, colors.lime)
and change it to
t:add("back", nil, 16, 2, 28, 11, colors.red, colors.lime)
it should do EXACTLY what you want


So in the end it should look like this: pastebin.com/7q2JWAKq ..?
If I use this code and press on a button i get the error: startup:11: index expected, got nil

Sorry if im a though one haha
The language barrier is pretty hard and then there is my lack of code knowleadge..
But thanks so much so far! :)/> I learned pretty much already
Balthamel #8
Posted 14 August 2015 - 11:13 AM
Is the monitor on top of the computer? leaving the comments (the –# stuff) in really helps with debugging. You should write comments on your code, it helps other people understand what's going on.
Balthamel #9
Posted 14 August 2015 - 11:19 AM
You have a full stop . instead of a , after p2 on line 11

Line 14 the button name is now "Links" telling touchpoint to toggle button "left" will crash
Edited on 14 August 2015 - 09:20 AM
valithor #10
Posted 14 August 2015 - 12:26 PM
edit:
To prevent any additional confusion… I started writing this just shortly after Balthamel's first reply, but was required to step away from my computer half way through. So if you are going to read it, read it as if there were no other replies to the topic. Otherwise there might be confusion.

Allow me to correct a little confusion with the touchpoint api.


os.loadAPI("touchpoint")
local t = touchpoint.new("top")

t:add("left", nil, 2, 2, 16, 19, colors.red, colors.lime)
t:add("left", nil, 21,14, 37, 19, colors.red, colors.lime)

t:draw()

while true do
  local event, left = t:handleEvents(os.pullEvent())
  if event == "buttons_click" then
	t:toggleButton("left")
	rs.setOutput("right", true)
  end
end

You the code above is the original code that you posted from your attempt. I will go through pointing out a few things that stand out to me.


t:add("left", nil, 2, 2, 16, 19, colors.red, colors.lime)
t:add("left", nil, 21,14, 37, 19, colors.red, colors.lime)

You define two buttons in 2 very different places, but you give them the same name. The first argument that you pass to the t:add function is the reference you use to access the button at any point, so having two buttons with the same name will for obvious reasons not work (how will it decide which of the two to activate?). My first suggestion would be to change the name of at least one of these two buttons to something different.


local event, left = t:handleEvents(os.pullEvent())

So what is going on here? Anytime a button is pressed the "event" variable is being set to the "button_click" event, and the "left" variable is being set to the name of the button that was pressed (Another reason why having differently named buttons is important).


t:toggleButton("left")

This stands out due to the "" around left. left is a variable, so putting "" around it causes it to treat it as a string rather than a variable.

With the slight confusion cleared up about the usage of the API, I am going to point out if you are going to use the api and its added events, then you will not need to use p1, p2, p3, p4, p5 to catch all of the events. If you are not going to worry about the other events that are thrown, then catching everything really does not matter. So personally I would go with something along the lines of:

local event, button = t:hhandleEvents(os.pullEvent())

edit:

As for telling the computer which button does what is simply a series of if statements checking which button was pressed.


os.loadAPI("touchpoint")
local t = touchpoint.new("top")

--Notice each of them have a different name
t:add("button1", nil, 2, 2, 10, 3, colors.red, colors.lime)
t:add("button2", nil, 2,4, 10, 5, colors.red, colors.lime)
t:add("button3", nil, 2, 6, 10, 7, colors.red, colors.lime)
t:add("button4", nil, 2,8, 10, 9, colors.red, colors.lime)

t:draw()

while true do
  local event, button = t:handleEvents(os.pullEvent()) --# Grabs the next event that is fired, example being mouse_click, or key_press
  if event == "button_click" then --checking to see if the event was a button_click event opposed to a different type of event.
	if button == "button1" then --# If the button pressed is equal to "button1"
	  print("button1 was pressed")
	elseif button == "button2" then --# if the button pressed is equal to "button2"
	  print("button2 was pressed")
	elseif button == "button3" then --# if the button pressed is equal to "button3"
	  print("button3 was pressed")
	elseif button == "button4" then --# if the button pressed is equal to "button4"
	  print("button4 was pressed")
	end
  end
end

Keep in mind I have never used this api, so the example above might have overlapping buttons if i did not interpret the arguments correctly. But the example is a valid way of checking against button labels.
Edited on 14 August 2015 - 11:31 AM
Balthamel #11
Posted 14 August 2015 - 12:56 PM
This stands out due to the "" around left. left is a variable, so putting "" around it causes it to treat it as a string rather than a variable.

The button's name is left, instead of passing the button name from the event paramater he is filtering for the paramater and then toggling the button directly. I don't understand why you think he should change that from a string to a variable with a name left!? you either haven't read through all the posts here since this is his latest code
http://pastebin.com/7q2JWAKq
which works just fine with the two fixes i mentioned above
or you have in which case in what world would you tell someone to change the string to a variable name like left when from reading the OP it clearly looks like he's trying to filter the event call using a filter of left and misunderstanding the entire concept of that line and the related comment in the example
–# p1 will be "left" or "right", since those are the button labels

By having him do a quick fix like that you're not helping him understand anything at all and just asking for him to come back here looking for more help every single time!
Edited on 14 August 2015 - 10:57 AM
valithor #12
Posted 14 August 2015 - 12:58 PM
This stands out due to the "" around left. left is a variable, so putting "" around it causes it to treat it as a string rather than a variable.

The button's name is left, instead of passing the button name from the event paramater he is filtering for the paramater and then toggling the button directly. I don't understand why you think he should change that from a string to a variable with a name left!? you either haven't read through all the posts here since this is his latest code
http://pastebin.com/7q2JWAKq
which works just fine with the two fixes i mentioned above
or you have in which case in what world would you tell someone to change the string to a variable name like left when from reading the OP it clearly looks like he's trying to filter the event call using a filter of left and misunderstanding the entire concept of that line and the related comment in the example
–# p1 will be "left" or "right", since those are the button labels

By having him do a quick fix like that you're not helping him understand anything at all and just asking for him to come back here looking for more help every single time!

The variable left is the one that has the name of the button that was clicked. It is just the smarter way of doing it especially if you want to have more than one button.

It is better for him to have a good understanding of the language and api than a script that works and no understanding.

edit:

Just going to reinforce the point…

The place where he calls that he never checks which button is pressed, so it is toggling that button no matter which one is being pressed. That is why it stood out to me as something that would have a unintended effect.
Edited on 14 August 2015 - 11:00 AM
Balthamel #13
Posted 14 August 2015 - 01:00 PM
the variable NAME left is a STUPID name the variable left holds string left?! You're not helping him understand you're making it more confusing!

It is better for him to have a good understanding of the language and api than a script that works and no understanding I JUST SAID THAT!

By having him do a quick fix like that you're not helping him understand anything at all and just asking for him to come back here looking for more help every single time!

In responce to your edit
Check the latest code! Telling him he got it wrong again after he's already worked out what was wrong in the OP and (I assume from his sudden silence after my last post) now has functioning code is just in the worst style of know it all condescension and self-aggrandizement that is so prevelent on so many code forums.
By assuming that he has not read through his code, realised he is naming different buttons the same name twice, re-written it in the functioning form you can see in the latest code link you are definately displaying arrogance and the kind of belittlement that I know has driven so many people from learning to code.
Edited on 14 August 2015 - 11:11 AM
valithor #14
Posted 14 August 2015 - 01:11 PM
the variable NAME left is a STUPID name the variable left holds string left?! You're not helping him understand you're making it more confusing!

It is better for him to have a good understanding of the language and api than a script that works and no understanding I JUST SAID THAT!

By having him do a quick fix like that you're not helping him understand anything at all and just asking for him to come back here looking for more help every single time!

I will try to walk you through this…

In his code he defines 2 buttons. Both with name "left" (I pointed out naming them both the same thing should be avoided in my first post)

He then checks to see which of the buttons is pressed using:

local even, left = t:handleEvents(os.pullEvent())

At this point seeing as both of the buttons are called "left" the only possible thing that the variable left can be is "left". Despite this being true…

t:toggleButton("left")
(The above code) Would not be right once he corrects his code. So since I know it is easier to understand if you do not change variable names when referencing the persons code you are correcting I left the variable name as "left" and showed him why it was wrong. So no it is not a "quick fix" it is a fix that is correct in every way, but uses a variable name that does not correctly describe what it is representing.

Either way. I in no point corrected his code to a workable state. I simply did what I said I was going to do, "I will go through pointing out a few things that stand out to me." Which is exactly what I did. I pointed out things that it was obvious needed correcting before giving a correct example which I did at the bottom.
Edited on 14 August 2015 - 11:12 AM
Balthamel #15
Posted 14 August 2015 - 01:17 PM
Read what I say!
HE FIXED HIS OWN CODE IT'S RIGHT HERE http://pastebin.com/7q2JWAKq
This code was written before you posted the first time!
In this code he does not have two methods describing the same button!
He has already rectified that mistake!

"I will try to walk you through this…"
The amount of condescension from that one line alone. I can read what you are saying. It is you who do not understand what I am saying. You are repeating back to me the same thing I am saying to you.
And since you didn't bother to read through our conversation you clearly didn't read the part where he says his English is not so good he can fully understand walls of text and decide therefore to throw a wall of text at him.


In his code he defines 2 buttons. Both with name "left" (I pointed out naming them both the same thing should be avoided in my first post)
Yeah I never said anything about you telling him the WRONG advice. I said you provided him with advice that any non condescending person would assume he has already worked out. Burying someone under a wall of you got that wrong and that wrong and that wrong is just offputting. Answer the question and encourage best practice sure but when the problem is fixed and there is a new version of code, that does not contain errors that were in the old version, going back to correct errors that are now irrelevent is juvenile. If the OP hasn't understood what he did wrong there then let him ask again specifically about that issue. Assume he's inteligent enough that he has understood it for now.
Edited on 14 August 2015 - 11:21 AM
valithor #16
Posted 14 August 2015 - 01:17 PM
Check the latest code! Telling him he got it wrong again after he's already worked out what was wrong in the OP and (I assume from his sudden silence after my last post) now has functioning code is just in the worst style of know it all condescension and self-aggrandizement that is so prevelent on so many code forums.
By assuming that he has not read through his code, realised he is naming different buttons the same name twice, re-written it in the functioning form you can see in the latest code link you are definately displaying arrogance and the kind of belittlement that I know has driven so many people from learning to code.

I actually started writing all of that post shortly after you wrote your first response, however, I was required to step away from my computer before being able to finish. So no I admit I did not see any post after your first.

edit:

"all of that post" meant all of the first post.

Just making sure there is no confusion.

edit2:

Either way if you wish to continue this either do so on my skype: valithor3
or in a private message.

I am only responding back on this topic if the op requests more help.
Edited on 14 August 2015 - 11:20 AM
Balthamel #17
Posted 14 August 2015 - 01:34 PM
I actually started writing all of that post shortly after you wrote your first response, however, I was required to step away from my computer before being able to finish. So no I admit I did not see any post after your first.
\
Context is so important for meaning, *laughs*, that really removes most of the stuff I said about you. Sorry about that. Didn't you get the alert popdown that says there have been replies since you started editing? Anyway, as a responce to the first two posts, yeah that was a decent help post. I had assumed you were responding to the entire thread due to your mention of p1 through 5.
Lyqyd #18
Posted 14 August 2015 - 05:42 PM
You need to calm down, or you may find that your responses start disappearing. Please use a civil tone, even when you are disagreeing with someone. Anger and ALL CAPS does not make for a useful post, and useless posts have no place here.
Obsidia #19
Posted 15 August 2015 - 02:22 AM
Thanks to @Balthamel and @valithor!
You both helped me out alot! I managed to get it my self thanks to you 2 :)/>
The only thing I might have to take a look more closely is the Redstone API for the bundledcable combining and stuff like that! :)/>
you 2 were a great help. Thanks!