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

Cannot read Sensor (OpenPeripheralAddons)

Started by gfcwfzkm, 30 March 2014 - 12:32 PM
gfcwfzkm #1
Posted 30 March 2014 - 02:32 PM
Hi Pro's

im trying to read a Sensor from OpenPeripheralAddons. I already could do read something in the lua prompt.

But now i wanted to make a lua program that refresh all 1.5 seconds. But now it cant read the usernames :o/>

in the lua shell, i typed in: print(textutils.serialize(p.getPlayerNames()))
and that works.
Now i wanted to print this on a monitor. But now my code dont works and i dont understand why :(/>


    local radar = peripheral.wrap("top")
    local x,y = 1,1
    local monitor = peripheral.wrap("left")
    local timer = os.startTimer(1.5)
    local counter = 0
    print("Radarsystem online")
    while true do
	  local event, param = os.pullEvent("key")
	  if param == "timer" then
	    monitor.clear()
	    counter = table.getn(radar.getPlayerNames())
	    if counter ~= 0 then
		  for i=0,counter,i+1 do
		    monitor.write(radar.getPlayerNames()[i]) --is doing nothing :/
		    x,y = monitor.getCursorPos()
		    y = y + 1
		    monitor.setcursorPos(1,y)
		  end
	    end
	    timer = os.startTimer(1.5)
	  elseif param == 16 then
	    print("closing program...")
	    error("Radar System Closed")
	  end
    end

i hope you pro's can see my fault

Thanks

gfc
CometWolf #2
Posted 30 March 2014 - 02:41 PM
Where on earth did you learn to do for loops?

for i=0,counter,i+1 do
I can't even begin to fathom the results of this lol

In Lua a numerically indexed table starts at 1, so we should start the loop at 1, not 0. The second argument is the end of the loop, so that is correct, the third one however is the increment. Since i is technically nil at this point, i would imagine it would error. What we want is just an increment of 1, however that is the default value, so we don't need to supply one.
It should be

for i=1,counter do
gfcwfzkm #3
Posted 30 March 2014 - 02:50 PM
i learned it in VB.net and C. And there, the table starts from '0' (makes more sense) so i thinked that this is in lua the same.

But well, its still not printing the players… :/
CometWolf #4
Posted 30 March 2014 - 03:21 PM
Well no, the timer condition will never be fulfilled because you specifically only pull key events.

		  local event, param = os.pullEvent("key")

also the result of a timer is not a string, it's a numerical id code which os.startTimer returns when you call it

		  if param == "timer" then --ditch the quotes

and finally, this is just all around a bad idea.

		    counter = table.getn(radar.getPlayerNames())
		    if counter ~= 0 then
				  for i=0,counter,i+1 do
				    monitor.write(radar.getPlayerNames()[i]) --is doing nothing :/
You should instead get the table once and store it, then check the size of it, then iterate through that same table. What you are doing instead is you get the table of players, and check the amount. Then you loop for the amount, getting the table over again everytime. This is a waste of processing power, and may cause errors if the player moves out of range of the sensor in that time.
gfcwfzkm #5
Posted 30 March 2014 - 03:53 PM
yes :/

i changed the code. is it "cleaner" now? I get now a realy strange error: Too long without yielding
And no linenumbers or something like that.


    local radar = peripheral.wrap("top") --Radar Pos
    local x,y = 1,1  --Monitor Cursor Position
    local players    --Spieler Tabelle
    local monitor = peripheral.wrap("left") --Monitor Pos
    local timer0 = os.startTimer(1)
    os.queueEvent("key")  --Auf Tastenanschlag warten
	
    print("Radarsystem gestartet")
    while true do
	  local event, param = os.pullEvent()
	  if param == timer0 then
	    monitor.clear()
	    players = radar.getPlayerNames()
	    for num,target in pairs(players) do
		  monitor.write(target)
		  x, y = monitor.getCursorPos()
		  y = y + 1
		  monitor.setCursorPos(1,y)
	    end
	    x,y = 1,1
	    timer0 = os.startTimer(1)
	  end
	  if param == 16 then
	    error("Radarsystem beendet")
	  else
	    os.queueEvent("key")
	  end
    end

Well, i think the for-loop looks now better. But what means that error?
CometWolf #6
Posted 30 March 2014 - 04:02 PM
The error is the result of you constantly queuing an empty key event. Why are you queuing it in the first place?
Edited on 30 March 2014 - 02:04 PM
gfcwfzkm #7
Posted 30 March 2014 - 04:16 PM
Aww, right. Thanks, it works now =)