2

If I do something wrong with the formatting or anything else, sorry, I don't use this site often.

I'm trying to make a function in lua that with take a name I give it, and create a subtable with that name, and when I tried something like this it just made the absolute name I put in the function's code.

NewSubtable =
function(SubtableName)
    Table.SubtableName = {} --Creates a subtable called SubtableName
end

How can I make this create a subtable that is called by the name I give in the function when I use it? Is there an indicator or something to let the code know not to use the name given, but to use the variable assigned when I use the function?

EDIT: So whenever I try this, I get the the result "table index is nil" and it points to the error on line 4

I went and tested this but with a different input type, and it was just my fault. I didn't think that strings would the the value type you'd need for what I'm doing. My problem is solved.

Complete code:

Items = {}

NewWeapon = function(id, name, desc, minDMG, maxDMG)
    Items[id] = {}
    Items[id].Name = name
    Items[id].Desc = desc
    Items[id].MinDMG = minDMG
    Items[id].MaxDMG = maxDMG
end

NewWeapon(Test, "test", "test", 1, 1)
1
  • 2
    Table[SubtableName] = {} Commented Jan 1, 2017 at 21:08

1 Answer 1

5

Table.SubtableName is actually a syntactic sugar for Table['SubtableName']. To use the contents of variable SubtableName use the idom Table[SubtableName].

Sign up to request clarification or add additional context in comments.

6 Comments

Welcome! Enclose text in backticks to show it as code.
Thanks for that tip. I already wondered how I can mark code! And a good 2017 to you!
Whenever I try to make a table with that way of typing it, I get the error "table index is nil" and I have no clue how to fix it.
Please show a xcomplete sample code that gives you that nil message, I am sure we can figure out what's going on.
Alright, it's not much code so it shouldn't be a problem. I really don't know much, just some simple stuff and since I've been stuck on this I haven't gotten far.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.