luamoonsharptabletop-simulator

Accessing data found in another object/script


In Tabletop Simulator, how can I exchange information between objects?

Is there a way, for example, to create a global variable?


Solution

  • To my knowledge, there are no global variables, and you can't create public object properties. But you're not out of luck.


    You can get and set an object's property using .getVar/.getTable and .setVar/.setTable. Docs.

    For example,

    Object aaaaaa

    function onChat(message, player)
        local o = getObjectFromGUID("bbbbbb")
        local x = o.getVar("x")
        x = x + 1
        o.setVar("x", x)
        print(x)
    end
    

    Object bbbbbb

    x = 0
    

    You can also use Global.getVar(...), etc.


    Alternatively, you can create a method, and call using .call. This provides better encapsulation.

    For example,

    Object aaaaaa

    function onChat(message, player)
        local o = getObjectFromGUID("82fbcf")
        local x = o.call("getX")
        x = x + 1
        o.call("setX", { value = x })
        print(x)
    end
    

    Object bbbbbb

    x = 0
    
    function getX(args)
        return x
    end
    
    function setX(args)
        x = args["value"]
    end
    

    The argument can only be a table.

    You can also use Global.call(...).