Math expression for all attributes in a table column
kolabo
Member Posts: 240
I'm writing a rule that says when all the boolean attributes in column 1 of "successTable" are true change game.attribute "game.success" to true.
Is there a way to write this rule using one condition in a single math expression as opposed to writing a bunch of conditions like this
Rule - When all
Numeric Expression tableCellValue(game.SuccessTable, 1, 1)
Numeric Expression tableCellValue(game.SuccessTable, 2, 1)
Numeric Expression tableCellValue(game.SuccessTable, 3, 1) ?
Is there a way to write this rule using one condition in a single math expression as opposed to writing a bunch of conditions like this
Rule - When all
Numeric Expression tableCellValue(game.SuccessTable, 1, 1)
Numeric Expression tableCellValue(game.SuccessTable, 2, 1)
Numeric Expression tableCellValue(game.SuccessTable, 3, 1) ?
Comments
Just type 'and' between your conditions in the expression editor
New to GameSalad? (FAQs) | Tutorials | Templates | Greenleaf Games | Educator & Certified GameSalad User
When
Numeric Expression tableRowCount(game.successTable)=12
Oops spoke to fast. This just counts the rows. I still need to figure out how to count them only when they are true. Will try something and repost.
The expression editor doesn't contain a sum expression. Can I just add it manually?
If you have the nightly version of Creator, it includes a Loop over Table behavior that can be used to easily move down the table and add each value to a sum attribute.
New to GameSalad? (FAQs) | Tutorials | Templates | Greenleaf Games | Educator & Certified GameSalad User
Rule; When Loop is false
Change Attribute Count to Count + 1
When tableCellValue(game.SuccessTable, Count, 1) = true
Change Attribute Successes to Successes + 1
Change Attribute Loop to true
Rule: When Count < 12 and Loop is true
Change Attribute Loop to false
Rule: When Count = 12 and Successes = 12
Change Attribute game.success to true
""You are in a maze of twisty passages, all alike." - Zork temp domain http://spidergriffin.wix.com/alphaghostapps
it will be a quite long expression, but you can handle it (and use copy/paste)
tableCellValue(your.table,row, column1)+tableCellValue(your.table,row, column1)+ ... +tableCellValue(your.table,row, column12)
No need to create other rules or attributes or loops.
If you have more table cells to calculate, it will hurt with the current expression editor
Just to clarify, @kolabo, using a solution for your first question, (not your sum question) you DO need other Rules, attributes and a loop mechanism.
But as to your second question, you could use a loop system to get your sum, if you preferred, instead of a long expression adding all the rows one by one. The solution below would be particularly time-saving with very many rows in particular, to add up:
Rule: When Loop is false
Change Attribute Count to Count + 1
Change Attribute Sum to Sum+ tableCellValue(game.SuccessTable, Count, 1)
Change Attribute Loop to true
Rule: When Count < 12 and Loop is true --12 being your number of rows
Change Attribute Loop to false
""You are in a maze of twisty passages, all alike." - Zork temp domain http://spidergriffin.wix.com/alphaghostapps
@gyroscope I will make some time tonight or tomorrow to give this a go. Thanks.
@gyroscope I've used your suggestions in a loop behavior. Very nice solution.
Loop
Until Attribute Count = TableRowCount(game.SuccessTable)
Change Attribute Count to Count + 1
When tableCellValue(game.SuccessTable, Count, 1) = true
Change Attribute Successes to Successes + 1
And now, I can even add rows to my SuccessTable without changing this behavior!
Cheers, @kolabo, I'm glad you like the solution and it worked out for you. :-)
""You are in a maze of twisty passages, all alike." - Zork temp domain http://spidergriffin.wix.com/alphaghostapps