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

[QUESTION] defining a table through a function parameter

Started by crackroach, 07 December 2012 - 05:48 AM
crackroach #1
Posted 07 December 2012 - 06:48 AM
Here's my problem, I'm trying to define what part of the table to use through the parameter.

That the problematic code:
SpoilerThat's the function i want to call with the parameter
IT IS OUTSIDE THE MAIN FILE

function layout.loginBox( topRow )
   local box = {"+-------------NEW------------+", "+------------LOGIN-----------+", "+----------DEPOSIT----------+", "+----------           WITHDRAW----------+"}

   term.setCursorPos(10, 6)
   print( box[topRow] )
   --write("+------------LOGIN-----------+")
   term.setCursorPos(10, 11)
   write("+----------------------------+")
   local x = 10
   local y = 7

   for i = 1, 4 do
      term.setCursorPos(x, y)
      write("|")
      y = y + 1
   end

   x = x + 29
   y = 7
   for i = 1, 4 do
      term.setCursorPos(x, y)
      write("|")
      y = y + 1
   end
end
That's the line of code that ask for this function



local function login()
   local program = "Login Screen"
   layout.mainScreen( program )
   layout.loginName()
   layout.loginPassword()
   layout.loginBox()
— the rest is useless for that question
remiX #2
Posted 07 December 2012 - 06:56 AM
You made the function need a parameter and you're trying to call it without a parameter?

local function login()
   local program = "Login Screen"
   layout.mainScreen( program )
   layout.loginName()
   layout.loginPassword()
   layout.loginBox() -- Where's your parameter when you call this?
crackroach #3
Posted 07 December 2012 - 07:58 AM
You made the function need a parameter and you're trying to call it without a parameter?

local function login()
   local program = "Login Screen"
   layout.mainScreen( program )
   layout.loginName()
   layout.loginPassword()
   layout.loginBox() -- Where's your parameter when you call this?

My bad, i posted my last try. It is still not working.
ChunLing #4
Posted 07 December 2012 - 08:04 AM
The function is from an API. The parameter toprow is the sequential numeric index of the table entry that you want displayed.

1 is NEW, 2 is LOGIN, 3 isDEPOSIT, 4 is WITHDRAW

You must provide a numeric argument to layout.loginBox() or it will error (attempt to index by nil).

Achievement get NINJA!
Edited on 07 December 2012 - 07:15 AM
Bubba #5
Posted 07 December 2012 - 08:05 AM
Ninja'd by ChunLing.
Spoiler
You made the function need a parameter and you're trying to call it without a parameter?

local function login()
   local program = "Login Screen"
   layout.mainScreen( program )
   layout.loginName()
   layout.loginPassword()
   layout.loginBox() -- Where's your parameter when you call this?

My bad, i posted my last try. It is still not working.
The code still looks the same to me. You need to have something in the layout.loginBox( [param] ) function.
crackroach #6
Posted 07 December 2012 - 08:17 AM
Ninja'd by ChunLing.
Spoiler
You made the function need a parameter and you're trying to call it without a parameter?

local function login()
   local program = "Login Screen"
   layout.mainScreen( program )
   layout.loginName()
   layout.loginPassword()
   layout.loginBox() -- Where's your parameter when you call this?

My bad, i posted my last try. It is still not working.
The code still looks the same to me. You need to have something in the layout.loginBox( [param] ) function.

do i have to put the bracket as a parameter like so : [1] ?
ChunLing #7
Posted 07 December 2012 - 09:10 AM
no.
crackroach #8
Posted 07 December 2012 - 09:22 AM
no.
I thought so :P/>
ChaddJackson12 #9
Posted 07 December 2012 - 03:03 PM
I don't believe you can have a "." in your function name unless it is an API. So your filename should be "layout" and the function should be named "loginBox". So when you load the API, inside the main file you'd type: "layout.loginBox"
Bubba #10
Posted 07 December 2012 - 04:17 PM
I don't believe you can have a "." in your function name unless it is an API. So your filename should be "layout" and the function should be named "loginBox". So when you load the API, inside the main file you'd type: "layout.loginBox"

You can have a period in the function IF that function is contained inside of a table.
Here's an example:

local layout = {
loginBox = function()
print("This does stuff")
end
}
That's valid. Here's another valid one.


local layout = {}
local function layout.loginBox()
print("This works too")
end

Here's an invalid example:

--No table is created at the startup
local function layout.loginBox()
print("Oh noes! This doesn't work!")
end

