Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing array as function parameter in Lua?

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);
like image 248
Lemony Lime Avatar asked Jan 02 '13 08:01

Lemony Lime


People also ask

Can we pass array as a parameter of function?

A whole array cannot be passed as an argument to a function in C++. You can, however, pass a pointer to an array without an index by specifying the array's name. In C, when we pass an array to a function say fun(), it is always treated as a pointer by fun().

Can you pass by reference in Lua?

Lua's function , table , userdata and thread (coroutine) types are passed by reference. The other types are passed by value.

How do parameters work in Lua?

When a function is invoked, you pass a value to the argument. This value is referred to as the actual parameter or argument. The parameter list refers to the type, order, and number of the arguments of a method. Arguments are optional; that is, a method may contain no argument.

Does Lua have array?

In Lua, arrays are implemented using indexing tables with integers. The size of an array is not fixed and it can grow based on our requirements, subject to memory constraints.


3 Answers

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.

like image 198
Kornel Kisielewicz Avatar answered Sep 25 '22 20:09

Kornel Kisielewicz


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.

like image 32
mkluwe Avatar answered Sep 25 '22 20:09

mkluwe


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!

like image 23
SelectricSimian Avatar answered Sep 24 '22 20:09

SelectricSimian