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

Calling from tables and using loops

Started by OnyxFox, 11 August 2015 - 07:43 PM
OnyxFox #1
Posted 11 August 2015 - 09:43 PM
I'm trying to find an easier way to add items to my HUD with OP terminal glasses using loops and tables to shorten my code and time to write things in.
Check out function Dis(), it won't let me call my arguments.
Any general suggestions/tweaks are welcome.

Code

local glass = peripheral.wrap('bottom')
rednet.open('right')
local op = 0.4
bridgeS = "..."
lavaS = "..."
manaS = "..."
local P = {
  time   = {1,1,55,10, 'box',
			box = {0xFFFFFF, op},
			text = {textutils.formatTime(os.time(), false), 0x000000}
		   },
  bridge = {60,12,55,10, 'box',
			box = {0x397fdb, op},
			text = {bridgeS, 0xffffff}
		   },
  lava   = {60,1,55,10, 'fluid',
			fluid = "lava",
			text = {lavaS .. "% Lava", 0xffffff}
		   },
  mana   = {1,12,55,10, 'box',
			box = {0xff34b3, op},
			text = {manaS .. "% Mana", 0xff34b3}
		   }
}

local function getUpdate()
  while true do
	local event, ID, mess = os.pullEvent('rednet_message')
	  if ID == 26 then
		manaS = mess
	  elseif ID == 36 then
		lavaS = mess
	  elseif ID == 16 then
		if mess == true then
		  bridgeS = "Open"
		else
		  bridgeS = "Closed"
		end
	  end
  end
end

local function commands()
  while true do
	local event, e1,e2,e3,e4 = os.pullEvent("glasses_chat_command")
	if e4 == "bridge" then
	  rednet.send(16, "zapp")
	end
  end
end

local function Dis(tName)
	glass.addBox(P.tName[1],P.tName[2],P.tName[3],P.tName[4],P.tName.box[1],P.tName.box[2])
	glass.addText(P.tName[1]+5,P.tName[2]+2,P.tName.text[1],P.tName.text[2])
end


function backDis()
  glass.addBox(0,0,120,25, 0x000000, op)
end

function updateDis()
  while true do
	glass.clear()
	backDis()
	Dis(time)
	Dis(mana)
	Dis(bridge)
	glass.sync()
	sleep(.1)
  end
end

parallel.waitForAny(updateDis, getUpdate, commands)


Also in function updateDis() I had a for loop to fire off all of the Dis() functions.

for i=1,#P do
    Dis(P [ i ] )
end
I realize this won't work because it is calling a table but I'm still looking for a work around.
Twijn #2
Posted 11 August 2015 - 10:00 PM
I am assuming that you're trying to get whatever the variable tName is in the Table, and I believe that this would be getting "tName" from the table (which is nil).

Try to use P[tName][1]
OnyxFox #3
Posted 11 August 2015 - 10:55 PM
I added that and it still returned nil
Twijn #4
Posted 11 August 2015 - 11:00 PM
I added that and it still returned nil

On the last part you added, you can do this:


for i,v in pairs(P) do
  --do stuff here. Note that "i" would be what it's called (time,bridge,lava,mana) and v would be the tables
end
OnyxFox #5
Posted 11 August 2015 - 11:11 PM
I added that and it still returned nil

On the last part you added, you can do this:


for i,v in pairs(P) do
  --do stuff here. Note that "i" would be what it's called (time,bridge,lava,mana) and v would be the tables
end

Ok so

for i,v in pairs(P) do
  Dis( i )
end
?

And the nil value is still returning in the first line of the Dis() function.
It doesn't like the P[tName][1]
Edited on 11 August 2015 - 09:13 PM
H4X0RZ #6
Posted 11 August 2015 - 11:46 PM
I added that and it still returned nil

On the last part you added, you can do this:


for i,v in pairs(P) do
  --do stuff here. Note that "i" would be what it's called (time,bridge,lava,mana) and v would be the tables
end

Ok so

for i,v in pairs(P) do
  Dis( i )
end
?

And the nil value is still returning in the first line of the Dis() function.
It doesn't like the P[tName][1]

Obviously it doesn't like it because you give it nil in the first place. At the bottom of the code, where you actually call Dis() you should put the arguments into "'s.

Also you could shorten the if-block for bridgeS: bridgeS = mess and" Open" or "Closed"
If you don't know how/why this works, you should read about the ternary operator.
Edited on 11 August 2015 - 09:50 PM
OnyxFox #7
Posted 12 August 2015 - 02:47 AM
Obviously it doesn't like it because you give it nil in the first place. At the bottom of the code, where you actually call Dis() you should put the arguments into "'s.

