Results 1 to 7 of 7

Thread: Object Function Using Global Function

  1. #1

    Object Function Using Global Function

    Is it possible to access functions in the global script space from functions in object specific script space?

    For example, I have a lot of objects which run very similar code. Ideally I would like to put this code in the Global space so that all objects can access the same code (with different passed parameters) so that the code resides only in one place and is common to all objects using it. However, when I try to call a function that was defined in the Global script space from a object specific script space, the function does not seem to exist. The same seems to be true for variables defined in the global script space.

    My current work around is to create a dummy object on which I put these functions and then have all the specific objects use the call() function to run the common code. This however requires the object specific calls to have a reference to the common object (which I currently do by GUID) but this is not ideal because if the GUID ever changes it will be located in many places.

    My current code is as follows:

    [Global]

    function onload()
    common = getObjectFromGUID("cfa33a")
    end

    function hideButtons()
    local cs=getAllObjects()
    for i,c in ipairs(cs) do
    c.clearButtons()
    end
    end

    function showButton(params)
    local p = {}
    p.click_function = params[3]
    p.function_owner = params[1]
    p.label = params[2]
    p.position = {1, 2.5, 0}
    p.rotation = {0, 180, 0}
    p.width = 500
    p.height = 300
    p.font_size = 100
    params[1].createButton(p)
    end

    [Custom Board] (aka my common objects for common scripts)

    function hideButtons()
    local cs=getAllObjects()
    for i,c in ipairs(cs) do
    c.clearButtons()
    end
    end

    function showButton(params)
    local p = {}
    p.click_function = params[3]
    p.function_owner = params[1]
    p.label = params[2]
    p.position = {1, 2.5, 0}
    p.rotation = {0, 180, 0}
    p.width = 500
    p.height = 300
    p.font_size = 100
    params[1].createButton(p)
    end

    [BlockRectangle] (aka my object specific code which calls the common code)

    function onPickedUp(o)
    --[[ Using common (defined in Global space) does not work here so we need to get a local reference here --]]
    local common = getObjectFromGUID("cfa33a")
    local p = {self, "Button 1", "onBlockRectangleButton1Click"}
    --[[ Using the Custom Board common object works but requires each specific object to have a reference to the common object which means specifying the GUID --]]
    common.call("hideButtons",nil)
    common.call("showButton", p)
    --[[ Using direct calls to try to access the global space functions does not work --]]
    hideButtons();
    showButtons(p);
    end

    function onBlockRectangleButton1Click(o)
    print("Clicked Button 1 in Local")
    end

    Obviously my current example has only one specific object (BlockRectangle) but the idea is that there would be many of those objects each having a reference to the common object GUID which is undesirable.

    Is there some easy way for object specific script functions to access the global address space (for functions and/or variables)?

  2. #2
    Yes, there is.

    callLuaFunctionInOtherScriptWithParams(Global, "ScriptName", params)

    Where Global is where the script you want to call is located, "ScriptName" is the name of the script (duh), and params is any parameters you wish to send along with the call.

    E: That is, if I understood what you were after

  3. #3
    Exactly what I was looking for. Thanks.

  4. #4
    Quote Originally Posted by Nightgaunt View Post
    Yes, there is.

    callLuaFunctionInOtherScriptWithParams(Global, "ScriptName", params)
    This is really starting to tick me off. I knew I had seen before but couldn't remember the wording and couldn't find it again. Something else not in the Knowledgebase.

  5. #5
    You can also use Global.call('functionName', params)
    Params table is optional.

    They added call() in a patch to replace the old functions. It's in the Object page of the knowledge base.

  6. #6
    Quote Originally Posted by Skorpion9x View Post
    You can also use Global.call('functionName', params)
    Params table is optional.

    They added call() in a patch to replace the old functions. It's in the Object page of the knowledge base.
    I didn't realize you could use Global instead of an object. Thanks.

  7. #7
    Quote Originally Posted by Skorpion9x View Post
    You can also use Global.call('functionName', params)
    Params table is optional.

    They added call() in a patch to replace the old functions. It's in the Object page of the knowledge base.
    Wow, that is way shorter than the one I use

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •