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

How do you call a function?

Started by bharhal, 21 February 2016 - 05:39 PM
bharhal #1
Posted 21 February 2016 - 06:39 PM
so im writing a menu for an apartment building in my city world and im trying to call functions but i dont know how? this is my code btw

function MainMenu()
term.clear()
term.setCursorPos(1,1)
print("+——–CraftCity Apartment's———–+")
print("| Who Are You? |")
print("| |")
print("| Owner Or Customer? |")
print("| |")
print("| Reply by typing owner or customer |")
print("+—————————————-+")
write("Answer: ")
answer = read("*")

if answer == "owner" then
call(OwnerAccess) –this calling isnt working
else
call(CustomerMenu) –this calling isnt working
end

function OwnerAccess()
term.clear()
term.setCursorPos(1,1)
print("+——–CraftCity Apartment's———–+")
print("|To Access Owner Files Please type the |")
print("| Correct Password |")
print("+—————————————-+")
print("Status: Unknown")
OwnerPass = read("*")
if OwnerPass == "test" then
– i need to call functions
end

function CustomerMenu()
term.clear()
term.setCursorPos(1,1)
print("+——–CraftCity Apartment's———–+")
print("| Customer Options |")
print("| room 1 |")
print("| room 2 |")
print("| room 3 |")
print("| Reply by typing owner or customer |")
print("+—————————————-+")
write("rooms: ")
end

function OwnerPassCorrect()
term.clear()
term.setCursorPos(1,1)
print("+——–CraftCity Apartment's———–+")
print("| Password Correct |")
print("| Welcome Bharhal |")
print("+—————————————-+")
print("Status: Owner")
sleep(5)

function OwnerOptions()
term.clear()
term.setCursorPos(1,1)
print("+——–CraftCity Apartment's———–+")
print("| Options: |")
print("| Edit Code |")
print("+—————————————-+")
print("Status: Owner")
write("Owner: ")
OwnerPass = read("*")

if EditPass == "TEST" then
term.clear()

end
end
end
end
KingofGamesYami #2
Posted 21 February 2016 - 08:33 PM
To call a function, type the name followed by parenthesis.
aFunction()
bharhal #3
Posted 21 February 2016 - 08:42 PM
To call a function, type the name followed by parenthesis.
aFunction()
ive done this but still have an error saying attempt to call nill :(/>
KingofGamesYami #4
Posted 21 February 2016 - 09:04 PM
ive done this but still have an error saying attempt to call nill :(/>

That'd be because your function isn't defined (yet) when you tried to call it. Since code executes top to bottom, you should place function declarations at the top.
bharhal #5
Posted 21 February 2016 - 09:10 PM
ive done this but still have an error saying attempt to call nill :(/>

That'd be because your function isn't defined (yet) when you tried to call it. Since code executes top to bottom, you should place function declarations at the top.
can i have an example of a decleration?
Lemmmy #6
Posted 21 February 2016 - 09:33 PM
I have modified your code slightly. First of all, I have moved all functions outside of MainMenu and moved them before it, so that you can call them. Previously in this thread it was mentioned that the code is called top to bottom. This means that you cannot call a function before you declare it.

As well as this, I have made your functions and variables local. Finally, I added a call to MainMenu() to start with.

As a tip, you can place code in the forums by putting [code] before your code, and [/code] after it.


local function OwnerAccess()
	term.clear()
	term.setCursorPos(1,1)

	print("+--------CraftCity Apartment's-----------+")
	print("|To Access Owner Files Please type the |")
	print("| Correct Password |")
	print("+----------------------------------------+")
	print("Status: Unknown")

	local OwnerPass = read("*")

	if OwnerPass == "test" then

	end
end

local function CustomerMenu()
	term.clear()
	term.setCursorPos(1,1)

	print("+--------CraftCity Apartment's-----------+")
	print("| Customer Options |")
	print("| room 1 |")
	print("| room 2 |")
	print("| room 3 |")
	print("| Reply by typing owner or customer |")
	print("+----------------------------------------+")

	write("rooms: ")
end

local function OwnerPassCorrect()
	term.clear()
	term.setCursorPos(1,1)

	print("+--------CraftCity Apartment's-----------+")
	print("| Password Correct |")
	print("| Welcome Bharhal |")
	print("+----------------------------------------+")
	print("Status: Owner")

	sleep(5)
end

local function OwnerOptions()
	term.clear()
	term.setCursorPos(1,1)

	print("+--------CraftCity Apartment's-----------+")
	print("| Options: |")
	print("| Edit Code |")
	print("+----------------------------------------+")
	print("Status: Owner")

	write("Owner: ")

	local OwnerPass = read("*") --# do something with OwnerPass?
end

local function MainMenu()
	term.clear()
	term.setCursorPos(1,1)

	print("+--------CraftCity Apartment's-----------+")
	print("| Who Are You? |")
	print("| |")
	print("| Owner Or Customer? |")
	print("| |")
	print("| Reply by typing owner or customer |")
	print("+----------------------------------------+")

	write("Answer: ")

	local answer = read("*")

	if answer == "owner" then
		OwnerAccess()
	else
		CustomerMenu()
	end

	if EditPass == "TEST" then --# where is EditPass defined?
		term.clear()
	end
end

MainMenu()

Because you didn't do this before, the extra spaces in your code were removed. You will have to fix this.
Edited on 21 February 2016 - 08:37 PM
Dog #7
Posted 21 February 2016 - 11:59 PM
can i have an example of a decleration?

A forward declaration simply establishes a variable for use before actually using it. The way functions work in Lua is that you assign a variable (the name) and that variable holds a pointer to the actual function in memory (iirc).

A simple forward declaration would look like this…

local myFunction --# forward declaration - myFunction is now local

--# code here (you can actually call myFunction from here, since it's already declared)

myFunction = function() --# assigning myFunction to point to a function - myFunction is already localized due to our forward declaration
  --# do stuff
end

--# more code (you can also call myFunction from here since it is 'above' this code)
Edited on 21 February 2016 - 11:02 PM
KingofGamesYami #8
Posted 22 February 2016 - 12:14 AM
can i have an example of a decleration?

Dog showed you an example of forward declaration, which is useful when you have two functions that need to "see" each other. Most of the time, that isn't necessary.


--#one way to declare a function
local function myFunction()

end
--#another way
local myFunction = function()

end
Edited on 21 February 2016 - 11:15 PM