Count how many objects are in selected state with an incremental variable

Oct 31, 2023

Hello, I have an object with a hover / selected state. I've used auto states to select / deselect when I click on an object.

I want to increment a variable when I click on this object to put it in the selected state, and to decrement it when I put it back to the normal state.

My triggers so far :

Trigger #1 : increment the counter variable by 1 when I click on the object, if the state = selected

Trigger #2 : decrement the counter variable by 1 when I click on the object, if the state = normal

For it to increment, it seems that I have to use the target (resultant) state of the click, not the initial state ... but sadly the decrementation does not work at all. 

6 Replies
Marine DEMAISON

Alright, although I'd love to see the reason why I cannot decrease the counter when the click returns the object to its normal state, I have found a quicker way to do it, by using the else option : 

What : Increase the counter variable by one

When : User clicks on object

Condition : if (resultant) state is selected

Else : Decrease the counter variable by one 

 

This creates a counter of how many objects are selected. Maybe there's a quicker way to count objects based on their states ? PLS tell me. 

Marine DEMAISON

Hello Phil and Nedim, thank you both for your great answers. 

To give you more details on what I was trying to do, I had a thermometer that had to go up and down depending on how many zones of a map were selected. I had it update on variable change, and the variable was changing based on the Select / Deselect states when the users clicks. It works great ... but needs quite some triggers !!

@Nedim : that's the solution I've gone for, more or less. Thanks !

@Phil Thanks for the precision. I didn't try to use the "not equal to" test, that could have saved the day ;) ... But your trick of a "calculator layer" is a very useful one, that will definitely come in handy in a next project. I think it might help getting the trigger count a bit down, and help with future maintenance (easier logic to understand for colleagues !!!). 

Thanks a lot,

Walt Hamilton

Phil, of course, is correct. Incrementing and decrementing variables is always risky. It’s more reliable to count every time you need a change. Using a layer allows you to create one set of variables that will work for all your buttons. Everything else, do as Phil says.

I would change Nedim’s suggestion to do your changes when Button 1 is clicked, rather than relying on when the state changes to selected. Detecting state changes is not always reliable.