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

Simple Page System, how to do it?

Started by se7en, 04 July 2014 - 11:06 AM
se7en #1
Posted 04 July 2014 - 01:06 PM
Hi
I've got a simple idea of how to make Informatic Screens on my server (instead of signs/commands)

But unfortunately i have no idea how to do it ;(
Got just simple lua skill, so any examples much appreciated.

What i want to do:

Page displayin on Adv.Monitor with for example

Welcome on our server!
Just press category u want to know more about

[COMMANDS]                  [RULES]               [VIP]

[CUSTOMCRAFT]             [WORLDS]           [ETC]

and when someone click on any of these buttons, monitor will show new screen with infos about that and at the end something like

PAGE 1/5   [>>>]             [BACK TO MAIN]

Please guys i need ur help since i have no idea how to do it ;]

Thanks
Obnoxious_Ninja #2
Posted 04 July 2014 - 05:34 PM
Basic idea of what you want. You'll have to set the "x" and "y" boundaries, since I don't know what you're working with.



local monitor = peripheral.wrap("right") --Change to whatever side the monitor is on

monitor.clear()
monitor.setCursorPos(1, 1)
monitor.write("[Commands]  [Rules]")

while true do
  local event, side, x, y = os.pullEvent("monitor_touch")

  if x >= 1 and x <= 10 and y == 1 then --Tests if the click was between the two x coords, and on the horizontal line y = 1
	shell.run("commands") --Runs the file you wish to have
  elseif x >= 13 and x <= 20 and y == 1 then
	shell.run("rules")
  end
end

[Edit]: This was on a 3x3 monitor screen
Edited on 04 July 2014 - 03:37 PM
se7en #3
Posted 04 July 2014 - 08:19 PM
WOW MATE it really works sweet, didnt even know that it can be done so easily :D/>

Btw. is "shell.run" command "okay"? i mean is it not too heavy for server? Wouldnt it be easier as 1 program ?
KingofGamesYami #4
Posted 04 July 2014 - 08:54 PM
It would be preferable to store the stuff as functions, most likely.


local function commands() --#create function commands
 --#stuff to do
end

local function rules() --#create function rules
 --#stuff to do
end

while true do
  local event, side, x, y = os.pullEvent("monitor_touch")

  if x >= 1 and x <= 10 and y == 1 then --Tests if the click was between the two x coords, and on the horizontal line y = 1
        commands() --#calls the commands function
  elseif x >= 13 and x <= 20 and y == 1 then
        rules() --#calls the rules function
  end
end
Obnoxious_Ninja #5
Posted 04 July 2014 - 09:21 PM
WOW MATE it really works sweet, didnt even know that it can be done so easily :D/>/>

Btw. is "shell.run" command "okay"? i mean is it not too heavy for server? Wouldnt it be easier as 1 program ?
It shouldn't be terribly noticeable, but as Yami said, it might be better to have it as functions. My background in OOP languages makes me want to do things in an Object Oriented fashion, hence the shell.run.
Lyqyd #6
Posted 04 July 2014 - 09:45 PM
That doesn't make it any more object oriented.
se7en #7
Posted 04 July 2014 - 10:28 PM
I need to notice one thing about shell.run.
I already made sort of that page system, but when i get to page 15 via shell.run("page15"), and i want to terminate, i need to Terminate 15 programs, which is i think bad.

I will try it now with functions, since it seems to be better idea ;]

It would be preferable to store the stuff as functions, most likely.


local function commands() --#create function commands
--#stuff to do
end

local function rules() --#create function rules
--#stuff to do
end

while true do
  local event, side, x, y = os.pullEvent("monitor_touch")

  if x >= 1 and x <= 10 and y == 1 then --Tests if the click was between the two x coords, and on the horizontal line y = 1
		commands() --#calls the commands function
  elseif x >= 13 and x <= 20 and y == 1 then
		rules() --#calls the rules function
  end
end

About that, i sort of didnt understand where i put my "main" page…
i made functions like page2(), page3() etc, but where i should put page1(main) ?

