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

Compucraft problem: Lua stupidity on my part

Started by Impervium, 12 October 2012 - 02:32 PM
Impervium #1
Posted 12 October 2012 - 04:32 PM
I'm BRAND NEW to Lua.
So I'm making a script to be able to get sensor readings on a monitor instead of a console itself, in realtime.
Problem is: The example I found on youtube only showed how the data got outputted within a console (computer) in a string array of sorts. I've done several attempts and searched the net for hours. At some point I thought I was onto something, but the problem is, the array outputted on a console can't be outputted on a monitor seeing as a monitor needs a new line for just about everything it seems…

So the entire bash goes into one line, which means I would have to make a screen a mile long and things would be not very user friendly or organized at all..



Cursor1 = 8
Cursor2 = 8
os.unloadAPI("sensors")
os.loadAPI("/rom/apis/sensors")

revfunc = {}
mon = peripheral.wrap("right")
mon.clear()
mon.setCursorPos(5,1)
mon.setTextScale(1)
mon.write("test")

function printDict(data)
for i,v in pairs(data) do
mon.write (tostring(i).." - "..tostring(v))
end
end

ctrl = sensors.getController()
mon.setCursorPos(6,2)
mon.write(ctrl)
data = sensors.getSensors(ctrl)
mon.setCursorPos(7,3)

EnergySensor = data[1]
Engine = data[2]

data = sensors.getSensorInfo(ctrl,Engine)
mon.setCursorPos(7,4)
printDict(data)
mon.setCursorPos(9,10)
mon.write(data[9])
mon.write(data[2])

Am I just doing this a very hard way?
Anyone got a good solution/explanation to my problem here?

Also, on a different note, I can never use any advanced code on multiplayer tekkit, due to my Alt Gr button being registered as a Ctrl button, and my right Ctrl button doing nothing, so in other words I'm unable to make the [] {} symbols.. among others.


Suggestions are greatly appreciated!



Keep in mind, I know I can add a function and a loop to my script to make it realtime, but one thing at a time here.
Doyle3694 #2
Posted 12 October 2012 - 04:40 PM
I'm suffering with you bro…. Swedish keyboard ftw!
darkrising #3
Posted 14 October 2012 - 02:48 PM
If http is turned on you could use pastebin to get your programs to your computer :)/>/>

If your output was one long string you could just use a split function and print each part of the array as needed, heres a good split function I use a lot:

function split(str, pattern)
  local t = { }
  local fpat = "(.-)" .. pattern
  local last_end = 1
  local s, e, cap = str:find(fpat, 1)
  while s do
    if s ~= 1 or cap ~= "" then
      table.insert(t,cap)
    end
    last_end = e+1
    s, e, cap = str:find(fpat, last_end)
  end
  if last_end <= #str then
    cap = str:sub(last_end)
    table.insert(t, cap)
  end
  return t
end

It returns an array, pattern is how you want the string to be split, if you had a string "something!S!somethingelse!S!everything" it would split into an array as t = {"something", "somethingelse", "everything"}

hope this helps :)/>/>

edit: derp, I didnt read everything right, I suppose you could just do alot of setCursorPos() for the monitor for each bit