Page 3 of 3

Re: Knife: a collection of micro-modules

Posted: Wed Dec 21, 2016 8:46 am
by airstruck
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:

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)
or

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)
Could also simplify it a bit:

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)
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.
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.

If that's even more confusing, just forget I said that and I'll try to think of a better way to explain it.

Re: Knife: a collection of micro-modules

Posted: Fri Dec 23, 2016 2:51 am
by scissors61
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.

If that's even more confusing, just forget I said that and I'll try to think of a better way to explain it.
I think I understand now, and that general statement seems to express a lot of how parameters with tables work. I'm thinking of references as coordinates of what is changeable (values and the ability to expand the table), but they don't lead to the already set up tables and fields so I can't change them (maybe it's not like that but I'm trying to create non-programmer explanations for this lol).
I still have a lot to learn, for instance I don't comprehend the solutions or the source of the problem with return, but I decided to use the elseif solution you provided.
Thanks for the support, I think I'm going to use this library for all my projects, specially after failing to learn and implement an oop system with not a really oop language.

Edit:
Something I noticed using knife.system for a while, the - before aspects is helpful to omit a component from the arguments list but it doesn't work when several components are grouped to be optional with the pipe character |. Meaning: "-mainMenu|arcade|gameover" does not and it has to be part of the components list.

Re: Knife: a collection of micro-modules

Posted: Sat Feb 25, 2017 7:52 pm
by Zireael
Meaning: "-mainMenu|arcade|gameover" does not and it has to be part of the components list.
Does not what?

Re: Knife: a collection of micro-modules

Posted: Sat Feb 25, 2017 8:04 pm
by airstruck
scissors61 wrote: Fri Dec 23, 2016 2:51 amSomething I noticed using knife.system for a while, the - before aspects is helpful to omit a component from the arguments list but it doesn't work when several components are grouped to be optional with the pipe character |. Meaning: "-mainMenu|arcade|gameover" does not and it has to be part of the components list.
Sorry, I missed that. I'll take a look at it. You can report stuff like this in the issue tracker on github, I'll get it faster that way.

Re: Knife: a collection of micro-modules

Posted: Sat Feb 25, 2017 8:42 pm
by scissors61
Zireael wrote: Sat Feb 25, 2017 7:52 pm Does not what?
Sorry for my poor writing skills. It would be something like "it does not work, and therefore it cannot be omitted from the arguments list."
airstruck wrote: Sat Feb 25, 2017 8:04 pm Sorry, I missed that. I'll take a look at it. You can report stuff like this in the issue tracker on github, I'll get it faster that way.
Great, and I'll open a github.