scissors61 wrote:doing this changes selected to 20
Looks like you forgot to remove "entity" from the parameter list, so the "entity" param actually held the value of the "selected" component, and "selected" held the value of "x" (which was 20, I assume).
If you remove "entity" from the parameter list, what you had in that top code snippet should work.
In the second code snippet, you have a possible execution path that doesn't return anything, which would result in "selected" being nil (when neither of those conditions are true, nothing is returned).
I was thinking that if I return something I need a recipient (variable) to store it, but it seems that knife.system doesn't need that, and although I'm not doing it in the right way, it's returning values to the components.
Yeah, ordinarily you would need to do something like that. In this case the library does that step for you.
If you're curious what's actually going on there, you can edit knife/system.lua and add a line with print(source) just before the last line in the file beginning with "return." That will dump the generated code to the console whenever you create a System.
I don't see the logic behind, I mean if this is passed "callback["selected"] = 1" to callback, why the much simpler "selected = 1" isn't passed to selected.
This is what I was getting at earlier. Say you write something like this:
Code: Select all
local function toggleSelection (selected)
if selected == 1 then
selected = 0
elseif selected == 0 then
selected = 1
end
end
And then you write something like this:
Code: Select all
local selected = 1
toggleSelection(selected)
print(selected) -- it's still 1
You didn't actually pass your variable
selected into toggleSelection, you just passed its value, 1. Inside the function, you have something called
selected again, and its value is 1 (until it changes to 0), but it's just internal to that function. It's not the same
selected that you passed in. So you can change the value of toggleSelection's internal
selected, but it makes no difference to the world outside of toggleSelection.
The reason you can get around this with tables is because tables have fields, and you can change the values those fields hold. So even though the function can't alter the value that an external variable holds, it doesn't need to, because it can alter the value of a field.
In other words, this works...
Code: Select all
local function toggleSelection (selected)
if selected.value == 1 then
selected.value = 0
elseif selected.value == 0 then
selected.value = 1
end
end
local selected = { value = 1 }
toggleSelection(selected)
print(selected.value) -- it's 0 now
And conversely, this doesn't work...
Code: Select all
local function toggleSelection (selected)
if selected.value == 1 then
selected = { value = 0 }
elseif selected.value == 0 then
selected = { value = 1 }
end
end
local selected = { value = 1 }
toggleSelection(selected)
print(selected.value) -- it's still 1
...because here, we're assigning an entirely new value to
selected, which is local to toggleSelection, and that new value ends up in the garbage once toggleSelection ends, just like the first example.
Don't worry, it's difficult to explain properly and probably not easy to understand if you're just getting started, but it should make sense after a while.