Error 500 on my previous post (MOAI class properties)

New to Moai? Get started here with our best tips and tutorials.

Error 500 on my previous post (MOAI class properties)

Postby kleibrook » Sun Jul 01, 2012 4:26 pm

So I can see that there are a couple of replies to my previous post, but for whatever reason that post is reporting an internal server error when I try to read it :|

If an admin knows what's up and can fix it that would be great, otherwise if whoever posted replies would be kind enough to reply again... :?
User avatar
kleibrook
 
Posts: 7
Joined: Wed Jun 20, 2012 11:16 pm

Re: Error 500 on my previous post (MOAI class properties)

Postby sgeos » Sun Jul 01, 2012 9:48 pm

You can print the properties in a table like this:
Code: Select all
  1. -- Function to print properties of table

  2. function printProperties(pTable, pName)

  3.   if "table" == type(pTable) then

  4.     for k,v in pairs(pTable) do

  5.       if "number" == type(k) then

  6.         print(pName .. "[" .. k .. "] =", v, "(" .. type(v) .. ")")

  7.       else

  8.         print(pName .. "." .. k .. " =", v, "(" .. type(v) .. ")")

  9.       end

  10.     end

  11.   else

  12.     print(pName .. " =", pTable, "(" .. type(pTable) .. ")")

  13.   end

  14. end

  15.  

  16. -- Works with tables...

  17. local table = {x=123, y=456, [1]="apples", [2]={x=789, y=0}}

  18. printProperties(table, "table")

  19.  

  20. -- ... but not with user data

  21. printProperties(MOAIInputMgr.device, "MOAIInputMgr.device")

Any original code posted by me is released via the CC0 Public Domain Dedication. It is in the public domain. Do whatever you want with it.
User avatar
sgeos
 
Posts: 241
Joined: Sat Apr 28, 2012 4:42 am
Location: Married in Japan.

Re: Error 500 on my previous post (MOAI class properties)

Postby pygy79 » Mon Jul 02, 2012 5:18 am

The error 500 is my fault (more on that below).

For the methods of userdatas are stored in the __index of their metatables. When the __index is a table, we can iterate over it. Sometimes, it is a function (MOAIInputMgr.device, for example), and we're out of luck.

The following function prints the fields of both tables and __index (when it is a table), in alphabetic order.
__index fields are prefixed with "-", table fields are prefixed with "=".

Code: Select all
  1.  

  2. function printFileds(T)

  3.         mt = getmetatable(T)

  4.         local accm={}

  5.         if mt and type(mt.__index)=="table" then

  6.                 for n,m in pairs (mt.__index) do

  7.                         table.insert(accm,n)

  8.                 end

  9.                 for _,n in ipairs(accm) do

  10.                         print("- "..n)

  11.                         print("        "..tostring(mt[n]),"(" .. type(mt[n]) .. ")")

  12.                 end

  13.                 accm={}

  14.         elseif mt and type(mt.__index)=="function" then

  15.                 print("# the __index is a function")

  16.         end

  17.         if type(T) == "table" then

  18.                 for n in pairs(T) do

  19.                         table.insert(accm,n)

  20.                 end

  21.                 table.sort(accm)

  22.                 for _,n in ipairs(accm) do

  23.                         print("- "..n)

  24.                         print("        "..tostring(T[n]),"(" .. type(T[n]) .. ")")

  25.                 end

  26.         else

  27.                 print(tostring(T), "(" .. type(T) .. ")")

  28.         end

  29. end



In my previous post, I posted the output (~2000 lines) of the following code. The forum chokes on it.
Code: Select all
  1. --Print the methods of all Moai classes, in alphabetic order.

  2. acc= {}

  3.  

  4. for k,v in pairs(_G) do

  5.     if k:find"MOAI" then table.insert(acc,k) end

  6. end

  7. table.sort(acc)

  8. for _,k in ipairs(acc) do

  9.         print"\n"

  10.         print (k)

  11.         printFields(_G[k])

  12. end

User avatar
pygy79
 
Posts: 69
Joined: Wed Jan 04, 2012 3:13 am

Re: Error 500 on my previous post (MOAI class properties)

Postby kleibrook » Mon Jul 02, 2012 9:34 pm

Thanks for the info guys, that will definitely come in handy.

It seems that the take-away though, is that to get complete coverage at least it's still necessary to refer to the headers/source, at least with the current state of MOAI's documentation.
User avatar
kleibrook
 
Posts: 7
Joined: Wed Jun 20, 2012 11:16 pm


Return to New Users

Who is online

Users browsing this forum: No registered users and 1 guest

x