Best way to find out all of the properties of MOAI classes?

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

Best way to find out all of the properties of MOAI classes?

Postby kleibrook » Sun Jul 01, 2012 12:11 am

I'm fairly new to both lua and moai, so my apologies if i'm overlooking something obvious. Aside from the samples, or reading the MOAI host code, is there a way for me to discover all of the properties that MOAI classes have?

For example I can see from the input examples that MOAIInputMgr has a device property, and whatever that is has a touch property, but referring to the MOAIInputMgr SDK documentation shows no members.

What is the expected/easiest way for me to discover properties which I don't notice (or aren't demonstrated) in the samples and otherwise don't know exist?

Thanks
User avatar
kleibrook
 
Posts: 7
Joined: Wed Jun 20, 2012 11:16 pm

Re: Best way to find out all of the properties of MOAI class

Postby nosheet » Sun Jul 01, 2012 4:21 am

This is actually a good question. Obviously, we can try to look for things in documentation, but very often in any SDK there are hidden gems not covered by documentation.
Some of MOAI Classes are represented as Lua tables, such as MOAIProp, and they can simply be iterated as:
Code: Select all
  1. for k,v in pairs( MOAIProp ) do

  2. print (k,v)

  3. end



Others, such as MOAIInputMgr are user data, and I am not sure if there is any way to access the user data members from Lua, so the best shot would be to download MOAI source and look in the proper source file.
You may hate my haircut, but you'll love my games:
http://www.spin-up-game.com and http://www.foosballhero.com
User avatar
nosheet
 
Posts: 188
Joined: Mon May 28, 2012 2:40 pm
Location: Madrid, Spain

Re: Best way to find out all of the properties of MOAI class

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

Looks like somebody cleaned up the post causing the server error. Thanks for the tip, nosheet
User avatar
kleibrook
 
Posts: 7
Joined: Wed Jun 20, 2012 11:16 pm

Re: Best way to find out all of the properties of MOAI class

Postby ibisum » Tue Jul 03, 2012 3:53 am

I use table.show():

Code: Select all
  1.  

  2.  

  3.  

  4. --[[

  5.    Author: Julio Manuel Fernandez-Diaz

  6.    Date:   January 12, 2007

  7.    (For Lua 5.1)

  8.    

  9.    Modified slightly by RiciLake to avoid the unnecessary table traversal in tablecount()

  10.  

  11.    Formats tables with cycles recursively to any depth.

  12.    The output is returned as a string.

  13.    References to other tables are shown as values.

  14.    Self references are indicated.

  15.  

  16.    The string returned is "Lua code", which can be procesed

  17.    (in the case in which indent is composed by spaces or "--").

  18.    Userdata and function keys and values are shown as strings,

  19.    which logically are exactly not equivalent to the original code.

  20.  

  21.    This routine can serve for pretty formating tables with

  22.    proper indentations, apart from printing them:

  23.  

  24.       print(table.show(t, "t"))   -- a typical use

  25.    

  26.    Heavily based on "Saving tables with cycles", PIL2, p. 113.

  27.  

  28.    Arguments:

  29.       t is the table.

  30.       name is the name of the table (optional)

  31.       indent is a first indentation (optional).

  32. --]]

  33. function table.show(t, name, indent)

  34.    local cart     -- a container

  35.    local autoref  -- for self references

  36.  

  37.    --[[ counts the number of elements in a table

  38.    local function tablecount(t)

  39.       local n = 0

  40.       for _, _ in pairs(t) do n = n+1 end

  41.       return n

  42.    end

  43.    ]]

  44.    -- (RiciLake) returns true if the table is empty

  45.    local function isemptytable(t) return next(t) == nil end

  46.  

  47.    local function basicSerialize (o)

  48.       local so = tostring(o)

  49.       if type(o) == "function" then

  50.          local info = debug.getinfo(o, "S")

  51.          -- info.name is nil because o is not a calling level

  52.          if info.what == "C" then

  53.             return string.format("%q", so .. ", C function")

  54.          else

  55.             -- the information is defined through lines

  56.             return string.format("%q", so .. ", defined in (" ..

  57.                 info.linedefined .. "-" .. info.lastlinedefined ..

  58.                 ")" .. info.source)

  59.          end

  60.       elseif type(o) == "number" or type(o) == "boolean" then

  61.          return so

  62.       else

  63.          return string.format("%q", so)

  64.       end

  65.    end

  66.  

  67.    local function addtocart (value, name, indent, saved, field)

  68.       indent = indent or ""

  69.       saved = saved or {}

  70.       field = field or name

  71.  

  72.       cart = cart .. indent .. field

  73.  

  74.       if type(value) ~= "table" then

  75.          cart = cart .. " = " .. basicSerialize(value) .. ";\n"

  76.       else

  77.          if saved[value] then

  78.             cart = cart .. " = {}; -- " .. saved[value]

  79.                         .. " (self reference)\n"

  80.             autoref = autoref ..  name .. " = " .. saved[value] .. ";\n"

  81.          else

  82.             saved[value] = name

  83.             --if tablecount(value) == 0 then

  84.             if isemptytable(value) then

  85.                cart = cart .. " = {};\n"

  86.             else

  87.                cart = cart .. " = {\n"

  88.                for k, v in pairs(value) do

  89.                   k = basicSerialize(k)

  90.                   local fname = string.format("%s[%s]", name, k)

  91.                   field = string.format("[%s]", k)

  92.                   -- three spaces between levels

  93.                   addtocart(v, fname, indent .. "   ", saved, field)

  94.                end

  95.                cart = cart .. indent .. "};\n"

  96.             end

  97.          end

  98.       end

  99.    end

  100.  

  101.    name = name or "__unnamed__"

  102.    if type(t) ~= "table" then

  103.       return name .. " = " .. basicSerialize(t)

  104.    end

  105.    cart, autoref = "", ""

  106.    addtocart(t, name, indent)

  107.    return cart .. autoref

  108. end

  109.  

  110.  




.. then:

Code: Select all
  1.  

  2.     print(table.show(MOAIEnvironment, "MOAIEnvironment")) -- dumps the table

  3.  




It often reveals lots of interesting things .. also good to use on the _gui table of MOAIGUI, for example, when trying to determine widget names and hierarchies .. At some point I'd like to write a general-purpose table navigator using MOAI features ..
;
--
ibisum@gmail.com
Got a MOAI snippet? Please consider adding it to http://moaisnippets.info
User avatar
ibisum
 
Posts: 1003
Joined: Mon Oct 17, 2011 1:11 am
Location: Vienna, Austria


Return to New Users

Who is online

Users browsing this forum: No registered users and 1 guest

cron

x