2 posts
Posted 27 March 2013 - 04:21 PM
I can't seem to get my programs to accept arguments. I wrote a program to build a series of square frames, and it works fine, but I have to manually edit the file each time I want a different size or different number of frames. So far I've tried the following formats:
tArgs = {...}
tArgs = ...
scale, repeat = {...}.
and
scale, repeat = ...
But every time I get this error
bios:338: [string "frame.txt"]:1: unexpected symbol
Am I missing something with the syntax? I have it on the first line of my program before any functions. I can post the full program if it's relevant.
7508 posts
Location
Australia
Posted 27 March 2013 - 04:26 PM
I don't see any problems with it and use that method heaps. only problem is the fullstop/period at the end of your scale/repeat line but I'm assuming that is a typo here. post up the code you are using it in and I'll check it out on my system.
2 posts
Posted 27 March 2013 - 04:39 PM
Full code here. Sorry the indents are a little out of whack, for some reason they don't all seem to copy/paste properly. Just threw in the comments now, so if they're formatted wrong that's not the problem. Also, is tArgs supposed to be declared local? I've tried it both ways with the same error.
tArgs = {...}
--Select slot with material
local function selectSlot()
local full = false
local slot = 1
while not full do
if turtle.getItemCount(slot) > 0 then
turtle.select(slot)
full = true
end
slot = slot + 1
end
end
--Build hollow frame of dimension size x size
local function buildFrame(size)
selectSlot()
local x = 0
local z = 0
turtle.up()
z = z + 1
while x < size do
selectSlot()
turtle.placeDown()
turtle.forward()
x = x + 1
end
turtle.turnRight()
turtle.turnRight()
while x > 0 do
turtle.forward()
x = x - 1
end
turtle.turnRight()
turtle.turnRight()
while z < size do
selectSlot()
turtle.up()
turtle.placeDown()
z = z + 1
end
while x < (size - 1) do
selectSlot()
turtle.forward()
turtle.placeDown()
x = x + 1
end
turtle.forward()
while z > 0 do
turtle.down()
z = z - 1
end
end
--Execution
for i = 1, tArgs[1] do
buildFrame(tArgs[0])
end
7508 posts
Location
Australia
Posted 27 March 2013 - 04:59 PM
To have the indenting correct set your editor to use spaces when you press tab. IP.board preserves them, it just makes every 4 spaces a tab :/
Yes you can and should declare tArgs local. you should actually be declaring everything local unless you do want another program to be able to access the function or variable.
The code works fine for me. Try rebooting the computer. Noteworthy fixes to do tho:
- Line 60, convert the tArgs[1] to a number using tonumber
- Line 61, tArgs[0] doesn't exist. Tables in Lua start at 1 and go up from there.
- Add some argument checking to tell the user they have used your program wrong, instead of it just erroring.