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

REQUEST: It's been too long since I've used LUA

Started by koalakyle, 22 September 2015 - 05:06 AM
koalakyle #1
Posted 22 September 2015 - 07:06 AM
It's been years since I programmed anything with Lua and I've unfortunately forgotten most of what little I knew. I need some help writing, or someone providing me with if possible, a program that players can input their name and a title into a computer and then output to a monitor on top of the computer.

I'd write it myself if I could but I can barely remember how I made my password doors.

Any help would be greatly appreciated. The monitor size is five across and four high if that matters.
Lyqyd #2
Posted 22 September 2015 - 07:46 PM
Moved to General.
KingofGamesYami #3
Posted 22 September 2015 - 07:51 PM

--#Finding monitor
local mon
for k, v in pairs( peripheral.getNames() ) do
  if peripheral.getType( v ) == "monitor" then
    mon = peripheral.wrap( v )
    break
  end
end

--#Error if no monitor is available
if not mon then
  error( "No monitors connected!", 0 )
end

--#Get their input
write( "Name: " )
local name = read()
write( "Title: " )
local title = read()

--#Put it on the monitor
mon.setCursorPos( 1, 1 )
mon.write( "Name: " .. name )
mon.setCursorPos( 1, 2 )
mon.write( "Title: " .. title )
Edited on 22 September 2015 - 05:52 PM
gollark8 #4
Posted 22 September 2015 - 08:26 PM

--#Finding monitor
local mon
for k, v in pairs( peripheral.getNames() ) do
  if peripheral.getType( v ) == "monitor" then
    mon = peripheral.wrap( v )
    break
  end
end

--#Error if no monitor is available
if not mon then
  error( "No monitors connected!", 0 )
end

--#Get their input
write( "Name: " )
local name = read()
write( "Title: " )
local title = read()

--#Put it on the monitor
mon.setCursorPos( 1, 1 )
mon.write( "Name: " .. name )
mon.setCursorPos( 1, 2 )
mon.write( "Title: " .. title )

You should get mon with peripheral.find("monitor") instead of that.
Dog #5
Posted 22 September 2015 - 08:36 PM
<snip>
You should get mon with peripheral.find("monitor") instead of that.

Keep in mind, that won't work if koalakyle is using an older version of CC.
KingofGamesYami #6
Posted 22 September 2015 - 09:54 PM
I like to keep backwards compatibility :)/>
Lupus590 #7
Posted 22 September 2015 - 10:43 PM
you could do

if not peripheral.find then
  function peripheral.find()
    --code
  end
end
Lyqyd #8
Posted 22 September 2015 - 10:56 PM
KingOfGamesYami's code isn't even particularly great for backwards compatibility. The difference between the introduction of peripheral.find and peripheral.getNames isn't as big as you think.
H4X0RZ #9
Posted 22 September 2015 - 11:10 PM
What about this code then?

 
for k,v in pairs(rs.getSides()) do
  if peripheral.isPresent(v) then
    -- do something here
  end
end

(I hope I didn't mess up anything.I'm On my phone right now.)
Lyqyd #10
Posted 23 September 2015 - 01:29 AM
Well, peripheral.getType instead of peripheral.isPresent, but yes, that would cover the most versions that I know of. Of course, it doesn't cover networking cables, so if one were to really want to push for grabbing the first peripheral of a type no matter which version you were on, I might do something like:


function getFirstPeripheral(mask)
  local func = peripheral.getNames or rs.getSides
  if peripheral.find then
    return (peripheral.find(mask))
  end
  for _, side in pairs(func()) do
    if peripheral.getType(side) == mask then
	  return peripheral.wrap(side)
    end
  end
end
koalakyle #11
Posted 23 September 2015 - 02:13 AM
Thanks a tonne. That's almost exactly what I needed, I realise I wasn't 100% clear, I needed it to be able to store multiple names/titles and display them on a monitor, preferably able to add more names as more players join

Thank you again for your help so far.
KingofGamesYami #12
Posted 23 September 2015 - 03:33 AM
Ah, that's an easy modification:


--#Finding monitor (Yes I could do it differently but I'm doing it this way..)
local mon
for k, v in pairs( peripheral.getNames() ) do
  if peripheral.getType( v ) == "monitor" then
    mon = peripheral.wrap( v )
    break
  end
end

--#Error if no monitor is available
if not mon then
  error( "No monitors connected!", 0 )
end

local players = {}

while true do
  --#Get their input
  write( "Name: " )
  local name = read()
  write( "Title: " )
  local title = read()
  players[ #players + 1 ] = {name = name, title = title}

  --#Put it on the monitor
  mon.clear()
  for i = 1, #players do
    mon.setCursorPos( 1, i * 2 -1 )
    mon.write( "Name: " .. players[ i ].name )
    mon.setCursorPos( 1, i * 2 )
    mon.write( "Title: " .. players[ i ].title )
  end
end
Lupus590 #13
Posted 23 September 2015 - 09:13 AM
Yami's revised code will allow you to keep adding names but won't allow you to stop.

You could ask how many names the user wants to type in and loop that many times.