Jump to content




[QUESTION] defining a table through a function parameter


  • You cannot reply to this topic
18 replies to this topic

#1 crackroach

  • Members
  • 62 posts
  • LocationSomewhere over the rainbow

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:
Spoiler


#2 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

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?


#3 crackroach

  • Members
  • 62 posts
  • LocationSomewhere over the rainbow

Posted 07 December 2012 - 07:58 AM

View PostremiX, on 07 December 2012 - 06:56 AM, said:

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.

#4 ChunLing

  • Members
  • 2,027 posts

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 by ChunLing, 07 December 2012 - 08:15 AM.


#5 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 07 December 2012 - 08:05 AM

Ninja'd by ChunLing.
Spoiler


#6 crackroach

  • Members
  • 62 posts
  • LocationSomewhere over the rainbow

Posted 07 December 2012 - 08:17 AM

View PostBubba, on 07 December 2012 - 08:05 AM, said:

Ninja'd by ChunLing.
Spoiler

do i have to put the bracket as a parameter like so : [1] ?

#7 ChunLing

  • Members
  • 2,027 posts

Posted 07 December 2012 - 09:10 AM

no.

#8 crackroach

  • Members
  • 62 posts
  • LocationSomewhere over the rainbow

Posted 07 December 2012 - 09:22 AM

View PostChunLing, on 07 December 2012 - 09:10 AM, said:

no.
I thought so :P

#9 ChaddJackson12

  • Members
  • 264 posts

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"

#10 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 07 December 2012 - 04:17 PM

View PostChaddJackson12, on 07 December 2012 - 03:03 PM, said:

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.

#11 crackroach

  • Members
  • 62 posts
  • LocationSomewhere over the rainbow

Posted 08 December 2012 - 04:27 AM

View PostBubba, on 07 December 2012 - 04:17 PM, said:

View PostChaddJackson12, on 07 December 2012 - 03:03 PM, said:

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:

Spoiler
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)

#12 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 08 December 2012 - 04:30 AM

os.loadAPI() is better for that sort of thing than dofile().

#13 crackroach

  • Members
  • 62 posts
  • LocationSomewhere over the rainbow

Posted 08 December 2012 - 05:06 AM

View PostLyqyd, on 08 December 2012 - 04:30 AM, said:

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?

#14 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

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.

#15 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 08 December 2012 - 07:28 AM

View Postcrackroach, on 08 December 2012 - 04:27 AM, said:

View PostBubba, on 07 December 2012 - 04:17 PM, said:

View PostChaddJackson12, on 07 December 2012 - 03:03 PM, said:

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:

Spoiler
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.

#16 crackroach

  • Members
  • 62 posts
  • LocationSomewhere over the rainbow

Posted 08 December 2012 - 10:16 AM

View PostBubba, on 08 December 2012 - 07:28 AM, said:


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:

#17 crackroach

  • Members
  • 62 posts
  • LocationSomewhere over the rainbow

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?

#18 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

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:" )


#19 crackroach

  • Members
  • 62 posts
  • LocationSomewhere over the rainbow

Posted 10 December 2012 - 09:55 AM

View PostKaoS, on 08 December 2012 - 11:22 PM, said:

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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users