Math expression for all attributes in a table column

kolabokolabo Member Posts: 240
edited January 2014 in Working with GS (Mac)
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) ?

Comments

  • slowcutslowcut Member, PRO Posts: 164
    Hi,
    Just type 'and' between your conditions in the expression editor
  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    Pretty sure if you have booleans you can also just add them together (0=false, 1=true) so you could check to see if the sum of all cells is equal to the tableRowCount.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • kolabokolabo Member Posts: 240
    edited January 2014
    Pretty sure if you have booleans you can also just add them together (0=false, 1=true) so you could check to see if the sum of all cells is equal to the tableRowCount.
    What a wonderful solution! My new rule has 1 condition instead of 12 :)

    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.




  • kolabokolabo Member Posts: 240
    Can't figure out how to find the sum of the cells in the column.

    The expression editor doesn't contain a sum expression. Can I just add it manually?
  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    Yes, you can add individual table cells and it will work and it will be a LONG expression.

    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

  • gyroscopegyroscope I am here.Member, Sous Chef, PRO Posts: 6,598
    edited January 2014
    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) ?
    Hi @kolabo working on your boolean yes or no's, try the following: Make an integer attribute, called Count and another called Successes so at the end, use these Rules:

    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

  • slowcutslowcut Member, PRO Posts: 164
    @kolabo as long as you have only twelve table cells, I would suggest to calculate the sum in the expression editor,
    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
  • gyroscopegyroscope I am here.Member, Sous Chef, PRO Posts: 6,598
    edited January 2014

    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

  • kolabokolabo Member Posts: 240
    @slowcut Agreed. Your solution works well. In my current game I'm pushing myself to look for well built and elegant solutions, and therin lies my need to find a formula. It will pay off in the long run.

    @gyroscope I will make some time tonight or tomorrow to give this a go. Thanks.
  • kolabokolabo Member Posts: 240
    Thanks all. This has been very helpful.
    @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!
  • gyroscopegyroscope I am here.Member, Sous Chef, PRO Posts: 6,598

    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

Sign In or Register to comment.