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

need help with beginning

Started by jim_veens, 25 April 2014 - 10:27 PM
jim_veens #1
Posted 26 April 2014 - 12:27 AM
i want to make a program but have No idea, how to start!

the program works like this,
in the monitor, there will be a looping slideshow like thing, showing rules, information, ETC!
because i am creating a village for beginners, in a server!

when you start up the computer, it shows a screen, with two buttons,
one main button, in the center, and one in the bottom right corner,
the main button needs to say "Start Slideshow" and when you click at it, it says "Stop Slideshow"
you know what it needs to do,

and when you click at Login, you need to write a password, and when you write it, you can access the terminal, etc, and can edit the program ETC,

can anyone tell me how to start? or, just write the program for me? it would be EXTREMELY helpful!
KingofGamesYami #2
Posted 27 April 2014 - 04:28 AM
For the buttons, I'd take a look at a modified version of the Touchpoint api. http://pastebin.com/TNB10BJU, http://www.computerc...pi/page__st__20
The looping slideshow, you could write individual functions that render each screen, and then use os.pullEvent() to tell it to continue.
The password part (by itself, not including the buttons) is pretty simple, use os.pullEventRaw() to make your program un-terminatable, then continuously check the password from inside a loop. If the password is correct, break the loop to end the program, if it isn't simply continue the loop.
Edited on 27 April 2014 - 02:32 AM
CCJJSax #3
Posted 28 April 2014 - 05:32 PM
Be extremely careful when using os.pullEventRaw(). if you over-write the terminate event, miscode it, and it's a startup program, then you're not going to be able to use that computer again unless you have access to the actual files on the HDD.

I'm not sure touchpoint would be necessary. He only needs 2 buttons. But there are advantages to having it, so that's up to you. For the login part, I'm not sure you need a button on a monitor for that. Typing in your password would be something you do inside the computer anyway, and I'm assuming most people will only need to see the monitor, and only admins or VIPs will need the computer. I would keep the login inside the computer and the slideshow on the monitor. Which would make it only use 1 button.

We probably won't write the whole program for you, but we love giving pointers and teaching people how to code for themselves.

Lyqyd has a computer basics post in the forums and lots of other tutorials are found here

SpoilerLyqyd Computer Basics Series
Computer Basics 1
Computer Basics 2

SurferPup's monitor button tutorial series
part 1
following parts linked in part 1

SerferPup's lua tutorial
lua basics

I'll give you a few pointers right here. I know there is a ton to learn and the links above have lots of info that you probably don't need with your specific program.

Spoiler

-- first you need to wrap the monitor as a peripheral.
mon = peripheral.wrap("top") -- replace top with the side it's on.  "left", "right", "back", "front", "top", "bottom".

-- to write something to the computers GUI use this
print("hello") -- print() adds a [url="http://en.wikipedia.org/wiki/Carriage_return"]carriage return[/url].  simply put, makes a  new line automatically
term.write("world") -- doesn't add a carriage return

-- to write it to the monitor, you need to use the variable you wrapped your monitor to, in our case, mon
mon.write("hello world") -- don't put in term.

-- remember mon takes terms place when using this monitor, always


-- to clear the entire screen type
term.clear()

-- to clear only 1 line you need to set the line first, then clear the line
term.setCursorPos(1,5) -- here x = 1, y = 5.  
term.clearLine()


-- to set [url="http://computercraft.info/wiki/Colors_(API)"]colors [/url]you need an advanced monitor/computer
term.setTextColor(colors.red)
term.setBackgroundColor(colors.blue)
print("hello") -- monitors don't have a print function by default. just a mon.write()

that is most of what you'll need to write to a monitor. remember you need to clear your screen and set the cursor position to avoid clustered text and to make text start at the top.
I'll show you an easy function that will give you a good understanding of how functions work.


-- first you have to give the function a name.

-- local means it's native to this program.  It's good practice.
-- then you have to say "function"
-- then what you want the function to be called, followed by ()
local function clear()
  term.clear() -- clears the screen
  term.setCursorPos(1, 1) -- sets position to the top left
end -- ends the function

--then you need to call it whenever you need it.
clear()


you could make a function for each slide by putting in term.write("line 1") then setting the cursor position down 1
then for line two use term.write("line2") as shown below.

to change slides automatically you need a loop. look for the function "slideshow()" in the below example


mon = peripheral.wrap("top")

local function clear()
  mon.clear()
  mon.setCursorPos(1, 1)
end

local function slide1()
  clear() -- don't forget this
  mon.write("this is line 1")
  mon.setCursorPos(1, 2)
  mon.write("this is line 2")
  mon.setCursorPos(1, 3)
end

local function slide2()
  clear()
  mon.write("slide 2, line 1")
  mon.setCursorPos(1, 2)
  mon.write("slide 2, line2 ")
end

local function slideshow() -- functions can call other functions.
  slide1() -- runs slide1()
  sleep(5) -- waits for 5 seconds
  slide2() -- runs slide2()
  sleep(5)
end

slideshow() -- calls the slideshow() function

and so on. That is how you make the slideshow.

finally the os.pullEvent() is probably the trickiest, but easy once you get the hang of it. Test with this and make sure the program works perfectly before using os.pullEventRaw()
in the os.pullEvent() link, you will find loads of information of what each event does.


mon = peripheral.wrap("top")
w, h = mon.getSize() -- gets the size of the monitor
timer = os.startTimer(1) -- this will be useful for the
while true do -- infinite loop
  mon.setCursorPos(5, h)
  mon.setTextColor(colors.cyan)
  mon.setBackgroundColor(colors.lightGray)
  mon.write(" click me ")
  mon.setTextColor(colors.white)
  mon.setBackgroundColor(colors.black)
  evt, p1, p2, p3 = os.pullEvent() -- os.pullEvent() waits for something to happen, then returns what happened
  -- evt means the type of event.
  -- p1 is the next thing that is returned in the event the rest are next in line.
  if evt == "monitor_touch" then -- use == when comparing.  use = when setting a variable
	mon.setCursorPos(p2, p3) -- sets the pos to where you touched the monitor
	mon.write(" ") -- makes a dot at that point
	sleep(1) -- just to keep that point there for a sec
	if p2 >= 5 and p2 <= 15 then
	  mon.clear()
	  mon.setCursorPos(1, 1)
	  mon.write("you did it!")
	  break
	end
  elseif evt == "timer" then
    -- this is where your timer would be to change slides.
    -- I recommend using this method over using the one in my earlier example.
    timer = os.startTimer(1) -- make sure to make this how long you want the slide to be.
  end
end
mon.setCursorPos(1, 2)
mon.write("if you didn't, click other places on the screen")



The password is easy enough. look at this.

So without testing any of this, I believe it all should work, and give you a good idea of what to do.
Edited on 28 April 2014 - 04:44 PM
Lyqyd #4
Posted 28 April 2014 - 05:41 PM
That's not true. You simply need a disk drive and a disk with a blank file named "startup".
CCJJSax #5
Posted 28 April 2014 - 06:41 PM
That's not true. You simply need a disk drive and a disk with a blank file named "startup".

Interesting… I actually used a disk for the first time yesterday. I have always just copied over the files from my HDD so I never needed them. Thanks for the tip.