Re: Knife: a collection of micro-modules
Posted: Wed Dec 21, 2016 8:46 am
Looks like your checkboxes are getting selected and then immediately deselected. If selected is 0, you change it to 1, but after that, if it's 1, you change it to 0. Use elseif or early return to avoid it:
or
Could also simplify it a bit:
If that's even more confusing, just forget I said that and I'll try to think of a better way to explain it.
Code: Select all
checkbox.study.checkboxSystem = system(
{"name", "=selected", "x", "y", "w", "h", "-study"},
function(name, selected, bx, by, w, h, mx, my, button)
if selected == 0 and button == 1 and mx > bx and mx < bx+w and my > by and my < by+h then
selected = 1
elseif selected == 1 and button == 1 and mx > bx and mx < bx+w and my > by and my < by+h then
selected = 0
end
return selected
end)
Code: Select all
checkbox.study.checkboxSystem = system(
{"name", "=selected", "x", "y", "w", "h", "-study"},
function(name, selected, bx, by, w, h, mx, my, button)
if selected == 0 and button == 1 and mx > bx and mx < bx+w and my > by and my < by+h then
return 1
end
if selected == 1 and button == 1 and mx > bx and mx < bx+w and my > by and my < by+h then
return 0
end
return selected
end)
Code: Select all
checkbox.study.checkboxSystem = system(
{"name", "=selected", "x", "y", "w", "h", "-study"},
function(name, selected, bx, by, w, h, mx, my, button)
if button == 1 and mx > bx and mx < bx+w and my > by and my < by+h then
return -selected + 1 -- toggle between 1 and 0
-- or use true/false instead and `return not selected`
end
return selected
end)
I understand your confusion. Here's one way to think about it: passing a value into a function always makes a copy of that value. But, when we're talking about things like tables and functions, "value" means something like a reference to the "actual" table or function, and the "actual" table or function is something we never actually deal with; we only deal with those reference-values. In other words, you don't get a copy of a table, you get a copy of a reference to a table.So, I cannot describe how parameters works with a general statement or logic such as it just makes a copy of the variable, or it just pass what's inside a variable, or the variable itself. To explain it properly it's necessary to point out all the rules and exceptions.
If that's even more confusing, just forget I said that and I'll try to think of a better way to explain it.