Results 1 to 4 of 4

Thread: Parameter variable name error

  1. #1
    Join Date
    May 2016
    Posts
    1,072

    Parameter variable name error

    I have boiled this but down to a repeatable format and I am working on a solution. Apply this script to a red block.

    Code:
    function onload()
        createButtonsOnSelf()
    end
    
    function createButtonsOnSelf()
        local howManyButtons = 0
        for _=1, 3 do
            local objPos = {0,3,0+2*howManyButtons}
            local funcName = "testButton_" .. howManyButtons
            local func = function() buttonClick_test(howManyButtons) end
            self.setVar(funcName, func)
            self.createButton({
                label=funcName, click_function=funcName, function_owner=self,
                position=objPos, rotation=rot, height=1000, width=1000
            })
            howManyButtons = howManyButtons + 1
        end
    end
    function buttonClick_test(testIndex)
        print(testIndex)
    end
    It creates 3 buttons. Each button gets a unique name tied to an index (howManyButtons), starting at 0. a function is created named func which is designed to trigger another function with a parameter of its index (0, 1 or 2 from howManyButtons. Then, the button is made and 1 is added to howManyButtons.

    Except when all 3 of these buttons are pressed, they are printing "3". However when setVar is being created, howManyButtons is equal to 0, 1 or 2. It should not be making their value 3 or repeating it. I am unsure what element is causing it at this point, but it is definitely some manner of bug.
    Last edited by MrStump; 06-04-2017 at 08:11 PM.

  2. #2
    Join Date
    May 2016
    Posts
    1,072
    I also fully recognize I could be overlooking something, so if someone could doublecheck my work here I'd appreciate it.

    I tried using a co-routine of 0.1 second delay between each button creation but that didn't seem to help it
    Last edited by MrStump; 06-04-2017 at 08:17 PM.

  3. #3
    Join Date
    Sep 2016
    Posts
    219
    I had a similar problem to this in C#. In that case, I was trying to use a delegate function with various variables, but when it was being called it was retrieving whatever value existed on the stack for those variables at the time of execution, instead of the values at the time the delegate function was created. My solution was to make sure that the variables on the stack didn't change after the delegate function was created. I don't know how it works in Lua under the hood, but I tried this solution and it worked:

    Code:
    function onload()
        createButtonsOnSelf()
    end
    
    function createButtonsOnSelf()
        local howManyButtons = 0
        for _=1, 3 do
            createButtonOnSelf(howManyButtons)
            howManyButtons = howManyButtons + 1
        end
    end
    
    function createButtonOnSelf(buttonIndex)
        local objPos = {0,3,0+2*buttonIndex}
        local funcName = "testButton_" .. buttonIndex
        local func = function() buttonClick_test(buttonIndex) end
        self.setVar(funcName, func)
        self.createButton({
            label=funcName, click_function=funcName, function_owner=self,
            position=objPos, rotation=rot, height=1000, width=1000
        })
    end
    
    function buttonClick_test(testIndex)
        print(testIndex)
    end
    My guess is when function() buttonClick_test(howManyButtons) end is being called, it is checking the value of howManyButtons on the stack. Since the function had already run through in it's entirety, the value of howManyButtons is stuck at 3. With my code, the value of buttonIndex on the stack doesn't change for each function call.

  4. #4
    Join Date
    May 2016
    Posts
    1,072
    Worked like a charm as a workaround, thank you.

    I added a line at the top of the for loop making dummyIndex = howManyButtons and then switched buttonIndex in "func"s creation into dummyIndex and it is working as expected. Another fun Lua lesson learned

Similar Threads

  1. Add callback parameter to paste
    By Tanners in forum Scripting Suggestions
    Replies: 0
    Last Post: 02-17-2017, 06:27 PM
  2. Button parameter suggestion
    By kiska in forum Scripting
    Replies: 2
    Last Post: 09-27-2016, 10:38 PM
  3. Interactable variable bug
    By Tanners in forum Scripting
    Replies: 0
    Last Post: 05-07-2016, 02:47 PM
  4. Table variable not saving new value
    By Skorpion9x in forum Scripting
    Replies: 4
    Last Post: 04-28-2016, 08:31 PM
  5. Add 'Cannot Be Box-Selected' Parameter
    By Adventurer in forum Suggestions
    Replies: 3
    Last Post: 05-22-2015, 09:21 AM

Posting Permissions

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