Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

How to check if multiple checkboxes have been checked?

A topic by mzungushao created Sep 03, 2024 Views: 151 Replies: 2
Viewing posts 1 to 2
(+2)

Hello! I'm sure this is quite simple to do and I tried to skim through older posts, but I can't seem to find a way to make it work. I want an alert to be displayed only when the user has clicked on all available buttons. Here's the basic setup:


Here's the script for button1 (button2 has the same but with checkbox2):

on click do
    checkbox1.value:1  
    card.event["yeah"] 
end

Then, I got this card script:

on yeah do  
    if checkbox1.value+checkbox2.value=2
        alert["yeah!"]
    end
end

When I click on a button, the right checkbox gets checked, but the alert systematically triggers instead of only appearing when both checkboxes are checked. 

Developer (1 edit) (+1)

In Lil, expressions are evaluated from right to left unless overridden with parentheses. The expression

checkbox1.value + checkbox2.value = 2

Is equivalent to

checkbox2.value + (checkbox2.value = 2)

What you probably mean is

2 = checkbox1.value + checkbox2.value

Which is equivalent to

2 = (checkbox1.value + checkbox2.value)

You might find it slightly simpler to use "&" (minimum/logical AND), which for a pair of 0/1 values will be 1 if and only if both inputs were 1:

checkbox1.value & checkbox2.value

If you have a whole bunch of checkboxes, you could make a list of them, extract each of their values, and take the minimum:

min (c1,c2,c3,c4)..value

You can use Decker's Listener to test out expressions like the above while you work; it's a very handy tool!

It's also worth noting that even if a button isn't visibly a checkbox, it still has a ".value" field like a checkbox, so you could simplify things by tracking clicks in the buttons themselves:

on click do
 me.value:1
 card.event["yeah"]
end 

If the "yeah[]" function is defined on the same card as the widget, (or in the deck script) it can be called directly, instead of triggered through an event (either way is fine. Using events is necessary if you're calling a function defined on a different card or widget):

on click do
 me.value:1
 yeah[]
end

Does that help?

(+2)

This is great, thanks a lot! As a straight up rookie, your answers are pure gold. It's feels great to learn so many new things so quickly.