Jump to content




OpenPeripherals Glasses attempt to index ?

peripheral

2 replies to this topic

#1 Scarjit

  • Members
  • 3 posts
  • LocationGermany

Posted 16 February 2014 - 11:41 AM

Hi iv wrote an little script wich should monitor some Mfsu's using OpenPeripheral + Rednet.
Server Code:
m = peripheral.wrap("top")
bridge = peripheral.wrap("right")
rednet.open("top")
mfsu_0e = 0
mfsu_2e = 0
Cy = 1
--Mikees script
bridge.clear()
width = 200
local storageUnits = {
   {
		 ["id"] = "mfsu_Main",
		 ["name"] = "Main Power"
   }
}
local offset = 0
for key, storageUnit in pairs(storageUnits) do
  pxOffset = offset * 20
  storageUnit["label"] = bridge.addText(4, 4 + pxOffset, storageUnit["name"], 0x000000)
  storageUnit["bar"] = bridge.addBox(4, 14 + pxOffset, 0, 5, 0xCC0000, 0.9)
--  storageUnit["bar"].setZIndex(2)
  storageUnit["bg"] = bridge.addBox(4, 14 + pxOffset, width, 5, 0x000000, 0.5)
  offset = offset + 1
end
--Mikees script end
while true do
  local senderID, message, distance = rednet.receive()
  x = message:find("x")
  x = x+1
  msg = message:sub(x)
  value = string.sub(message, 1, x-2)
  write(msg..": "..value)

	if msg == mfsu_0 then do
	  mfsu_0e = value
	  end
	end
	if msg == mfsu_2 then do
	  mfsu_2e = value
	  end
	end
storageUnit = mfsu_Main
capacity = 80000000
amount = mfsu_0e+mfsu_2e
barwidht = width / capacity * amount
write(mfsu_0e)
storageUnit["bar"].setWidth(barwidht)

end
Client Code:
m = peripheral.wrap("top")
rednet.open("top")
mfsu0 = peripheral.wrap('mfsu_0')
mfsu2 = peripheral.wrap('mfsu_2')
while true do
-- MFSU_0
amount = mfsu0.getEUStored()
rednet.broadcast(amount.."xmfsu_0")
write(amount)
-- MFSU_2
amount = mfsu2.getEUStored()
rednet.broadcast(amount.."xmfsu_2")
write(amount)
-- Other Funktions
os.sleep(10)
term.clear()
term.setCursorPos(1,1)
end
The Clients sends strings like 40000000xmfsu_0
The server should decode them in to:
mfsu_0 (id of the mfsu) and 40000000 (actual energy)
Then he should save 40000000 to mfsu_0e
The last Part is for changing the bar on the glasses.
amount = mfsu_0e+mfsu_2e is because i have 2 msfu's that i want to combine.

#2 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 16 February 2014 - 06:32 PM

The error message would've contained a number indicating the line with the problem, and you also haven't mentioned which script throws the error. There are enough issues here that this makes it difficult to pinpoint the exact one.

Anyway, it looks like you're a bit unclear as to how "for" loops with "pairs" function. Take a look at these code segments:

local storageUnits = {
   {
                 ["id"] = "mfsu_Main",
                 ["name"] = "Main Power"
   }
}
.
.
.
for key, storageUnit in pairs(storageUnits) do
   .
   .
   .
end

When the loop runs, each iteration starts off by taking a key (an "index name") from "storageUnits", and storing it in the variable called "key". It also takes the value stored against that key, and stores that against "storageUnit".

For example, on the first go through the loop, "key" might be assigned the string "id", and "storageUnit" might be assigned the string "mfsu_Main". On the second run through, "key" might be assigned the string "name" and "storageUnit" might be assigned the string "Main Power". There is no third run through, as that accounts for all the keys in the table.

Inside the loop, you try to refer to "storageUnit" as though it's a table. It's not; it's always going to be a string, because you only put strings in the "storageUnits" table. Your only actual table is still "storageUnits" so that code would error if it ran.

Once the loop ends, the "key" and "storageUnit" variables are simply discarded.

Later on, you try to set "storageUnit" to the value of "mfsu_Main" (which is undefined, or "nil"), and then try to treat it as a table again. I believe this is the bit throwing the error.

I suggest linking to the script you're borrowing code from.

#3 Scarjit

  • Members
  • 3 posts
  • LocationGermany

Posted 17 February 2014 - 04:06 PM

Have rescripted it. New working code for the server is:
local bridge = peripheral.wrap("right")
rednet.open("top")
bridge.clear()
width = 200
x = 0
capacity = 80000000
amount = 0
msg = ""
message = ""
value = 0
mfsu0e = 1
mfsu2e = 1
local storageUnits = {
   {
		 ["id"] = "mfsu_0",
		 ["name"] = "Main Energy"
   }
}
local offset = 0
for key, storageUnit in pairs(storageUnits) do
  pxOffset = offset * 20
  storageUnit["label"] = bridge.addText(4, 4 + pxOffset, storageUnit["name"], 0x000000)
  storageUnit["bar"] = bridge.addBox(4, 14 + pxOffset, 0, 5, 0xCC0000, 0.9)
--  storageUnit["bar"].setZIndex(2)
  storageUnit["bg"] = bridge.addBox(4, 14 + pxOffset, width, 5, 0x000000, 0.5)
  offset = offset + 1
end
while true do
local senderID, message, distance = rednet.receive()
x = message:find("x")
x = x+1
msg = message:sub(x)
value = tonumber(string.sub(message, 1, x-2))
if msg == "mfsu_0" then do
  mfsu0e = value
  amount = mfsu0e+mfsu2e
  end
end
if msg == "mfsu_2" then do
  mfsu2e = value
  amount = mfsu2e+mfsu0e
  end
end
  for i=#storageUnits,1,-1 do
	    widht = 200
	    storageUnit = storageUnits[i]
		  storageUnit["bar"].setWidth(width / capacity * amount)
		  write("W"..tostring(widht))
		  write("C"..tostring(capacity))
		  write("A"..tostring(amount))
	    end
  end






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users