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

syntax bug needing a little help

Started by kain184, 17 March 2015 - 10:45 PM
kain184 #1
Posted 17 March 2015 - 11:45 PM
hello all trying to make a drive info program for a monitor on spacial io i am trying to make it auto create the buttons using a modified version of direwolf20's button api i am getting an error and having a problem finding how to fix it. the main issue is that i am getting a nil error from a table i set up

--init and load-------------------
os.loadAPI("button")
c=peripheral.wrap("top")
invintory={}
direct="west"
local Rname=_
active={}
mon=peripheral.find("monitor")
xendv,yendv=mon.getSize()
print(mon.getSize())
print(xendv..yendv)
size={}
size={xmin=1,xmax = 10,ymin = 1,ymax = 3,xend = xendv,yend = yendv , xval = size.xmax - size.xmin , yval= size.ymax - size.ymin} -- error is here in the xvalues it keeps giving me nil for any of the values after in the outputs even tho i have defined them
print(size.xmin)
   print("monitor size is "..xendv .." "..yendv)
   print(size.yend .. size.xend)
------------------------------
function Materialize()
end
function Dematerialiaze()
end
------------------------------
function Stack(Rname,direct)
v=c.getInventorySize()
  for i=1,v  do
  print("checking slot"..i)
  sleep()
	    cond= c.getStackInSlot(i)
   if cond then
    stack = cond.display_name
    print("stackname is ".. stack)
	 table.insert(invintory,i,stack)
   
	 print("item in slot "..i.."is ".. invintory[i])
   else
   end
  end
   for i=1,#invintory do
   
	 if invintory[i] == Rname then
	  print("requested area is "..Rname)
	  print("moving required area")
	  c.pushItem(direct,i)
	 end
	  
	    for i=1,v do
	   
		 if cond then
		  stack =cond.display_name
		  print("checking if required area is still in slot "..i)
		   if stack == Rname then print("true") print("attempting to move area")
		    for i=1,#invintory do
		   
			  if invintory[i] == Rname then
			   print("requested area is "..Rname)
			   print("moving required area")
			   c.PushItem(tostring(direct),i)
			  end
		    end
		   end
		 end
	    end
   end
end
-------------------
function createButton()
local i = 1
v=c.getInventorySize()
print("enter")
while i<=v do
  cond=c.getStackInSlot(i)
  if cond then
    print(cond.display_name)
    print("enter")
    button.setTable(cond.display_name,Stack,tostring(cond.display_name),direct)
 
   print("xmin is "..size.xmin)
   if not button.checkLoc(size.xval,size.yval) then button.setLoc(cond.display_name,size.xmin,size.xmax,size.ymin,size.ymax)
    button.screen()
   else
    print("enter")
    while button.checkLoc(size.xval,size.yval) do
	  
	   size.xmin=size.xmin+10 size.xmax=size.xmax+10 if size.xmax >= xend then size.ymin=size.ymin+5 size.ymax=size.ymax+5 size.xmin=1 size.xmax=10 end
	    if not button.checkLoc(xval,yval) then button.setLoc(cond.display_name,size.xmin,size.xmax,size.ymin,size.ymax) button.screen() end
	  
	  
	
	 print("exit")
    end
    i=1+1
   end

 
  end
   i=i+1
  
end

end
------------------
function getClick()
   event,side,x,y = os.pullEvent("monitor_touch")
   button.checkxy(x,y)
  
end
----------------
mon.clear()
createButton()
--getClick()
KingofGamesYami #2
Posted 17 March 2015 - 11:48 PM
Please post the full error - I'm not going to dig through your code for something that looks wrong. The error should include at least a line number.
Dragon53535 #3
Posted 17 March 2015 - 11:55 PM
You cannot use a value not yet created.

size={xmin=1,xmax = 10,ymin = 1,ymax = 3,xend = xendv,yend = yendv , xval = size.xmax - size.xmin , yval= size.ymax - size.ymin}
size.xmin, size,ymin, ymax xmax whatever doesn't technically exist until the table is done being loaded.

Try doing this

size={xmin=1,xmax = 10,ymin = 1,ymax = 3,xend = xendv,yend = yendv }
size["xval"] = size.xmax - size.xmin
size["yval"] = size.ymax - size.ymin
kain184 #4
Posted 18 March 2015 - 12:06 AM
thank you worked like a charm. i thought it was something like that but was trying every line but those two. lol now if i can figure out why the button sequince is not moving ill work on it and get back to you guys
kain184 #5
Posted 18 March 2015 - 12:19 AM
i have a question for you guys. would this bit of code

function checkLoc(x,y)

   for name,data in pairs(button) do
   print("name is "..name.."y is  "..y.." x is "..x)
	 if not data["ymin"] then print("data "..data["ymin"]) return false end
		  if y>=data["ymin"] and  y <= data["ymax"] then
				 if x>=data["xmin"] and x<= data["xmax"] then
						return true
						--data["active"] = not data["active"]
						--print(name)
				 end
		  end
   end
   return false
end
run through every name in the table button and check the data to see if there was a button there and if not what code would you reccomend to get that result.
Edited on 17 March 2015 - 11:20 PM