Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines
(+1)

Yeah, assigning to a widget’s .value does not actually trigger the widget’s on change handler. That is a good thing - for example, you might have a slider and a field, so the player can enter a number by quickly scrubbing along the slider, or typing a precise number in the field. Changes to the slider need to set the field text, changes to the field text need to set the slider value, so if setting .value automatically triggered on change, it would get caught in a loop: changing the text would set the slider, which would change the text, which would set the slider… The way it works now, each field can just set the other’s value to match its own without any mishaps.

For what it’s worth, your code could be simplified a little. Since both arms of the if test me[pos] = colors.black, we can lift that condition out:

val:Switch.value
if me[pos] = colors.black
 if (Switch.value = 0)
  Switch.value:Switch.value + 1
 else
  Switch.value:Switch.value - 1
 end
end
Switch.event["change" val] 

That inner if statement now just makes Switch.value into 1 if it was 0, and 0 if it was 1. It turns out Lil already has an operator that does exactly that: the ! operator.

val:Switch.value
if me[pos] = colors.black
 Switch.value:!Switch.value
end
Switch.event["change" val] 

Lastly, I see you’re sending the “change” event with the original value, even if the new value is changed. Normally the “change” event gets called with the new value, so that can get confusing if sometimes it gets the new value (from the user) and sometimes the old (from code like this). Unless you specifically need it to be the original value for some reason, it’s probably better to send the new value - then you don’t need to save the original value at all. Also, the “change” event is typically only sent when something actually changes:

if me[pos] = colors.black
 Switch.value:!Switch.value
 Switch.event["change" Switch.value] 
end
(+2)

Thank you for the optimization!

Unironically, I love it when, indeed, a program could be simplified further.