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

Can You Call A Function With A Variable?

Started by randomsteve95370, 21 October 2013 - 12:21 PM
randomsteve95370 #1
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:


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.
LBPHacker #2
Posted 21 October 2013 - 02:40 PM
If those so called "commands" refer to the function in the program, this will do the trick:
getfenv()[command1]()
That gets the current environment of the program, which, of course, contains these functions, and then calls the functions in it which is named command1.
randomsteve95370 #3
Posted 21 October 2013 - 02:54 PM
That did the trick. I know it had to be something to do with the getfenv command but I had no idea of what the syntax would be. Thank you!
LBPHacker #4
Posted 21 October 2013 - 03:00 PM
Just be sure to not try to call undeclared functions - or use pcall
Engineer #5
Posted 21 October 2013 - 03:47 PM
Just be sure to not try to call undeclared functions - or use pcall
I want to add to that, that this only will work as long as the variables are global. When you use local, this is not a realiable way of doing this. I would recommend doing something like this:

Define all your functions in a table:

local allFunctions = {
  x = function()
    print( "this is a function" )
  end,
  etc = function()
    print( "etc." )
  end
}

local command = read()
if allFunctions[ command ] then
   allFunctions[ command ]()
end
Lyqyd #6
Posted 21 October 2013 - 05:00 PM
Yeah, the first place to go with this sort of thing is putting all of the relevant functions in a local table. This allows you to check validity and makes it easy to keep track of what the input can and can't call. Putting the functions in a local table should definitely be tried before jumping straight to getfenv().