4 posts
Posted 10 February 2013 - 08:22 AM
I have code like this:
local Boxes = {"Box1","Box2"}
..unimportant code..
AllBoxes = function()
for i = 1,#Boxes do
-- I need help with this part
end
end;
..unimportant code..
In the Boxes table I have name of 2 tables (in strings)
Now in the AllBoxes function I need to get the data out of the tables but I have only the names of them.
I cant use anything table.concat("Box1",1,1).
I basically want something like: print("Box1".Width) to print the Width entry in the table named Box1.
1688 posts
Location
'MURICA
Posted 10 February 2013 - 08:35 AM
Just store the tables inside the tables?
local Boxes = {}
-- function to insert a box, also returns the new box
function newBox(x, y, width, height)
local box = {
x = x,
y = y,
width = width,
height = height
}
table.insert(Boxes, box)
return box
end
-- make some boxes
local box1 = newBox(2, 2, 4, 3)
local box2 = newBox(4, 4, 4, 3)
-- print the boxes widths
print(box1.width)
print(box2.width)
4 posts
Posted 10 February 2013 - 08:48 AM
I could but I want to do things like:
Box1.Delete() not Boxes.Box1.Delete()
I know its just how it looks but I would like to do it the style withs strings (somehow).
Of course if theres no way then I'll use the tables inside table.
1688 posts
Location
'MURICA
Posted 10 February 2013 - 08:51 AM
That's exactly what my method allows you to do. In the newBox function, you could just define a new function:
function newBox(x, y, width, height)
local box = {
x = x,
y = y,
width = width,
height = height,
delete = function()
-- do stuff
end
}
table.insert(Boxes, box)
return box
end
And because it returns the box, you can just store it in a variable, then use the delete function.
local box = newBox()
box.delete()
4 posts
Posted 10 February 2013 - 08:55 AM
Oh, you store it in both Boxes and Box1… I didnt think of it this way :D/>
That should work. Thanks a lot!
2088 posts
Location
South Africa
Posted 10 February 2013 - 09:01 AM
Printing this sort of thing just requires a little bit of math and trial and error.
You can do something like this
Spoiler
--[[
startX = the X position of where the box will start
startY = the Y position of where the box will start
width = the width of the box
height = the height of the box
borderCol = the colour of the outside of the box (border)
insideCol = the colour of the inside of the box
textCol = the colour of the text
--]]
Boxes = {
Box1 = {
startX = 10,
startY = 2,
width = 30,
height = 4,
borderCol = colours.grey,
insideCol = colours.lightGrey,
textCol = colours.white,
},
Box2 = {
startX = 10,
startY = 10,
width = 10,
height = 6,
borderCol = colours.grey,
insideCol = colours.lightGrey,
textCol = colours.white,
},
}
local function drawBoxes()
for i, data in pairs(Boxes) do
--[[ This part does the border of the box ]]--
term.setBackgroundColour(Boxes[i].borderCol)
-- Horizontal Lines
for x = 0, data.width - 1 do
term.setCursorPos(data.startX + x, data.startY)
write(" ")
term.setCursorPos(data.startX + x , data.startY + data.height + 1)
write(" ")
end
-- Vertical Lines
for y = 0, data.height - 1 do
term.setCursorPos(data.startX, data.startY + y + 1)
write(" ")
term.setCursorPos(data.startX + data.width - 1, data.startY + y + 1)
write(" ")
end
--[[ This part colours the insides of the boxes ]]--
term.setBackgroundColour(Boxes[i].insideCol)
for x = 0, data.width - 3 do
for y = 0, data.height - 1 do
term.setCursorPos(data.startX + x + 1, data.startY + y + 1)
write(' ')
end
end
end
end
term.setCursorPos(1, 1)
term.clear()
drawBoxes()
Alternatively, you can check out these two functions I made exactly for this over
here.