Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes, It is possible to add new methods to an existing exported class.</p> <p>Foobar is just a plain table, so you can attach methods onto it as you would any other table. [1]</p> <pre><code>Foobar["extra_method"] = function (self, arg1, arg2) print(arg1, arg2) end </code></pre> <p>and now you can do:</p> <pre><code>Foobar:extra_method(1,2) </code></pre> <p>and <code>1 2</code> will be displayed.</p> <p>[1] tolua++ works somewhat oddly. It creates one main table for each class, which is what you see as Foobar, that holds both the static member functions of the class (e.g. <code>Foobar:new()</code>) <em>and</em> the instance member functions of the class (e.g. what <code>Foobar:method1()</code> likely is). The <code>:</code> operator in lua is syntactic sugar; <code>a:b()</code> is converted to <code>a.b(a)</code>. This means when you call <code>Foobar:new()</code>, it is transposed to <code>Foobar:new(Foobar)</code>, thus the <code>self</code> argument is the Foobar table. When you create an object of type Foobar, and then call method1() on it, <code>self</code> will be that object. Thus, adding a new method <code>method3</code> to the Foobar table will allow you to do <code>Foobar:method3()</code> <em>and</em> <code>obj = Foobar:new(); obj:method3()</code>, although the former will give an error. Thus, this answer <em>only</em> works for tolua++.</p> <p><strong>Edit:</strong> to address comment</p> <p>Let me use actual code here. So let's assume we have a class declaration of Foobar that looks like:</p> <pre><code>class Foobar { public: void method1(); static void method2(); }; </code></pre> <p>When tolua++ generates the lua binding code for this class it's going to give you a table with the following methods</p> <ul> <li>new() - Call as <code>Foobar:new()</code> to create an instance of Foobar.</li> <li>delete() - Call as <code>instance:delete()</code> to destroy an instance of Foobar.</li> <li>method1() - Call as <code>instance:method1()</code> to run method1 on instance.</li> <li>method2() - Call as <code>Foobar:method2()</code> to run method2.</li> </ul> <p>However, tolua++ doesn't actually use two different tables (one for the methods that should be attached to the class itself and one for the methods of the instance of that class). Instead, it merges the two together, so it's possible to run <code>Foobar:method1()</code> and <code>instance:new()</code>... even though that's not how the methods should be used. Thus, there is no difference between how static methods are used and how instance methods are used. Syntactically, it's also the same. (I personally feel that this is a problem with tolua++.)</p> <p>If you're looking for an example, here is how you should be calling these functions from within lua:</p> <pre><code>obj = Foobar:new() print( obj:method1() ) obj:delete() </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload