Posted 21 October 2013 - 02:21 PM
Im working on making a program which uses a bit of user interface in the form of the read() function. What I would like to know is if it is possible do so something like so:
inside of the commands function is where I have the issue. running the variable command2 as a function does seem to work, so my question is how can I convert it to something I can run as a function.
rednet.open("right")
miners = {}
loaders = {}
fuelBosses = {}
powers = {}
tArgs = {...}
-- findTurtles() sends a message of "checkIn"
-- to any turtle in the area, what is returned
-- determines what table is incremented.
function findTurtles()
local gotMsg = true
local id, msg, dist
rednet.broadcast("checkIn")
while gotMsg do
id,msg,dist = rednet.receive(1)
if msg == "Miner" then
print(id..":"..msg)
miners[#miners+1] = id
elseif msg == "chunkloader" then
print(id..":"..msg)
loaders[#loaders+1] = id
elseif msg == "fuelBoss" then
print(id..":"..msg)
fuelBosses[#fuelBosses+1] = id
miners[#miners+1] = id
elseif msg == "power" then
print(id..":"..msg)
powers[#powers+1] = id
elseif msg == "Done" then
else
print("Done")
gotMsg = false
end
end
end
-- Simple User Interface. Lets the user know what
-- commands are allowed.
function instructions()
print("You can run any of the following commands")
sleep(0.5)
print("forward, back, left, right, up, down")
sleep(0.5)
print("You can run those programs on these types of turtles")
sleep(0.5)
print("miners, powers, fuelbosses, boss")
print("Active turtles:")
end
-- allows the user to set what program to run
-- and who to tell to run it
-- sets the user input as variables used in the tell function
function commands()
print("what command should I run?")
command1 = read()
print("Who should I tell to run ",command1)
command2 = read()
if command2 == "boss" then
command1
-- this is the part I have trouble with, I want to be able to run
-- the variable of command1 as the proper function.
else
tell()
end
-- sends the variable command1 (defined in the script above)
-- off to the turtles in the table of command 2, also
-- defined above.
function tell()
for x,y in pairs(getfenv()[command2]) do
rednet.send(y,command1)
end
end
-- the following are functions designated to be
-- called with the Commands program
-- allows multiple turtles to be told to move
-- in a direction with 1 command. See the commands
-- program for more info
function forward()
turtle.forward()
end
function back()
turtle.turnLeft()
turtle.turnLeft()
turtle.forward()
turtle.turnLeft()
turtle.turnLeft()
end
function up()
turtle.up()
end
function left()
turtle.turnLeft()
end
function right()
turtle.turnRight()
end
-- this is what actually runs the above functions.
-- runs the instructions function, then the findTurtles function
-- then the commands function. All of which are defined above.
instructions()
findTurtles()
commands()
inside of the commands function is where I have the issue. running the variable command2 as a function does seem to work, so my question is how can I convert it to something I can run as a function.