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

Failing for no reason

Started by Creeper9207, 17 May 2015 - 02:38 PM
Creeper9207 #1
Posted 17 May 2015 - 04:38 PM
I get an Attempt to index ? (a function value) on line 5 for no reason

Code (screenshot API)
Spoilerlocal term=term.native
local screen={}
screen.__index=term
setmetatable(screen,screen)
local w, h = term.getSize() –Erroring line #5
local blink=true
local color={}
color.text=colors.white
color.background=colors.black
color[2^0]="1"
color[2^1]="2"
color[2^2]="3"
color[2^3]="4"
color[2^4]="5"
color[2^5]="6"
color[2^6]="7"
color[2^7]="8"
color[2^8]="9"
color[2^9]="10"
color[2^10]="11"
color[2^11]="12"
color[2^12]="13"
color[2^13]="14"
color[2^14]="15"
color[2^15]="16"
local vscreen={}
vscreen.tcolor={}
vscreen.text={}
vscreen.bcolor={}

local function dtcolor() return 1 end
local function dbcolor() return 32768 end
local function dtext() return " " end

for y=0,h+1 do
vscreen.tcolor[y]={}
vscreen.text[y]={}
vscreen.bcolor[y]={}
vscreen.tcolor[y].__index=dtcolor
vscreen.text[y].__index=dtext
vscreen.bcolor[y].__index=dbcolor
setmetatable(vscreen.tcolor[y],vscreen.tcolor[y])
setmetatable(vscreen.text[y],vscreen.text[y])
setmetatable(vscreen.bcolor[y],vscreen.bcolor[y])
end

screen.write=function(_sText)
local x,y=term.getCursorPos()
for i=1,_sText:len() do
vscreen.text[y][x+i-1]=_sText:sub(i,i)
vscreen.tcolor[y][x+i-1]=color.text
vscreen.bcolor[y][x+i-1]=color.background
end
term.write(_sText)
end

screen.setTextColor=function(_nColor)
color.text=_nColor
term.setTextColor(_nColor)
end

screen.setTextColour=function(_nColor)
color.text=_nColor
term.setTextColour(_nColor)
end

screen.setBackgroundColor=function(_nColor)
color.background=_nColor
term.setBackgroundColor(_nColor)
end

screen.setBackgroundColour=function(_nColor)
color.background=_nColor
term.setBackgroundColour(_nColor)
end

screen.clear=function()
for y=1,h do
for x=1,w do
vscreen.text[y][x]=" "
vscreen.tcolor[y][x]=colors.text
vscreen.bcolor[y][x]=color.background
end
end
term.clear()
end

screen.clearLine=function()
local x,y=term.getCursorPos()
for x=1,w do
vscreen.text[y][x]=" "
vscreen.tcolor[y][x]=color.text
vscreen.bcolor[y][x]=color.background
end
term.clearLine()
end

screen.setCursorBlink=function(_bBlink)
blink=_bBlink
term.setCursorBlink(_bBlink)
end

screen.scroll=function(n)
if n > 0 then
for i=1,n do
for y=1,h do
for x=1,w do
vscreen.tcolor[y][x]=vscreen.tcolor[y+1][x]
vscreen.bcolor[y][x]=vscreen.bcolor[y+1][x]
vscreen.text[y][x]=vscreen.text[y+1][x]
end
end
end
else
for i=n,-1 do
for y=h,1,-1 do
for x=1,w do
vscreen.tcolor[y][x]=vscreen.tcolor[y-1][x]
vscreen.bcolor[y][x]=vscreen.bcolor[y-1][x]
vscreen.text[y][x]=vscreen.text[y-1][x]
end
end
end
end
term.scroll(n)
end

function get()
return screen
end

function takeScreenshot(_sPath)
local xp,yp=term.getCursorPos()
local file=fs.open(_sPath,"w")
file.write("{\\rtf1{\\fonttbl{\\f0\\fnil\\fcharset0 Courier New;}}{\\colortbl;\\red240\\green240\\blue240;\\red235\\green136\\blue68;\\red195\\green84\\blue205;\\red102\\green137\\blue211;\\red222\\green222\\blue108;\\red65\\green205\\blue52;\\red216\\green129\\blue152;\\red67\\green67\\blue67;\\red153\\green153\\blue153;\\red40\\green118\\blue151;\\red123\\green47\\blue190;\\red37\\green49\\blue146;\\red81\\green48\\blue26;\\red59\\green81\\blue26;\\red179\\green49\\blue44;\\red30\\green27\\blue27;\\red0\\green0\\blue0;}\\sa0")
local bcolor
for y=1,h do
for x=1,w do
bcolor=color[vscreen.bcolor[y][x]]
if bcolor=="16" then
bcolor="17"
end
if y==yp and x==xp and blink then
if vscreen.text[y][x]==" " then
file.write("\\cf"..color[vscreen.tcolor[y][x]].."\\highlight"..bcolor.." _")
else
file.write("\\cf"..color[vscreen.tcolor[y][x]].."\\ul\\highlight"..bcolor.." "..vscreen.text[y][x].."\\ulnone")
end
else
file.write("\\cf"..color[vscreen.tcolor[y][x]].."\\highlight"..bcolor.." "..vscreen.text[y][x])
end
end
file.write("\\highlight0\\par")
end
file.write("}")
file.close()
end
Creeper9207 #2
Posted 17 May 2015 - 04:46 PM
Also, Can someone PLEASE give me some kind of walkthrough on how taking a cc screenshot would work? :(/>
flaghacker #3
Posted 17 May 2015 - 05:16 PM
1)
term.native is a function, not a table. To receive the table you want, you must call that function:

term = term.native ()
The error message explains this too, you're "attempting to index (use as a table) a function"

2)
Overwrite all of term's functions to write to a buffer while still writing to the screen. When you want to take a screenshot, simply read the buffer.
Edited on 17 May 2015 - 03:18 PM
Creeper9207 #4
Posted 17 May 2015 - 05:21 PM
1) thanks!
2) i have no idea how to do that, please explain
Edited on 17 May 2015 - 03:24 PM
flaghacker #5
Posted 17 May 2015 - 05:34 PM
1) np
2) I would like to write an explenation here, but I'm on my phone right now and typing goes sloooow… Maybe you can take a look at this program to get an idea about how it's done?
Lyqyd #6
Posted 17 May 2015 - 05:43 PM
It is important to note that term.native was a table until the version that added term.current() and changed how terminal redirecting worked.
flaghacker #7
Posted 17 May 2015 - 06:48 PM
It is important to note that term.native was a table until the version that added term.current() and changed how terminal redirecting worked.

I figured it was enough to link him to the wiki page, thanks for the clarification though.
Creeper9207 #8
Posted 17 May 2015 - 06:57 PM
thanks for the effort, but that program's pastebin was 404
Creeper9207 #9
Posted 17 May 2015 - 08:27 PM
SOLVED thank you for the help