Also you could shorten the if-block for bridgeS: bridgeS = mess and" Open" or "Closed"
If you don't know how/why this works, you should read about the ternary operator.

Ok that fixed it. However function updateDis() no longer repeats.

Spoiler

local glass = peripheral.wrap('bottom')
rednet.open('right')
local op = 0.4
bridgeS = "..."
lavaS = "..."
manaS = "..."

local P = {
  time   = {1,1,55,10, 'box',
			box = {0xFFFFFF, op},
			text = {textutils.formatTime(os.time(), false), 0x000000}
		   },
  bridge = {60,12,55,10, 'box',
			box = {0x397fdb, op},
			text = {bridgeS, 0xffffff}
		   },
  lava   = {60,1,55,10, 'fluid',
			fluid = "lava",
			text = {lavaS .. "% Lava", 0xffffff}
		   },
  mana   = {1,12,55,10, 'box',
			box = {0x008000, op},
			text = {manaS .. "% Mana", 0xff34b3}
		   }
}


local function getUpdate()
  while true do
	local event, ID, mess = os.pullEvent('rednet_message')
	  if ID == 26 then
		manaS = mess
	  elseif ID == 36 then
		lavaS = mess
	  elseif ID == 16 then
		bridgeS = mess and "Open" or 'Closed'
	  end
  end
end

local function commands()
  while true do
	local event, e1,e2,e3,e4 = os.pullEvent("glasses_chat_command")
	if e4 == "bridge" then
	  rednet.send(16, "zapp")
	end
  end
end

local function Dis(tName)
  if P[tName][5] == 'fluid' then
	glass.addFluid(P[tName][1],P[tName][2],P[tName][3],P[tName][4],P[tName].fluid)
  else
	glass.addBox(P[tName][1],P[tName][2],P[tName][3],P[tName][4],P[tName].box[1],P[tName].box[2])
  end
	glass.addText(P[tName][1]+5,P[tName][2]+2,P[tName].text[1],P[tName].text[2])
end


function backDis()
  glass.addBox(0,0,120,25, 0x000000, op)
end

function updateDis()
  while true do
	glass.clear()
	backDis()
	Dis('time')
	Dis('mana')
	Dis('bridge')
	Dis('lava')
	glass.sync()
	sleep(.1)
  end
end

parallel.waitForAny(updateDis, getUpdate, commands)
OnyxFox #8
Posted 12 August 2015 - 03:16 AM
This is the working script from before I started trying to consolidate it.
Spoiler

local glass = peripheral.wrap('bottom')
rednet.open('right')
local op = 0.4
local P = {
  time   = {1,1,55,10},
  bridge = {60,12,55,10},
  lava   = {60,1,55,10},
  mana   = {1,12,55,10}
}



mana = "..."
lava = "..."
bridge = "..."
local function getUpdate()
  while true do
	local event, ID, mess = os.pullEvent('rednet_message')
	  if ID == 26 then
		mana = mess
	  elseif ID == 36 then
		lava = mess
	  elseif ID == 16 then
		brid = mess
	  end
  end
end

local function commands()
  while true do
	local event, e1,e2,e3,e4 = os.pullEvent("glasses_chat_command")
	if e4 == "bridge" then
	  rednet.send(16, "zapp")
	end
  end
end

local function bridgeDis()
  if brid == "true" then
	bridge = "Open"
  else
	bridge = "Closed"
  end
  glass.addBox(P.bridge[1],P.bridge[2],P.bridge[3],P.bridge[4],0x397fdb, op)
  local bridgeT = glass.addText(P.bridge[1]+5,P.bridge[2]+2,bridge,0xffffff)
  bridgeT.setScale(1)
end

local function lavaDis()
  glass.addFluid(P.lava[1],P.lava[2],P.lava[3],P.lava[4],"lava")
  glass.addText(P.lava[1]+5,P.lava[2]+2,lava .. "% Lava", 0xffffff)
end

local function timeDis()
  glass.addBox(P.time[1],P.time[2],P.time[3],P.time[4],0xFFFFFF, 1)
  glass.addText(P.time[1]+5,P.time[2]+2,textutils.formatTime(os.time(), false), 0x000000)
end

local function manaDis()
  glass.addBox(P.mana[1],P.mana[2],P.mana[3],P.mana[4], 0x008000, op)
  glass.addText(P.mana[1]+5,P.mana[2]+2,mana .. "% Mana", 0xff34b3)
end

function backDis()
  glass.addBox(0,0,120,25, 0x000000, op)
end

function updateDis()
  while true do
	glass.clear()
	backDis()
	timeDis()
	manaDis()
	lavaDis()
	bridgeDis()
	glass.sync()
	sleep(.1)
  end
end

parallel.waitForAny(updateDis, getUpdate, commands)