在Lua中将数组作为函数参数传递?

时间:2021-04-22 21:46:16

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)

我正在尝试更改我在此视频中找到的类示例,以使其更加流线型使用。希望我的评论能够解释我想要达到的目标。我遇到的问题是,当我尝试使用数据表时,它给了我这个错误:lua:class example.lua:7:尝试索引字段'data'(零值)

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.

我假设这意味着数组没有正确传递给函数,但我不知道为什么。我是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 个解决方案

#1


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.

这样打印的结果是64。

#2


5  

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

您没有创建object.data表 - Lua中的每个表都需要初始化:

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

or

local object = { data = {} }

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

但是,除非修复getData函数,否则您的示例将无法按预期方式工作:

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

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

最后,这是Lua,所以你不需要任何东西;在你的代码中:P。

#3


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

像其他人说的那样,object.data需要初始化,并且for循环和getData存在缺陷。此外,虽然它不是一个完全错误的错误,但你的系统将键传递为奇数和值甚至是基于C语言的非常好的方法,没有关联数组/字典/表文字,但是在Lua中,这个成语是

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

{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

所以在你的情况下,monsterdata可能只是

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

and you could remove the for loop altogether

你可以完全删除for循环

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:

注意:您只能以这种方式表示字符串键。对于其他类型的键,如数字,布尔值,甚至函数和其他表,请在[方括号]中包围键。例如,如果你想要一个mynot表,要将布尔值映射到它们的对立面,你可以使用:

{
    [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!

我想你越了解Lua,你就会越喜欢它。它真的是一种很棒的语言,有很多有趣的小功能。快乐的编码!

#1


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.

这样打印的结果是64。

#2


5  

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

您没有创建object.data表 - Lua中的每个表都需要初始化:

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

or

local object = { data = {} }

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

但是,除非修复getData函数,否则您的示例将无法按预期方式工作:

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

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

最后,这是Lua,所以你不需要任何东西;在你的代码中:P。

#3


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

像其他人说的那样,object.data需要初始化,并且for循环和getData存在缺陷。此外,虽然它不是一个完全错误的错误,但你的系统将键传递为奇数和值甚至是基于C语言的非常好的方法,没有关联数组/字典/表文字,但是在Lua中,这个成语是

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

{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

所以在你的情况下,monsterdata可能只是

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

and you could remove the for loop altogether

你可以完全删除for循环

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:

注意:您只能以这种方式表示字符串键。对于其他类型的键,如数字,布尔值,甚至函数和其他表,请在[方括号]中包围键。例如,如果你想要一个mynot表,要将布尔值映射到它们的对立面,你可以使用:

{
    [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!

我想你越了解Lua,你就会越喜欢它。它真的是一种很棒的语言,有很多有趣的小功能。快乐的编码!