このビデオで見つけたクラスの例を変更して、もう少し効率的に使用できるようにしようとしています。私のコメントが、私が達成しようとしていることを十分に説明してくれることを願っています。私が抱えている問題は、データ テーブルを使用しようとすると、次のエラーが表示されることです: lua: class example.lua:7:
これは、配列が関数に適切に渡されていないことを意味していると思いますが、その理由はわかりません。私はLuaの初心者です。
これが私が持っているものです:
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);