On/Off Button - Tutorial Question

DrGlickertDrGlickert Member Posts: 1,135
edited November -1 in Working with GS (Mac)
In TSB's video, he uses a game.music integer attribute.

Then, he has a rule that when touch is pressed on the "On/Off button actor";
Change attribute game.music to (game.music+1)%2

When game.music = 0 .......

My question is;
Is it necessary to have the game.music attribute and structure the On/Off button like that? I have an On/Off button in my game for music and sound but I just made a simple rule, When touch is pressed change attribute game.music to 0 otherwise change attribute game.music to 1...

What's the best way for performance?

Comments

  • IsabelleKIsabelleK Member, Sous Chef Posts: 2,807
    Hi Doc!
    I use the rule you described (when touch is pressed on the "On/Off button actor";Change attribute game.music to (game.music+1)%2), in many places in my games (not only for music), and it works great (it doesn't affect performance at all).

    It the second way you described, you can have some problems - because it will change your attribute everytime when the button is released. So if 1 means "Play the music" and 0 means "Stop the music", your music will stop only when you have this actor pressed, and when you realese it, the music will play again.
  • LumpAppsLumpApps Member Posts: 2,881
    Is this modulus used here the same as change attribute to 1-attribute?
    Or is this not as good as Change attribute game.music to (game.music+1)%2)
    It switches from 1 to zero and 0 to 1 too.

    ??
  • tenrdrmertenrdrmer Member, Sous Chef, Senior Sous-Chef Posts: 9,934
    Modulus is exrtemely useful and has many uses not just a simple on off switch. To expand on what john said i'll use the tutorial for my first example. (game.music+1)%2 means when the attribute reaches 2 it instead switches to 0. So now lets take that further. Change it to (game.music+1)%10 so now everytime the attribute is changed it cycles through 0-9. Say you want an actor to change images each time a button is pressed and you only want it to be 5 certain images and you want it to always cycle. Set you actor to display an image based on the value of game.imageselect and then in your button you have to options for rule sets.

    Option 1

    When touch is pressed
    --Change attribute game.imageselect to game.imageselect+1

    When Attribute game.imageselect > 4
    --Change attribute game.imageselect to 0

    Or…

    Option 2

    When touch is pressed
    --Change attribute game.imageselect to (game.imageselect+1)%5

    Add your done.

    Obviously Option 2 is one less rule which means less memory used by the Game Engine. I know it may not be much but small performance improvements like this can through your game can really benefit things. Not to mention its one less rule for you to create.

    Dr the rule you listed you are using instead of TSB's tutorial looks like it will switch the music to off when pressed but it also switches back on as soon as you release because the behaviors you place in the otherwise will instantly run as soon as the rules conditions become invalid.

    Hope that helps you guys

    Aaron
    ___________________________________________________________________________________
    TEMPLATES AND PROJECT HELP BY TENRDRMER. CLICK HERE!!!

    AppSolute Entertainment on Facebook
    AppSolute Entertainment on iTunes
Sign In or Register to comment.