Knife: a collection of micro-modules

Showcase your libraries, tools and other projects that help your fellow love users.
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: Knife: a collection of micro-modules

Post 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.
User avatar
scissors61
Citizen
Posts: 76
Joined: Fri Jan 08, 2016 10:16 am

Re: Knife: a collection of micro-modules

Post 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.
Zireael
Party member
Posts: 139
Joined: Fri Sep 02, 2016 10:52 am

Re: Knife: a collection of micro-modules

Post by Zireael »

Meaning: "-mainMenu|arcade|gameover" does not and it has to be part of the components list.
Does not what?
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: Knife: a collection of micro-modules

Post 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.
User avatar
scissors61
Citizen
Posts: 76
Joined: Fri Jan 08, 2016 10:16 am

Re: Knife: a collection of micro-modules

Post 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.
Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests