Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How does one pass a connect line optional parameters with a named function?

Asked by 10 years ago

I'm trying to call a custom function through a button click connection and I want to pass an argument to the function but I'm getting an error that says the passed value (AddQuant('Food')) is not a function. What do I need to do to pass that argument? I don't want to use an anonymous function, and I've already checked spelling and capitalization. Anyways, here's the connection line and function.

--The function
function AddQuant(type)
    amount[type] = amount[type] + incriment[type]
    Labels[type].Text = "Food: " .. amount[type]    
end
]
--The connection line
Buttons['Food'].MouseButton1Click:connect(AddQuant('Food'))

Thanks for any help.

-GoldenPhysics

Edit: added function being called.

1 answer

Log in to vote
1
Answered by
MrFlimsy 345 Moderation Voter
10 years ago

When you connect an event, it adds its own parameters based on the event. Attempting to add your own via the connection line will produce an error. A solution is to call your named function with an anonymous function, like this:

--The function
function AddQuant(type)
    amount[type] = amount[type] + incriment[type]
    Labels[type].Text = "Food: " .. amount[type]    
end
]
--The connection line
Buttons['Food'].MouseButton1Click:connect(function()
    AddQuant('Food')
end)
0
I was hoping for a shorter connection line, but It works well. Thanks! GoldenPhysics 474 — 10y
Ad

Answer this question