embeded elseifs or just ifs

beaudoin_nbeaudoin_n Member Posts: 184
edited November -1 in Working with GS (Mac)
Hi,

Has anybody experimented of the advantages of using otherwise versus simply stating another rule

Example
rule if attribute a = 1
behavior
otherwise
rule if attribute a =2
behavior
otherwise
...

or simply rule1 if attribute a =1
behavior

rule 2 if attribute a = 2
behavior

How is performance affected ?
Are there known issues
Cases were it might cause bugs ?

Thanks

Comments

  • HunnenkoenigHunnenkoenig Member Posts: 1,173
    I never really understood else if rules, so it would interest me too.
  • firemaplegamesfiremaplegames Member Posts: 3,211
    Anything to save CPU cycles is key. Using Otherwise where appropriate is good practice.

    You want the processor to check as few conditions as possible.

    So in your second scenario, if your attribute IS 1, the second Rule still has to be checked, wasting resources.
  • beaudoin_nbeaudoin_n Member Posts: 184
    thanks :)

    Ok now i wish for a case function ;)
  • HunnenkoenigHunnenkoenig Member Posts: 1,173
    Thank you for explanation. Now I have to check all my rules :-P
  • beaudoin_nbeaudoin_n Member Posts: 184
    Arghhh ! Joe i need your help

    I have this positionning done on one of 5 spots
    and i check if i am not in any i try to find the first available.

    Somehow i got it all messed up :(

    It has a first rule that states (if out of bounds)
    then i had embedded ifs to check if the first spot value is false
    then otherwise if second spot is false
    etc...

    So looks like i have to keep the otherwise statements... i also had a mini timer but now it seems that with more objects on the screen it starts behaving weird :(

    is the speed of the timer in any way affected by the overall performance ?
  • beaudoin_nbeaudoin_n Member Posts: 184
    any more detailed explanation about the "run to completion" attribute ?
  • synthesissynthesis Member Posts: 1,693
    run to completion forces a rule to run to completion once it is executed.

    Example:
    If myMainSwitchingAttribute = True
    then... (run to completion checked)

    -- myMainSwitchingAttribute = False
    -- myOtherTrailingAttributes are still executed (even though the function rule is no longer valid)

    If myMainSwitchingAttribute = True
    then... (run to completion is NOT checked)

    -- myMainSwitchingAttribute = False
    -- myOtherTrailingAttributes are NOT executed and are ignored (because the mainSwitchingAttribute was changed and the function is no longer valid)
  • beaudoin_nbeaudoin_n Member Posts: 184
    How about with the timer rule ? how does the run to completion rule work then ?
  • synthesissynthesis Member Posts: 1,693
    The same principle is true. If it is checked...the timer will continue to execute no matter what until it is finished...which can be dangerous to processing drain if its an "Every" timer rule. (which is in line with what FMG is saying)

    Clean and efficient rule structures are key to optimizing performance.
  • firemaplegamesfiremaplegames Member Posts: 3,211
    Run to completion will make sure that the Timer finishes, even if the condition is no longer valid. Like synthesis said, do not use it with an "every" Timer. Here are two examples:

    Rule
    When Touch is Pressed
    Timer
    After 3 seconds
    Change Attribute self.hasbeenPressed To TRUE

    Rule
    When Touch is Pressed
    Timer
    After 3 seconds Run To Completion
    Change Attribute self.hasbeenPressed To TRUE

    In the first scenario, if I remove my finger from the button before 3 seconds, the Timer will not fire.
    Whereas it will in the second scenario...
Sign In or Register to comment.