Oh one more thing.
When im in page2() function and i want to press "Next Page" which is goin to page3(), it gives me an error (propably because there is no function page3() IN function page2()…

In other words, how to call function while inside of another funcion ? Sort of main.page1() ?
Edited on 04 July 2014 - 11:24 PM
Bomb Bloke #8
Posted 05 July 2014 - 02:44 AM
It's best not to, at least, not in that manner. Have each page function end instead, so that the running instances don't pile up (they stay loaded otherwise, same as you noticed when you were running new scripts over and over).

You could make your functions end by having them return something. You can take this return value and use it to determine what to do next - eg, go back a page, forward a page, return to the main menu, etc.

Ideally you'd have one function for showing pages. You'd pass it parameters indicating which you want.

local function moo(par1, par2)
  print(par1)
  print(par2)

  print("onwards?")
  local input = read()

  return input
end

local caughtValue = moo("hello", "world")
print("Function returned "..caughtValue)
se7en #9
Posted 05 July 2014 - 06:41 PM
It's best not to, at least, not in that manner. Have each page function end instead, so that the running instances don't pile up (they stay loaded otherwise, same as you noticed when you were running new scripts over and over).

You could make your functions end by having them return something. You can take this return value and use it to determine what to do next - eg, go back a page, forward a page, return to the main menu, etc.

Ideally you'd have one function for showing pages. You'd pass it parameters indicating which you want.

local function moo(par1, par2)
  print(par1)
  print(par2)

  print("onwards?")
  local input = read()

  return input
end

local caughtValue = moo("hello", "world")
print("Function returned "..caughtValue)

i think i didnt catch that…
Or in other words, i think u misunderstanded me…

What i do mean:

Got a screen with waiting to input player touch_monitor, when it is done and in right place, i load an function by page1(). Then i got another screen with touch_monitor, if i push next page button there should be.. should be what ?

If i return at the next page button, how do i get to another function?

Any possible way to fix that? Maybe sort of main.page1() links ?

P.s. or when using shell.run, maybe there is possible way to close other running proccesses in each other program ? so it won't pile up…

Maybe ill show what i got so u can see what ive already done:

http://pastebin.com/vj2Hefhy
Edited on 05 July 2014 - 08:28 PM
Bomb Bloke #10
Posted 06 July 2014 - 07:11 AM
Ok, so here's a summary of your current code, focusing on the structuring:

local function skyblock1()
	-- Write page 1 of the skyblock note.

	-- Wait for input. Based on which button is pushed, either:
	  -- Go back to the main menu, or
	  -- go to the next page.
end

local function skyblock2()
	-- Write page 2 of the skyblock note.

	-- Wait for input. Based on which button is pushed, either:
	  -- Go back to the main menu, or
	  -- go to the first page.
end

-- Draw the main menu.

-- Wait for input. Based on which button is pushed, either:
  -- Go to the first page of the rules
  -- Go to the first page of skyblock
  -- Go to the first page of the commands
  -- etc

What I'm suggesting is that you use function parameters and return values to allow the different areas of your code to communicate with each other. For example:

local function drawPage(pageName, pageNum)
  if pageName == "command" then
    if pageNum == 1 then
      -- Write the first "command" page.
    elseif pageNum == 2 then
      -- Write the second "command" page.
    end
  else if pageName == "rules" then
    if pageNum == 1 then
      -- Write the first "rules" page.
    elseif pageNum == 2 then
      -- Write the second "rules" page.
    end
  end

  -- Render buttons at the bottom.
  -- Only render a back button if not on the first page, etc.

  -- Wait for the user to push a button:
  while true do
    local event, side, x, y = os.pullEvent("monitor_touch")

    if (back a page button pushed) and pageNum > 1 then
      return pageNum - 1
    elseif (forward button pushed) and pageNum < 3 then  -- If you're thinking "but some subjects have more than two pages", then now'd be a good time for you to start researching tables.
      return pageNum + 1
    elseif (main menu button pushed) then
      return
    end
  end
end

-- Draw the main menu.

-- Wait for input:
while true do
  local event, side, x, y = os.pullEvent("monitor_touch")

  local pageName
  local pageNum = 1

  if (rules button pushed) then
    pageName = "rules"
  elseif (commands button pushed) then
    pageName = "command"
  end

  while pageName and pageNum do  -- Keep showing pages until one of these values becomes undefined.
    pageNum = drawPage(pageName, pageNum)
  end
end

Bear in mind that there are an immeasurable number of ways you can do this. The above isn't the most efficient; it's merely an example I'm hoping will make sense to you. It's geared towards demonstrating use of parameters and return values, but truth be told, once you understand them, you should be able to see why a function call isn't needed here in the first place.
Edited on 06 July 2014 - 05:12 AM
se7en #11
Posted 08 July 2014 - 05:17 PM
That's really best solution and exactly what ive looking for!
Yeah, there would be more advanced solutions, with multiple pages on every "screen", not always same number to all, but i dont need it now.

Thanks mate again !

P.s. I would like to know 1 more thing about that.

Got something similar to:
m.write("Page ")
m.write(pageNum)
m.write("/2")

But it keeps displaying "Page 1.0/2"
How do i do it "Page 1/2" (with no decimal spaces) ?

And one more thing, "Menu" button works, but it seems to not showing screen of main menu, but it stays on last page (still pushing place where first button should be works as it would be on main menu).

Here is what i got so far: http://pastebin.com/4CDypJbV
Edited on 08 July 2014 - 03:30 PM
Dog #12
Posted 08 July 2014 - 05:25 PM
Try this…

m.write(tostring(pageNum))

This will eliminate the '.0' displayed at the end of the number
se7en #13
Posted 08 July 2014 - 06:02 PM
Try this…

m.write(tostring(pageNum))

This will eliminate the '.0' displayed at the end of the number

Yep, that did the job, thanks!
Still need help with the "back to main menu" problem :)/>
Bomb Bloke #14
Posted 09 July 2014 - 12:18 AM
You'd want to shove the main menu drawing code into its own function. Call it on startup, then again whenever the "return to menu" button gets pushed.

