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

can't call a function.

Started by phil872001, 19 June 2013 - 06:29 AM
phil872001 #1
Posted 19 June 2013 - 08:29 AM
had been playing with FORTH for some time, moved over to LUA not long ago and i like it much better. one probelm i have though which is holding me back is the use of functions.

could someone tell me what i am doing wrong with this code? the varable is correctly set with the key press (i confirmed this with a "print (var1)" but the function defined for the key is not executed.

i guess i am using this as a work around for not having a goto in LUA (which i know is bad programing), is the use of functions as "jump to points" bad form?
Thank you in advance for any input.

PS, i've tried executing the "If true then" section below the functions with no change in how it works.

sorry, couldn't figure out how to put it this into the code subwindow like i see in other threads, when i post this all my intdenting goes away..


if true then
  print "Waiting for input:"
  print "Press 1 or 2 then Enter"
  var1 = read()	 --   wait for input

if var1 == 1 then		--   if 1 is pressed run this
  test1()				   -- jump to function test1
end

if var1 == 2 then	   -- if 2 is pressed run this
  test2()				  -- jump to function test2
end

function test1()		  
  print "function 1 succussful"	   -- what to do if 1 is pressed
end

function test2()
  print "function 2 succussful"	  -- what to do if 2 is pressed
end

end
Edited by
Lyqyd #2
Posted 19 June 2013 - 01:33 PM
Split into new topic.

Move your functions to the top of the program, it's best to have the declarations above the first call. read() returns strings, so you need to tonumber() the result, or check if it's equal to "1" instead of 1. Functions are not a stand-in for goto.
phil872001 #3
Posted 19 June 2013 - 04:59 PM
very good, i will play around with this later tonight. thank you for reviewing this.
Xenon #4
Posted 20 June 2013 - 09:43 AM
Don't know if you already got it working, but fixed it for you anyways:


print "Waiting for input:"
print "Press 1 or 2 then Enter"
local var1 = read()
function test1()				 
print "Function 1 successful"
end
function test2()
print "Function 2 successful"
end
if var1 == "1" then
test1()
elseif var1 == "2" then
test2()
else
print "Invalid input"
end
dent308 #5
Posted 20 June 2013 - 01:00 PM
Not sure about within CC but I believe Lua requires functions be defined before you call them.


test1()
function test1()				 
  print "function 1 succussful"
end

Fails with "attempt to call global 'test1' (a nil value)"

Where this one works :


function test1()				 
  print "function 1 succussful"
end
test1()
ElvishJerricco #6
Posted 20 June 2013 - 02:36 PM
On top of needing to declare your functions first, please use locals. The CC shell doesn't sandbox programs at all, so if you don't declare a variable as

local x = 3
or

local function test()
    print("test")
end

then any other program that gets run can access the data or functions declared by your program, which can lead to a whole mess of problems.