开发者

Can you compare the "types" of tables or metatables in Lua?

开发者 https://www.devze.com 2023-02-04 08:24 出处:网络
I am calling an API function in the Lord of the Rings Online (LOTRO) Beta Lua scripting feature.The API开发者_运维知识库 method returns a \"type\" called ClassAttributes that will be on of the given C

I am calling an API function in the Lord of the Rings Online (LOTRO) Beta Lua scripting feature. The API开发者_运维知识库 method returns a "type" called ClassAttributes that will be on of the given Class Attribute "types". I say "types" because when I call type() on the return value, it says its a table.

Is there a way for me to check the type, or metatable type? e.g.:

local returnedTable = player:GetClassAttributes();

if (returnedTable.Name == "CaptainClassAttributes")
    print("You are playing a captain");
end

UPDATE The following code is what I use:

player = Turbine.Gameplay.LocalPlayer.GetInstance();

Turbine.Shell.WriteLine("player:GetClass():" .. player:GetClass());
Turbine.Shell.WriteLine("Turbine.Gameplay.Class.Captain:" .. Turbine.Gameplay.Class.Captain);

if (player:GetClass() == Turbine.Gameplay.Class.Captain) then
    Turbine.Shell.WriteLine("You are playing a captain");
end

and here is the output:

player:GetClass():24

Turbine.Gameplay.Class.Captain:24

You are playing a captain


The API docs are a bit confusing, although I suppose I found what you're looking for. The following code should tell you if player is a Captain:

local player = Turbine.Gameplay.Player
if (player:GetClass() == Turbine.Gameplay.Class.Captain) then
    print("You are playing a captain")
end

Captain is a member of the Gameplay.Class table, which is just an integer number as reading from the docs.

Note: You don't need to end a Lua sentence with a ";".

Couldn't test it. Hope it works.


If you have a list of the possible metatables for those values, you can get their metatables with the getmetatable(obj) function and compare them to the ones you already have. Without having access to the LOTRO API I can't say more about the subject: where can I read it?

That, of course, is assuming that the table returned by the GetClassAttributes() function has a metatable of itself, and that it is possible to differentiate between it's metatable and the other classes' attributes tables.

0

精彩评论

暂无评论...
验证码 换一张
取 消