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

Help with infinite loop and auto start

Started by wannab, 25 October 2012 - 04:13 PM
wannab #1
Posted 25 October 2012 - 06:13 PM
I am trying to put together a script so that the screen in my spawn area displays info on an infinite loop and starts up when the server goes through its scheduled reboot.

what i have gotten so far is how to display the text to the top monitor with this code


local tText = {
"multiple",
"lines",
" of info"
}

local sSide = "top"
local nTextSize = 1.5 – change the text size here (from 1 to 5)

local function printCenter(mon, t)
local w, h = mon.getSize()
local y = math.floor((h / 2) - (#t / 2)) - 1
for i, line in ipairs(t) do
local x = math.floor((w / 2) - (#line / 2))
mon.setCursorPos(x, y + i)
mon.write(line)
end
end

local mon = peripheral.wrap(sSide)
mon.setTextScale(nTextSize)
printCenter(mon, tText)

what i would also like to do is have the script display 3 different screens of info with a timer between each page being displayed, i did some searching and i think im supposed to use os.sleep for the timer, just not sure how or where to put the code for the second page, i assume i need to clear screen b4 each display with monitor.clear

as for infinite loop i have seen some post to use something like: local function infiniteloop while true do do stuff, and what would i need to do to make this start when my server goes through its scheduled restarts

im just not sure how to put all this information together to make a workable script as i have no coding experience

thanks for any help
Ditto8353 #2
Posted 25 October 2012 - 06:21 PM
The loop and screen changing is pretty straight forward.
while true do
   screen1()
   os.sleep(5) //5 seconds
   screen2()
   os.sleep(5)

   screen3()
   os.sleep(5)
end

However, I do not know if you can have a computer turn on after the server restart without clicking on it. I am sure someone else here can answer that part.
wannab #3
Posted 25 October 2012 - 06:37 PM
so how do i get screen1() to display the code that i have already started on, do i put that code in the () or do i have to name the code i have so far screen1 and this is just a call to that
Ditto8353 #4
Posted 25 October 2012 - 06:49 PM
You could just replace 'screen1()' with the code that displays the screen, but a cleaner way to do it would be to make a function for each screen. I just put those fake functions there so my explanation would be shorter.
wannab #5
Posted 25 October 2012 - 07:16 PM
yeh im reading roblox tutorials on functions right now, so if i created these functions as screen1 etc, i would do:

function screen1()
code
end

my next question is can i make the functions after the while true do loop at the beginning

so it the code would look like :

while true do
screen1()
end

function screen1()
code
end
Doyle3694 #6
Posted 25 October 2012 - 07:21 PM
yeah, that would work ^_^/>/>
Ditto8353 #7
Posted 25 October 2012 - 07:31 PM
You need to define the functions before you can use them.
Orwell #8
Posted 25 October 2012 - 07:51 PM
You need to define the functions before you can use them.

You can also just forward declare it.

local f, g	-- 'forward' declarations
  
function g ()
  return f()
end
  
function f ()
  return 42
end

print( g() )

Just sharing this with the people that don't know yet. ^_^/>/>
Ditto8353 #9
Posted 25 October 2012 - 07:54 PM
If you attempt to call a function before it has been declared…
local test

test()

local function test()
   print("It works")
end

input:3: attempt to call local 'test' (a nil value)
Doyle3694 #10
Posted 25 October 2012 - 07:56 PM
oh derp, it's hard to understand your code without code tags.I thought that first codebit was part of the code. Thought you meant if you could redefine functions
Orwell #11
Posted 25 October 2012 - 09:30 PM
If you attempt to call a function before it has been declared…
local test

test()

local function test()
   print("It works")
end

input:3: attempt to call local 'test' (a nil value)

Do you know the difference between declaring and defining? Anyways, it seems that you need to have them defined before you're at the point of calling them anyway.
cheekycharlie101 #12
Posted 25 October 2012 - 10:40 PM
to make it run on a scheuled server reboot just type edit startup. then in the start up file put this:


shell.run("monitor", "back", "test")

just change the back to the direction you need and the test to what your program is called. simple
wannab #13
Posted 25 October 2012 - 11:57 PM
im still not sure how to put all of this together

how would i set this code

local tText = {
"multiple",
"lines",
" of info"
}
local sSide = "top"
local nTextSize = 1.5 -- change the text size here (from 1 to 5)
local function printCenter(mon, t)
local w, h = mon.getSize()
local y = math.floor((h / 2) - (#t / 2)) - 1
for i, line in ipairs(t) do
local x = math.floor((w / 2) - (#line / 2))
mon.setCursorPos(x, y + i)
mon.write(line)
end
end
local mon = peripheral.wrap(sSide)
mon.setTextScale(nTextSize)
printCenter(mon, tText)

in a function to be called screen1 using the method posted early with while true do
wannab #14
Posted 26 October 2012 - 12:56 AM
well this is what i got and im getting an error at line 30 when using monitor.clear() but this is the code if someone can point me in the proper direction of what i am doing wrong


while true do
  function screen1()
	local aText = {
	"Welcome to Miners' Haven Tekkit Server",
	"",
	" server ",
	"info",
	" goes ",
	"here"
	}
	local sSide = "top"
	local nTextSize = 1.5 -- change the text size here (from 1 to 5)
	local function printCenter(mon, a)
	local w, h = mon.getSize()
	local y = math.floor((h / 2) - (#a / 2)) - 1
	for i, line in ipairs(a) do
			local x = math.floor((w / 2) - (#line / 2))
			mon.setCursorPos(x, y + i)
			mon.write(line)
		end
	end
	local mon = peripheral.wrap(sSide)
	mon.setTextScale(nTextSize)
	printCenter(mon, aText)
	os.sleep(10)
	monitor.clear()
  end

  screen1()

  function screen2()
	local bText = {
	"Welcome to Server",
	"",
	"some",
	"more",
	"info",
	"here"
	}
	local sSide = "top"
	local nTextSize = 1.5 -- change the text size here (from 1 to 5)
	local function printCenter(mon, ^_^/>/>
	local w, h = mon.getSize()
	local y = math.floor((h / 2) - (#b / 2)) - 1
	for i, line in ipairs(:P/>/> do
			local x = math.floor((w / 2) - (#line / 2))
			mon.setCursorPos(x, y + i)
			mon.write(line)
		end
	end
	local mon = peripheral.wrap(sSide)
	mon.setTextScale(nTextSize)
	printCenter(mon, bText)
	os.sleep(10)
	monitor.clear()
end
	screen2()
end

after my 10 second delay i get attempt to index ? (a nil value) which also occurs if try monitor.clear(top) im not sure what im doing wrong but at that error the script halts and will not continue, it will however display the first set of text to the monitor
KillaVanilla #15
Posted 26 October 2012 - 01:05 AM

monitor.clear()
should be

mon.clear()
wannab #16
Posted 26 October 2012 - 01:18 AM
yeh that worked i was going off of what i saw on this page

http://www.computercraft.info/wiki/index.php?title=Peripheral_(API)

it has monitor.clear()

but thanks so much to everyone who helped, now i gotta try that startup script charlie posted

again thanks everyone
Lyqyd #17
Posted 26 October 2012 - 04:02 PM
yeh that worked i was going off of what i saw on this page

http://www.computercraft.info/wiki/index.php?title=Peripheral_(API)

it has monitor.clear()

but thanks so much to everyone who helped, now i gotta try that startup script charlie posted

again thanks everyone

Yes, and it also says that you're supposed to replace 'monitor' with whatever variable you wrapped that side into.