7 replies to this topic
#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
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
#2
Posted 21 February 2016 - 08:33 PM
To call a function, type the name followed by parenthesis.
aFunction()
#4
Posted 21 February 2016 - 09:04 PM
bharhal, on 21 February 2016 - 08:42 PM, said:
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.
#5
#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.
Because you didn't do this before, the extra spaces in your code were removed. You will have to fix this.
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 by Lemmmy, 21 February 2016 - 09:37 PM.
#7
Posted 21 February 2016 - 11:59 PM
bharhal, on 21 February 2016 - 09:10 PM, said:
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 by Dog, 22 February 2016 - 12:02 AM.
#8
Posted 22 February 2016 - 12:14 AM
bharhal, on 21 February 2016 - 09:10 PM, said:
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 by KingofGamesYami, 22 February 2016 - 12:15 AM.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











