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

add Pages & Next/prev. Page button or scrolling up/down button to my enhanced Portal 3 mod dialing program?

Started by mariodk, 02 May 2015 - 06:28 PM
mariodk #1
Posted 02 May 2015 - 08:28 PM
hey i have made this program:
http://pastebin.com/kLX6bVYJ

used this for help with coding: https://github.com/e...i/ComputerCraft
to use for this:
http://i.imgur.com/aRzMLDH.jpg
http://i.imgur.com/4GdcFEL.jpg (each listed item = a dial in the dialing device shown on the touch screen)

but the screen is too small and dont want to make it bigger for place for more dials/buttons on the screen
so anyway to add more pages so it shows more dials there or scroll up/down button so it can show all the dials if touch a scroll down/up button on the screen?

i have tried to use this API:
http://www.computercraft.info/forums2/index.php?/topic/17857-auto-button-api/
but i really dont know how to get it to work with my program :(/>
Edited on 02 May 2015 - 06:41 PM
KingofGamesYami #2
Posted 03 May 2015 - 02:13 AM
If you don't mind using others' apis, go ahead and use mine.


os.loadAPI( "simpleButton" )
local t = simpleButton.makeButtonPages( colors.black, colors.red, colors.white, "Hello", "World" )
t:render()
while true do
  local event = {t:handleEvents( os.pullEvent() )}
  if event[1] == "button_click" then
    --#a button was clicked, either "Hello", or "World"
  end
end

You may add as many buttons as you like, simpleButton.makeButtonPages will handle all of them and automatically generate new pages also.

It should work on monitors, as long as the term is redirected, though I have not tested on a monitor.

Edit: I forgot to index my table. Silly me…
Edited on 03 May 2015 - 12:45 PM
mariodk #3
Posted 03 May 2015 - 07:39 AM
If you don't mind using others' apis, go ahead and use mine.


os.loadAPI( "simpleButton" )
local t = simpleButton.makeButtonPages( colors.black, colors.red, colors.white, "Hello", "World" )
t:render()
while true do
  local event = {t:handleEvents( os.pullEvent() )}
  if event == "button_click" then
	--#a button was clicked, either "Hello", or "World"
  end
end

You may add as many buttons as you like, simpleButton.makeButtonPages will handle all of them and automatically generate new pages also.

It should work on monitors, as long as the term is redirected, though I have not tested on a monitor.
cant seems to get that to work too :(/>
LBPHacker #4
Posted 03 May 2015 - 08:38 AM
It just ignores your clicks, doesn't it?
Try thisI'm pretty sure Yami's code is missing an index into the event table:
os.loadAPI( "simpleButton" )
local t = simpleButton.makeButtonPages( colors.black, colors.red, colors.white, "Hello", "World" )
t:render()
while true do
  local event = {t:handleEvents( os.pullEvent() )}
  -- if event == "button_click" then -- hmm
  if event[1] == "button_click" then
    --#a button was clicked, either "Hello", or "World"
  end
end

If that doesn't help, show us how you tried to integrate Yami's API.
Edited on 03 May 2015 - 06:45 AM
mariodk #5
Posted 03 May 2015 - 09:00 AM
http://pastebin.com/T71HHtUK the new code edited:

and
screen shot what error i getting:

http://i.imgur.com/75TENLm.png

think i have to rewhite the whole enhanced portal 3 program to work with any button APIs but not sure how or what guides can help me(not many enhanced portal 3 mod programs i can look on for help and that is kinda weird since that mod is on almost every modpacks ever)
Edited on 03 May 2015 - 07:02 AM
flaghacker #6
Posted 03 May 2015 - 09:26 AM
http://pastebin.com/T71HHtUK the new code edited:

and
screen shot what error i getting:

http://i.imgur.com/75TENLm.png

think i have to rewhite the whole enhanced portal 3 program to work with any button APIs but not sure how or what guides can help me(not many enhanced portal 3 mod programs i can look on for help and that is kinda weird since that mod is on almost every modpacks ever)

So your program crashes instantly on the first line? I don't see any "still alive"s in the screenshot?
LBPHacker #7
Posted 03 May 2015 - 09:29 AM
-snip-
OP might not have updated the screenshot yet. I just told them to insert "stillalives" in the code and see how far it gets.
mariodk #8
Posted 03 May 2015 - 09:43 AM
http://pastebin.com/T71HHtUK the new code edited:

and
screen shot what error i getting:

http://i.imgur.com/75TENLm.png

think i have to rewhite the whole enhanced portal 3 program to work with any button APIs but not sure how or what guides can help me(not many enhanced portal 3 mod programs i can look on for help and that is kinda weird since that mod is on almost every modpacks ever)

So your program crashes instantly on the first line? I don't see any "still alive"s in the screenshot?
http://i.imgur.com/fmapsty.png i have done it and i think it is this code here:
local t = simpleButton.makeButtonPages( colors.black, colors.red, colors.white, " " .. portal.getStoredName(i) .. " " )


it might be portal.getStoredName(i) but needs that for get the names from the dialing device
like as seen here: https://github.com/e...i/ComputerCraft
flaghacker #9
Posted 03 May 2015 - 10:56 AM
You're calling getStoredName with parameter "i", and there's nothing in you program setting it to something. You have to pass a number, currently you're passing nil. If you expect "i" to be set to a number by another program, try printing it just before you use it.
mariodk #10
Posted 03 May 2015 - 11:06 AM
You're calling getStoredName with parameter "i", and there's nothing in you program setting it to something. You have to pass a number, currently you're passing nil. If you expect "i" to be set to a number by another program, try printing it just before you use it.
it worked with this program here:
http://pastebin.com/kLX6bVYJ this is where i got the names & how many dials there was on the screen:
http://i.imgur.com/4GdcFEL.jpg (each listed item = a dial named after a item in the dialing device shown on the touch screen)
flaghacker #11
Posted 03 May 2015 - 01:53 PM
You're calling getStoredName with parameter "i", and there's nothing in you program setting it to something. You have to pass a number, currently you're passing nil. If you expect "i" to be set to a number by another program, try printing it just before you use it.
it worked with this program here:
http://pastebin.com/kLX6bVYJ this is where i got the names & how many dials there was on the screen:
http://i.imgur.com/4GdcFEL.jpg (each listed item = a dial named after a item in the dialing device shown on the touch screen)

I'll assume you're talking about this piece of code:

for i=0, portal.getStoredCount()-1 do
  monitor.setBackgroundColor(color)
  color = colors.blue
  monitor.write(" " .. portal.getStoredName(i) .. " ")
  newLine()
end
The "i" is set by the for loop here, so you'll want to use a for loop youself. Something along these lines:

buttonNames = {};

//read button names
for i=0, portal.getStoredCount()-1 do
  table.insert(buttonNames, portal.getStoredName(i))
end

//create gui
local t = simpleButton.makeButtonPages( colors.black, colors.red, colors.white, unpack(buttonNames))

//continue on
t:render()
...
You might want to read up about for loops and tables if you're not yet familiar with them. And "unpack" basically splits a table into different arguments (simply explained)
Edited on 03 May 2015 - 11:59 AM
mariodk #12
Posted 03 May 2015 - 03:06 PM
i got it to work now atleast showing the buttons on the computer (not the touch screen) & got the buttons to have same names & same dials as on the dialing device

but when i click on the buttons the programs stops

and i needing to get the buttons from the terminal to the big touch screen instead of inside the computer block

remember im a newbie to computercraft/coding: first ever program i made and so half of my coding is just lucky i got to work i guess

here is the code so far:
http://pastebin.com/T71HHtUK

and i think the CC program stops after this:
  • if event[1] == "button_click" then
Edited on 03 May 2015 - 01:08 PM
flaghacker #13
Posted 03 May 2015 - 03:22 PM
i got it to work now atleast showing the buttons on the computer (not the touch screen) & got the buttons to have same names & same dials as on the dialing device

but when i click on the buttons the programs stops

and i needing to get the buttons from the terminal to the big touch screen instead of inside the computer block

remember im a newbie to computercraft/coding: first ever program i made and so half of my coding is just lucky i got to work i guess

here is the code so far:
http://pastebin.com/T71HHtUK

and i think the CC program stops after this:
  • if event[1] == "button_click" then

Start by removing line 21 and changing line 23 to

local event = {t:handleEvents(os.pullEvent())}
Then you can check the event name in event[1] and the button name in event[2].

What do you mean with "stops? Are there any errors? Does the program "stop" at the if statement or at the portal.terminate?
Edited on 03 May 2015 - 01:23 PM
mariodk #14
Posted 03 May 2015 - 03:33 PM
i got it to work now atleast showing the buttons on the computer (not the touch screen) & got the buttons to have same names & same dials as on the dialing device

but when i click on the buttons the programs stops

and i needing to get the buttons from the terminal to the big touch screen instead of inside the computer block

remember im a newbie to computercraft/coding: first ever program i made and so half of my coding is just lucky i got to work i guess

here is the code so far:
http://pastebin.com/T71HHtUK

and i think the CC program stops after this:
  • if event[1] == "button_click" then

Start by removing line 21 and changing line 23 to

local event = {t:handleEvents( os.pullEvent())}

What do you mean with "stops? Are there any errors? Does the program "stop" at the if statement or at the portal.terminate?

i got it to work now atleast showing the buttons on the computer (not the touch screen) & got the buttons to have same names & same dials as on the dialing device

but when i click on the buttons the programs stops

and i needing to get the buttons from the terminal to the big touch screen instead of inside the computer block

remember im a newbie to computercraft/coding: first ever program i made and so half of my coding is just lucky i got to work i guess

here is the code so far:
http://pastebin.com/T71HHtUK

and i think the CC program stops after this:
  • if event[1] == "button_click" then

Start by removing line 21 and changing line 23 to

local event = {t:handleEvents( os.pullEvent())}
Then you can check the event name in event[1] and the button name in event[2].

What do you mean with "stops? Are there any errors? Does the program "stop" at the if statement or at the portal.terminate?
after clicked on of the buttons on the computercraft terminal (wish it was touch screen but for now let me get it to work on the terminal)
the program: http://pastebin.com/T71HHtUK stops at this:
  • if(yPos <= address_count) then
here is a screenshot: http://i.imgur.com/n0CGOHR.png after i clicked one of the buttons
trying to make so onces you click the button the program will use that same dial where the button got its name from
and after done this you can use the program again for next time you needing to dial a other place to

also want this button api to not use the ternimal but the touch screen instead
Edited on 03 May 2015 - 01:35 PM
KingofGamesYami #15
Posted 03 May 2015 - 03:58 PM
The problem is simple: yPos is not defined anywhere!

event[ 2 ] will be the text of the button, FYI.

If you want to use it on a monitor, simply term.redirect to that monitor before creating your button pages.
mariodk #16
Posted 03 May 2015 - 04:23 PM
The problem is simple: yPos is not defined anywhere!

event[ 2 ] will be the text of the button, FYI.

If you want to use it on a monitor, simply term.redirect to that monitor before creating your button pages.
i tried term.redirect but then clicking on the screen nothing happens

also edited the code:
http://pastebin.com/T71HHtUK but still not seems to work
Edited on 03 May 2015 - 03:42 PM
KingofGamesYami #17
Posted 03 May 2015 - 04:30 PM
Ah, sorry I forgot to update the paste :P/>. It should work with monitors now. (Please tell me if it doesn't, I'll try and fix it)
mariodk #18
Posted 03 May 2015 - 04:45 PM
only thing left is to do to get the button of the dial name to use the same dial name on the dial device and active the portal

im sure this is wrong but dialLocation(event[1]) sure does not work and maybe add 1-2 more buttons per pages (if possible) (remember newbie to CC coding/coding at all)
Edited on 03 May 2015 - 02:48 PM
KingofGamesYami #19
Posted 03 May 2015 - 05:05 PM
See, I don't know how dialLocation works, or what it expects, or what you are giving my API. If you want to use the text of the button that was clicked, use event[ 2 ].
mariodk #20
Posted 03 May 2015 - 05:58 PM
See, I don't know how dialLocation works, or what it expects, or what you are giving my API. If you want to use the text of the button that was clicked, use event[ 2 ].
ok
but i alteast come really far

only needing to add 1,2 or 3 more buttons on the touch screen since only 2 per pages as well as maybe make the buttons abit closer to each other
and dont want to make the touch screen bigger so i have to somehow/somewhere edit the API (i have a copy of your updated API in my own pastebin)

and do so it dial&amp;active the portal once clicked on of the buttons and it seems it dont work so well with that yet
it stops at:
dialLocation(event[1])
in this coding from this pastebin http://pastebin.com/T71HHtUK :
  • while true do
  • local event = {t:handleEvents( os.pullEvent() )}
  • if event[1] == "button_click" then
  • portal.terminate()
  • print("still alive")
  • dialLocation(event[1])
  • end
  • end
i am trying to make it dial the same dial from the dialing device as it got its name from this part of coding:
  • buttonNames = {};
  • //read button names for i=0, portal.getStoredCount()-1 do table.insert(buttonNames, portal.getStoredName(i)) end //create gui local t = simpleButton.makeButtonPages( colors.black, colors.red, colors.white, unpack(buttonNames)) //continue on t:render() as Flaghacker showed me
Edited on 03 May 2015 - 04:04 PM
flaghacker #21
Posted 03 May 2015 - 07:41 PM
Currently you passing nothin, aka nil, to the dialLocation function, but in the method itself you're treating is as a number. You'll want to pass the button's number to the dialLocation function. That number can be found in the buttonNames table:


//find value v in table t, return the key k
function find(t, v)
  for k, l in pairs(t) do
	if l == v then
	  return k
	end
  end
end

//new line 30:
dialLocation(find(buttonNames, event[2])

2 more things:
  • You can remove line 26, as that's already done in the dialLocation function
  • What do you expect line 28 to do? Something isn't right there…
Edited on 03 May 2015 - 05:42 PM
mariodk #22
Posted 03 May 2015 - 07:56 PM
Thank you all for all the help got it to work just as i wanted

now just to edit the button api abit so there is place for more buttons on each pages on the touch screen &amp; the buttons is abit closer to each other
&amp; maybe when pressed the button it will be green for abit so you can see its pressed
Edited on 03 May 2015 - 05:57 PM
flaghacker #23
Posted 03 May 2015 - 08:03 PM
Thank you all for all the help got it to work just as i wanted

now just to edit the button api abit so there is place for more buttons on each pages on the touch screen &amp; the buttons is abit closer to each other
&amp; maybe when pressed the button it will be green for abit so you can see its pressed

I summon KingofGamesYami to improve his APi :P/>!

Edit: no [user][/user] tags :(/>
Edited on 03 May 2015 - 06:03 PM
KingofGamesYami #24
Posted 03 May 2015 - 09:13 PM
I don't think I could add more buttons per page without a serious rewrite of the API, because the only place to put more buttons would be in columns.

The buttons are a single line apart, I don't know how you could get them any closer…

As for changing the color of the buttons, it's fairly easy to add that, simply make this edit:


local function getClicked( tButtons, x, y )
	for _, button in ipairs( tButtons ) do
		if button:isClicked( x, y ) then
			button:render( colors.green, colors.white ) --#this line was added
			sleep( 0.1 ) --#and this line was added
			return button.str
		end
	end
end

Please note that you will have to manually call t:render() in order to reset the color of the button.