2

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

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
John B
  • 20,062
  • 35
  • 120
  • 170

2 Answers2

3

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.

RBerteig
  • 41,948
  • 7
  • 88
  • 128
  • Here is a version of the API docs: http://www.lotrointerface.com/wiki/LocalPlayer – John B Jan 12 '11 at 19:22
  • So what I'm hearing is that it may not be possible? That is pretty surprising to me as a newcomer to Lua! – John B Jan 12 '11 at 19:23
  • I didn't have enough time to read it yet, but it may not be possible because, if the returned values are simple tables with values, no inheritance nor anything, there may be no reason for them to have distinct metatables. The type of the value you have is a table, and nothing more than a table may be needed. Some people convention ways to build OOP in Lua by dealing with metatables in a way that you can differentiate them, but it's not a rule set in stone. (Edit: When I say OOP, I mean Class-based OOP) – Alexandre Araujo Moreira Jan 13 '11 at 00:03
  • I just gave it check and, although the document doesn't say it, it appears to me that each object has a metatable that is different according to the type. You should be able to, having those tables in http://www.lotrointerface.com/wiki/ClassAttributes at hand, compare those to (probably) the metatable on the result you get from calling the method. Although the lack of documentation may hint that, whatever you're trying to do could possibly be done in other way. – Alexandre Araujo Moreira Jan 13 '11 at 00:26
  • So it sounds like I should be able to get a reference to the metatable for a given "type", and then compare that to the metatable returned from a function. Any idea how I would do that? – John B Jan 13 '11 at 16:06
  • getmetatable(obj) gives you the metatable on one object, you can use this to get the metatable on whatever player:GetClassAttributes() returns. After that, you should be able to compare to those tables given in the link on my previous message, the wiki page on ClassAttributes. If you're lucky, you'll be able to compare them easily. – Alexandre Araujo Moreira Jan 13 '11 at 17:13
  • Thanks for the help Alexandre! The solution is the other guys answer, FYI. – John B Jan 19 '11 at 21:34
3

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.

erpy
  • 46
  • 2