1

In real project, TEST_TABLE would contain much of TEST_TABLE_NESTED, each with its own testVariable and bunch of testScript. test function from testScript would be used in C++ code, and TEST_TABLE_NESTED tables would be added automatically from C++ code too.

TEST_TABLE =
{
    TEST_TABLE_NESTED =
    {
        testVariable = 5, 

        testScript =
        {
            test = function()

                print(testVariable, "hello") --How to access 'testVariable'?

            end
        }
    }
}

EDIT : This is the actual scenario of using this script:

GameObjectScriptTables =
{
    GameObject_1 = --Container of scripts corresponding to some gameObject
    {
        gameObjectOwner = actual_object_passed_from_c++, --This is an actual object passed from c++

        GameObjectScript_1 = --This is a script with update(dt) method which will be called somwhere in c++ code
        {
            update = function(dt)

                --here I want to use some data from gameObjectOwner like position or velocity

            end
        }
    }

    GameObject_2 =
    {
        gameObjectOwner = actual_object_passed_from_c++,

        GameObjectScript_1 =
        {
            update = function(dt)

                --here I want to use some data from gameObjectOwner like position or velocity

            end
        },

        GameObjectScript_2 =
        {
            update = function(dt)

                --here I want to use some data from gameObjectOwner like position or velocity

            end
        }
    }

    --And so on
}

Idea is that exists some testVariable object (passed from C++), which data is used all over TEST_TABLE_NESTED. For me, above example looks natural for this task, but it prints nil instead of 5. So how to acces a testVariable from testScript without printing a full path like TEST_TABLE.TEST_TABLE_NESTED.testVariable?

Russel Ledge
  • 221
  • 3
  • 11
  • 1
    You can't. `testVariable` is not a closure for test function, but an index in TEST_TABLE_NESTED, which in turn is an index in TEST_TABLE. In order to access testVariable, you'd need access to TEST_TABLE_NESTED or TEST_TABLE. What's the actual scenario? – W.B. Aug 13 '14 at 11:47
  • @W.B. I'm using it for game scripting. I added more information – Russel Ledge Aug 13 '14 at 12:02

2 Answers2

3

You're asking for something like a "parent" pointer, which tells table B about table A, but that doesn't exist. Internally, the only association they have is that one of A's values happens to be B, but any number of tables could contain B as a value. Which is B's parent?

If you want B to know about A, you'll need to tell it. You can add an extra parameter to update which receives the game owner object, or update can be a closure which contains the game owner as a bound variable, so on and so forth.

Mud
  • 28,277
  • 11
  • 59
  • 92
-1

I made it work by providing a gameObjectOwner instance for each GameObjectScript_N. However I don't know is it expensive solution or not.

Russel Ledge
  • 221
  • 3
  • 11