Of course, as Chadd said you can also just have the code in an in API named "layout", but an API is basically just a table with the entire file loaded into that table. They work similarly to the environment variable (_G) if you're familiar with that concept.
crackroach #11
Posted 08 December 2012 - 04:27 AM
I don't believe you can have a "." in your function name unless it is an API. So your filename should be "layout" and the function should be named "loginBox". So when you load the API, inside the main file you'd type: "layout.loginBox"
You can have a period in the function IF that function is contained inside of a table.

My program works with multiple class(I know Lua don't have class, I was able to work like if it could), that way the main page can load 'layout.loginBox' and the 'login page' can also load 'layout.loginBox'. It is very simple, let me explain it to you: At the top of the program you can use the dofile(string) command, this command load a file outside the one you are doing the program and make it possible to create a table containing different function. The utility of doing that is the fact that you don't have to retype or paste/copy a function you want to use in different parts of your program. Let me show you an example:

Spoilerthe 'dofile'( file outside containing the function you want to use in multiple files)

layout = {} --important that its NOT local

function layout.loginBox()
   print("It worked")
end

Here is the login file

dofile("layoutHelper") -- you have to put the directory of the file you want to import

local function login()
   print(im login- in")
   layout.loginBox() -- you use it as any function
end

login()
Any question feel free to PM me or post it here. (By the way i'm not a pro, but i like to share knowledge)
Lyqyd #12
Posted 08 December 2012 - 04:30 AM
os.loadAPI() is better for that sort of thing than dofile().
crackroach #13
Posted 08 December 2012 - 05:06 AM
os.loadAPI() is better for that sort of thing than dofile().

Do you think it would be better to make an API that just make a file containing the function you want to use?
Lyqyd #14
Posted 08 December 2012 - 06:30 AM
Your question is semantically equivalent to, "Is an API better than an API?". I'm saying that the built-in functions for handling APIs are going to be of more utility when using files as APIs.
Bubba #15
Posted 08 December 2012 - 07:28 AM
I don't believe you can have a "." in your function name unless it is an API. So your filename should be "layout" and the function should be named "loginBox". So when you load the API, inside the main file you'd type: "layout.loginBox"
You can have a period in the function IF that function is contained inside of a table.

My program works with multiple class(I know Lua don't have class, I was able to work like if it could), that way the main page can load 'layout.loginBox' and the 'login page' can also load 'layout.loginBox'. It is very simple, let me explain it to you: At the top of the program you can use the dofile(string) command, this command load a file outside the one you are doing the program and make it possible to create a table containing different function. The utility of doing that is the fact that you don't have to retype or paste/copy a function you want to use in different parts of your program. Let me show you an example:

Spoilerthe 'dofile'( file outside containing the function you want to use in multiple files)

layout = {} --important that its NOT local

function layout.loginBox()
   print("It worked")
end

Here is the login file

dofile("layoutHelper") -- you have to put the directory of the file you want to import

local function login()
   print(im login- in")
   layout.loginBox() -- you use it as any function
end

login()
Any question feel free to PM me or post it here. (By the way i'm not a pro, but i like to share knowledge)

Heh you don't have to explain it to me. I understand what you are doing. I was just responding to ChaddJackson :)/>/>

In my opinion, os.loadAPI() is a better way to go about this than dofile(), but it's really just semantics.
crackroach #16
Posted 08 December 2012 - 10:16 AM
Heh you don't have to explain it to me. I understand what you are doing. I was just responding to ChaddJackson :)/>/>

In my opinion, os.loadAPI() is a better way to go about this than dofile(), but it's really just semantics.

No offence, I was trying to explain my point. :lol:/>
crackroach #17
Posted 08 December 2012 - 05:54 PM


function intern.makeBox( operation )
local box = {"+----------DEPOSIT----------+", "+----------WITHDRAW----------+", "+----------TRANSFER----------+"}

term.setCursorPos(17, 6)
write( box[operation] )
--write("+------------LOGIN-----------+")
term.setCursorPos(17, 11)
write("+----------------------------+")
local x = 17
local y = 7

for i = 1, 4 do
term.setCursorPos(x, y)
write("|")
y = y + 1
end

x = x + 29
y = 7
for i = 1, 4 do
term.setCursorPos(x, y)
write("|")
y = y + 1
end

end
I know, i've already come up with this but the issue persist and i have no clue. The error says bios:156: bad argument: string expected got nil…

Any idea?
KaoS #18
Posted 08 December 2012 - 11:22 PM
I think that is correct but you are just calling it wrong (not specifying a valid 'operation' var). this can be solved by adding a default

write( box[operation] or "no input D:" )
crackroach #19
Posted 10 December 2012 - 09:55 AM
I think that is correct but you are just calling it wrong (not specifying a valid 'operation' var). this can be solved by adding a default

write( box[operation] or "no input D:" )
I'll try it might work.