Like this:

Spoiler
local function drawMainMenu()
  -- Draw the main menu.
end

local function drawPage(pageName, pageNum)
  if pageName == "command" then
    if pageNum == 1 then
	  -- Write the first "command" page.
    elseif pageNum == 2 then
	  -- Write the second "command" page.
    end
  else if pageName == "rules" then
    if pageNum == 1 then
	  -- Write the first "rules" page.
    elseif pageNum == 2 then
	  -- Write the second "rules" page.
    end
  end

  -- Render buttons at the bottom.
  -- Only render a back button if not on the first page, etc.

  -- Wait for the user to push a button:
  while true do
    local event, side, x, y = os.pullEvent("monitor_touch")

    if (back a page button pushed) and pageNum > 1 then
	  return pageNum - 1
    elseif (forward button pushed) and pageNum < 3 then  -- If you're thinking "but some subjects have more than two pages", then now'd be a good time for you to start researching tables.
	  return pageNum + 1
    elseif (main menu button pushed) then
	  drawMainMenu()
	  return
    end
  end
end

drawMainMenu()

-- Wait for input:
while true do
  local event, side, x, y = os.pullEvent("monitor_touch")

  local pageName
  local pageNum = 1

  if (rules button pushed) then
    pageName = "rules"
  elseif (commands button pushed) then
    pageName = "command"
  end

  while pageName and pageNum do  -- Keep showing pages until one of these values becomes undefined.
    pageNum = drawPage(pageName, pageNum)
  end
end
se7en #15
Posted 09 July 2014 - 05:45 AM
You'd want to shove the main menu drawing code into its own function. Call it on startup, then again whenever the "return to menu" button gets pushed.

Like this:

Spoiler
local function drawMainMenu()
  -- Draw the main menu.
end

local function drawPage(pageName, pageNum)
  if pageName == "command" then
	if pageNum == 1 then
	  -- Write the first "command" page.
	elseif pageNum == 2 then
	  -- Write the second "command" page.
	end
  else if pageName == "rules" then
	if pageNum == 1 then
	  -- Write the first "rules" page.
	elseif pageNum == 2 then
	  -- Write the second "rules" page.
	end
  end

  -- Render buttons at the bottom.
  -- Only render a back button if not on the first page, etc.

  -- Wait for the user to push a button:
  while true do
	local event, side, x, y = os.pullEvent("monitor_touch")

	if (back a page button pushed) and pageNum > 1 then
	  return pageNum - 1
	elseif (forward button pushed) and pageNum < 3 then  -- If you're thinking "but some subjects have more than two pages", then now'd be a good time for you to start researching tables.
	  return pageNum + 1
	elseif (main menu button pushed) then
	  drawMainMenu()
	  return
	end
  end
end

drawMainMenu()

-- Wait for input:
while true do
  local event, side, x, y = os.pullEvent("monitor_touch")

  local pageName
  local pageNum = 1

  if (rules button pushed) then
	pageName = "rules"
  elseif (commands button pushed) then
	pageName = "command"
  end

  while pageName and pageNum do  -- Keep showing pages until one of these values becomes undefined.
	pageNum = drawPage(pageName, pageNum)
  end
end

weird but it works. have no idea it will work since earlier couldnt call function while i was in another function.

But whatever, thanks mate again! ;)/>