5

I'm trying to alter the class example I found in this video to make it a bit more streamlined to use. Hopefully my comments explain what I'm trying to accomplish well enough. The problem I'm having is that when I try to use the data table it gives me this error: lua: class example.lua:7: attempt to index field 'data' (a nil value)

I'm assuming that this means that array isn't being properly passed into the function, but I don't know why. I am very much a beginner with Lua.

Here's what I've got:

local enemy = {}; --enemy class table

function enemy:New(data)
  local object = {}; --table to store all of data within class
  local len = # data --get length of passed table
  for i = 1, len, 2 do --loop to input all data from passed table into object table
    object.data[i] = data[i + 1];
  end

  function object:getData(choice) --function that allows us to retrieve data from the class
    return self[choice];
  end

  return object; --return class data table so we can create objects using the class
end

local monsterdata = {"name", "monster", "x", 64, "y", 128, "hp", 4}; --table containing data of monster. keys are odd numbered, values to those keys are even numbered
local monster = enemy:New(monsterdata); --create a object using the class

local test = monster:getData("x"); --set variable to a value with the getData function

print(test);

3 Answers 3

5

You didn't create the object.data table -- each table in Lua needs to be initialized:

local object = {}
local object.data = {}

or

local object = { data = {} }

However, your example will not work the intended way, unless you fix the getData function:

function object:getData(choice) 
  return self.data[choice]
end

Finally, this is Lua, so you don't need any ; in your code :P.

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

2 Comments

Haha, I know the ; isn't necessary, I just started programming with C, and my teacher was very strict about these things, so it's comforting to have them. :P
What I was doing to begin with works (minus the stupid mistake I made that mkluwe pointed out) with less code than this, and no need to save the data table a second time. Is there any reason I should do it this way over the other?
4

If you want object to hold the data, you probably meant to write

object[data[i]] = data[i + 1];

instead of

object.data[i] = data[i + 1];

Doing this the result printed is 64.

2 Comments

Ah, it was such a simple mistake... though I suppose that's usually the case.
Yes, and not an uncommon one. Typing t.a instead of t[ a ] happens to me from time to time.
2

Like the others said, object.data needs to be initialized, and there's a flaw in the for loop and getData. Also, while it's not a bug exactly, your system of passing keys as odd and values as even is a very good way to do it in a C-based language, with no associative-array/dictionary/table literals, but in Lua, the idiom is

{keyname = value, keyname = value, ...}

and, if the table spans multiple lines

{
    keyname = value;
    keyname = value;
    keyname = value;
    ...
}

So in your case, monsterdata could simply be

{
    name = "monster";
    x = 64;
    y = 128;
    hp = 4;
}

and you could remove the for loop altogether

note: you can only represent string keys this way. For other kinds of keys, like numbers, booleans, or even functions and other tables, surround the key in [square brackets]. For example, if you wanted a mynot table, to map booleans to their opposites, you could use:

{
    [true] = false;
    [false] = true;
}

or, if you wanted to map a set of functions to their libaries

{
    [print] = "standard";
    [os.execute] = "standard os";
    [math.sin] = "standard math";
    [function() print "a user function" end] = "me!";
}

I think the more you learn about Lua the more you'll like it. It's really a great language, with a lot of fun little features. Happy coding!

Comments

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.