Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8846363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:57:53+00:00 2026-06-14T11:57:53+00:00

This Lua code, creates a table and dynamically adds a new member. Running this

  • 0

This Lua code, creates a table and dynamically adds a new member. Running this I can get "hello" on the screen as expected:

foo = {}
foo.x = "hello"
print(foo.x)

But now I’m using SWIG to bind some C++ classes to Lua.
For that purpose, in a test.i (SWIG module file) I created a simple class like this:

%module test

%inline
%{

class Foo
{
public:
  Foo() { X = 0; }
  void SetX(int x) { X = x; }
  int GetX() { return X; }
private:
  int X;
};

%}

Then I wrote a test Lua code like that:

obj = test.Foo()
obj:SetX(5)
print("Number: " .. obj:GetX())

Running and getting "Number 5" as expected. The problem is that when I dynamically add a new member to my SWIG-binded object, and I try to access it, like so:

obj.Y = 7
print("Number: " .. obj.Y)

I get this error message:

"attempt to concatenate field 'Y' (a nil value)"

Is it possible to dynamically add new members on objects binded using SWIG? Is there some option without having to move to another Lua binding library?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T11:57:54+00:00Added an answer on June 14, 2026 at 11:57 am

    SWIG doesn’t use tables for its objects; it uses userdata. After all, those objects are C++ objects, and need to store C++ data that Lua code shouldn’t be able to touch.

    And I wouldn’t bother looking for “another Lua binding library”; pretty much all of them use userdata, which Lua code explicitly cannot modify (in order to provide the ability to do exactly this).

    However, that doesn’t mean you can’t cheat.

    You can always wrap the object you get from C++ code into your own Lua table, which would have a metatable that forwards unknown calls to the C++ object. The code to do so would look something like this:

    local function WrapObject(cppObject)
    
        local proxy = {}
    
        local wrapper_metatable = {}
    
    function wrapper_metatable.__index(self, key)
        local ret = rawget(self, key)
        if(not ret) then
            ret = cppObject[key]
            if(type(ret) == "function") then
                return function(self, ...)
                    return ret(cppObject, ...)
                end
            else
                return ret
            end
        else
            return ret
        end
    
    end
    
    
        setmetatable(proxy, wrapper_metatable)
        return proxy
    end
    

    The returned proxy object is a Lua table that can have keys and values set on it. When you get a value, such as to call a function, it will see if that value was set in the table. If not, it attempts to fetch it from the C++ object that you wrapped, which will go through its metatable.

    You’ll need to expand this metatable if your C++ class uses other metafunctions like __add, __sub, __tostring and so forth.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have some lua code like this: doSomething(function() print(Hello!) end) How can I
I am tring to get table length with lua_rawlen, code like this lua_createtable(L, 0,
Can someone explain why the rectangle doesn't spin here? This is Lua code and
The code related to this question is here: https://github.com/jchester/lua-polarssl/tree/master/src Currently I'm trying to wrap
Why can't this Lua function using a self : be marked local without getting:
I have seen code in lua like this if (a==b or false) then what
If I had code like this in Lua, how would I call 'this'? array
I am writing D2 bindings for Lua. This is in one of the Lua
This is my code: <CheckBox android:id=@+id/sprint_checkbox android:layout_width=fill_parent android:layout_height=wrap_content android:text=@string/sprint_game /> <CheckBox android:id=@+id/marathon_checkbox android:layout_width=fill_parent android:layout_height=wrap_content
This code below allows me to find the word error in all my files

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.