Jump to content




Newb with tables help.


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

#1 sirdabalot

  • Members
  • 115 posts

Posted 29 September 2012 - 03:56 PM

Spoiler

Right so, I'm trying to make a simple GUI and am bad at using tables, my error is on line 43 (menus[curmenu].drawfunc()) and it says attempt call nill... I realise I'm probably choosing the wrong key or something but the problem is I don't know the syntax for calling stuff from tables no matter how many tutorials I read. So what's the syntax in this particular case?

#2 Kolpa

  • New Members
  • 260 posts
  • LocationGermany

Posted 29 September 2012 - 04:53 PM

View Postsirdabalot, on 29 September 2012 - 03:56 PM, said:

Spoiler

Right so, I'm trying to make a simple GUI and am bad at using tables, my error is on line 43 (menus[curmenu].drawfunc()) and it says attempt to call nill. I realise I'm probably choosing the wrong key or something but the problem is I don't know the syntax for calling stuff from tables no matter how many tutorials I read. So whats the syntax in this particular case?

i can only state the obvious because i never used stacking tables but if you want to call an string you have to put the "" aroud it like this write(menus["main"].options[i])

#3 Kolpa

  • New Members
  • 260 posts
  • LocationGermany

Posted 29 September 2012 - 04:58 PM

View PostKolpa, on 29 September 2012 - 04:53 PM, said:

View Postsirdabalot, on 29 September 2012 - 03:56 PM, said:

Spoiler

Right so, I'm trying to make a simple GUI and am bad at using tables, my error is on line 43 (menus[curmenu].drawfunc()) and it says attempt to call nill. I realise I'm probably choosing the wrong key or something but the problem is I don't know the syntax for calling stuff from tables no matter how many tutorials I read. So whats the syntax in this particular case?

i can only state the obvious because i never used stacking tables but if you want to call an string you have to put the "" aroud it like this write(menus["main"].options[i])

wait nevermind this should be the real error:
term.clear()
term.setCursorPos(1,1)
curmenu = "main"
menus = {
  main = {
	options = {
	"Numbers",
	"Letters",
	"Quit",
	},
  drawfunc = drawMain
},
numbers = {
  options = {
  "1",
  "2",
  "3",
   },
},
letters = {
  options = {
  "A",
  "B",
  "C",
   },
}
}
-- Main Program
while true do
menus.drawfunc() --drawfunc is not in the main table
end


#4 sirdabalot

  • Members
  • 115 posts

Posted 29 September 2012 - 05:09 PM

Oh, well I derped big time. :)/>

EDIT: Wait, are you sure it's not in the main table? According to notepad++ it is.

#5 Kolpa

  • New Members
  • 260 posts
  • LocationGermany

Posted 29 September 2012 - 06:35 PM

in the code that i posted (which i copied) it isn't but there are more } than there are { so i guess that's an copy error

then try
menus[1].drawfunc()

and 1. of all try declaring the function before you rename it :)/>

#6 sirdabalot

  • Members
  • 115 posts

Posted 29 September 2012 - 07:31 PM

View PostKolpa, on 29 September 2012 - 06:35 PM, said:

in the code that i posted (which i copied) it isn't but there are more } than there are { so i guess that's an copy error

then try
menus[1].drawfunc()

and 1. of all try declaring the function before you rename it :)/>

After a bit of testing I found that functions probably don't need to be placed in a different order, but anyway if I put the function above the table it gives me an error at write(menus[main].options[i]). Furthermore if i try menus[1].drawfunc() it gives me attempt to index ? a nill value.

BTW on the opening post I said it gives me attempts to call nill error, it was actually attempt to index ? a nill value.

EDIT: I lied, by changing it to menus[1].drawfunc() it gave me attempt to index ? a nill value

#7 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 29 September 2012 - 08:38 PM

You need to define the function before assigning it to another variable. Also, there's an error in the drawMain function:
write(menus[main].options[i])
-- should be:
write(menus["main"].options[i])
-- or
write(menus.main.options[i])


#8 sirdabalot

  • Members
  • 115 posts

Posted 29 September 2012 - 08:59 PM

View PostMysticT, on 29 September 2012 - 08:38 PM, said:

You need to define the function before assigning it to another variable. Also, there's an error in the drawMain function:
write(menus[main].options[i])
-- should be:
write(menus["main"].options[i])
-- or
write(menus.main.options[i])

Okay, so let me clear one last thing up. When do I use a ., when do I use [] and when do enclose in quotes?

#9 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 29 September 2012 - 09:51 PM

This are the same:
t["key"]
t.key
It will get the value stored on the table with the string "key" as key.
This:
t[key]
will get the value in the table with the content of the variable "key" as key. So, if the variable key contains the number 2, it would be like doing
t[2]


#10 sirdabalot

  • Members
  • 115 posts

Posted 29 September 2012 - 10:05 PM

View PostMysticT, on 29 September 2012 - 09:51 PM, said:

This are the same:
t["key"]
t.key
It will get the value stored on the table with the string "key" as key.
This:
t[key]
will get the value in the table with the content of the variable "key" as key. So, if the variable key contains the number 2, it would be like doing
t[2]

And it works with numbers too? So if I had table t and had a value at index 3 I could use:
t.3

As well as:
t[3]


#11 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 29 September 2012 - 10:12 PM

No, it only works with strings.

#12 sirdabalot

  • Members
  • 115 posts

Posted 30 September 2012 - 08:09 AM

View PostMysticT, on 29 September 2012 - 10:12 PM, said:

No, it only works with strings.

Okay, thanks for your time. :)/>





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users