How do you do, every X...

Quick question. How do you do a line of code that says every x do this.
Example: lets says i want my integer attribute to say "every interval of 5 do xyz." Its not the xyz portion thats the problem, its the interval that i'm stuck on.
In case i'm still confusing anyone, heres another example:
Lets say i want to pop an ad up every 4 times a player hits the play game button. I would obviously need a line of code that says every interval of 4 show ad.
Thanks a million!
P.S. I realize i could just make a counter for that example that counts to 4 and then resets to 0, but thats not what i'm looking for. I want something to keep counting every instance of a number (like every 4).

Comments

  • beefy_clyrobeefy_clyro Member Posts: 5,394
    You'd need two attributes;

    1. Number of touches.
    2. 4th touch activated.

    When touch is pressed changed attribute 1 to attribute 1+1
    When touch = 4 change attribute 2 to 1 or true (depending on attribute used)

    Perform action now wanted.

    Then have a rule that then resets both attributes back to 0 so it can loop again.
  • gamesfuagamesfua Member Posts: 723
    Thanks i appreciate that, but i still dont think thats what im looking for. Its a wonderful work around and i appreciate it. Im still looking for a line of code that has the ability to count every x amount of times. It has to exist. At least im hoping. Anyways thanks again, really appreciate it.
  • beefy_clyrobeefy_clyro Member Posts: 5,394
    I dont think you're going to find that, i've been using GS for 3 years now and never seen that! The only 'every' statement you have is the timer but that only does time obviously and not touches. There maybe other workarounds but thats the easiest and simplest one I could think of, off the top of my head.
  • gamesfuagamesfua Member Posts: 723
    Instead of having two attributes just make 1. Every time touch (or whatever) happens add 1 to counter. When counter equals 4 do x and change counter back to zero. Same thing no? Less code. Perhaps youre right. Perhaps theres no way to do what i was hoping for. I appreciate your time beefy! Thank you again!
  • beefy_clyrobeefy_clyro Member Posts: 5,394
    No worries :) Yeh 1 line would work fine in fairness, I was saying use 2 as that second attribute could be a game attribute, therefore you may want to trigger multiple actions across multiple actors.

    i.e. self counter = 4
    game 4thTouch = true

    actor 1

    when

    game 4thTouch = true
    Do .....

    actor 2

    when

    game 4thTouch = true
    Do .....

    It all depends on whether you want just 1 actor to do something or multiples.
  • gamesfuagamesfua Member Posts: 723
    Much abliged! Seems like a perfectly good solution. Thanks again :)
  • MarkOnTheIronMarkOnTheIron Member Posts: 1,447
    In a case like this I usually use a method like this:

    1. Create an integer attribute (it can also be a self attribute if all your rules will be on a single actor)

    2. In your actor of choice add this:
    - Rule: When touch is pressed
    -- Change attribute: game.attribute to (game.attribute+1)%4

    3. Then add this:
    - Rule: When game.attribute = 0
    -- Do whatever you want


    With this method you don't have to reset your game attribute every time, it will be done automagically. To change the number of touches you need for your something to happen just change the number after the "%".

  • beefy_clyrobeefy_clyro Member Posts: 5,394
    This will only work, if the only actor that depends on knowing its the 4th press is the actor being touched itself. Other actors will not know that it has been pressed 4 times.

    If its only that actor that needs to know then using the % is def the way to go as it saves on the resetting rule :)
  • MarkOnTheIronMarkOnTheIron Member Posts: 1,447
    If you want other actor to see it then use a game.attribute instead of a self.attribute.

    By using the "%" system the attribute will automatically reset itself to 0 once it reaches the number after the "%". So you just have to check when that attribute reaches 0 and you're good to go, regardless of what actor the "%" rule is placed.

  • beefy_clyrobeefy_clyro Member Posts: 5,394
    That is true, completely overlooked that ha, love the simple answers!

    Use a game attribute, it will start on 0, it then counts 1, 2, 3 and 4 and resets back to 0. Run your rule once the value = 4.
  • MarkOnTheIronMarkOnTheIron Member Posts: 1,447
    edited January 2013
    Something like that. However to avoid confusion it would be better to run the rule when the attribute reaches 0. For example in my example above the attribute will reach 3 and then revert back to 0 even though you used 4 after the "%".

    4 is the value at which a %4 module reset:
    %2 -> 0, 1, 0, 1, 0, 1, ...
    %3 -> 0, 1, 2, 0, 1, 2, ...
    %4 -> 0, 1, 2, 3, 0, 1, ...
    %5 -> 0, 1, 2, 3, 4, 0, ...
    and so on.

  • beefy_clyrobeefy_clyro Member Posts: 5,394
    Bingo .. You are 100% correct. If the rule runs on 0 though, it will run straight away without any touches at all as it will start on 0! Obviously there are ways around this, it all depends on how you are using the actor, what the rule is etc.
  • MarkOnTheIronMarkOnTheIron Member Posts: 1,447
    The way around this is to set the attribute at 1 to begin with.

  • beefy_clyrobeefy_clyro Member Posts: 5,394
    Then in this case it wouldn't work! If you start on 1, and trigger every time on the 0 then it will be as follows;

    2,3,0,1 - So every 4th press will be on 1. If you keep your rule to trigger on 0 it will incorrectly trigger as it will be after 3 presses when it needs to be 4. If you keep it at 0, it will trigger straight away the 1st time without any presses, after this it would be correct but 1st time only it would trigger when not needed. Again, you could build a work around to this.
  • gamesfuagamesfua Member Posts: 723
    Whoa this ble up quick hehe. Can someone explain what this whole percentage is about? Thanks!
  • beefy_clyrobeefy_clyro Member Posts: 5,394
    Haha nah nothing blowing up here :)

    Basically the percentage used in the way @MarkOnTheIron suggested means the change attribute will cycle between a pre-defined number of options.

    So when Mark said - Create a rule that when touched - Change attribute: game.attribute to (game.attribute+1)%4

    This makes a game attribute you created every time you press it, go up like this;

    0,1,2,3 and upon next touch, will start back at 0 ... so, another cycle of 0,1,2,3, it will continuously loop as 0,1,2,3,0,1,2,3 each time it is pressed (in increments of +1 to each touch).

    So the number after the % symbol is determining how many changes the attribute has;

    %2 -> 0, 1, 0, 1, 0, 1, ...
    %3 -> 0, 1, 2, 0, 1, 2, ...
    %4 -> 0, 1, 2, 3, 0, 1, ...
    %5 -> 0, 1, 2, 3, 4, 0, ...

    See how they loop when they reach their maximum number of options you specify after the % symbol, make sense now?
  • SocksSocks London, UK.Member Posts: 12,822
    The way around this is to set the attribute at 1 to begin with.

    -1
  • beefy_clyrobeefy_clyro Member Posts: 5,394
    That would sort of work fine @Socks

    It would start as -1, on 1st touch it would change to 0, 2nd to 1, 3rd to 2, 4th to 3 and then repeat. So if you changed the main rule to activate when the game attribute equalled 3 that would work
  • SocksSocks London, UK.Member Posts: 12,822
    edited January 2013
    That would sort of work fine @Socks

    It would start as -1, on 1st touch it would change to 0, 2nd to 1, 3rd to 2, 4th to 3 and then repeat. So if you changed the main rule to activate when the game attribute equalled 3 that would work

    Yep, like this:


    The counting rule: Change self.count to (self.count+1)%4

    And start the attribute on -1

    http://www.mediafire.com/?l6c62cjn7jvoc5h
  • beefy_clyrobeefy_clyro Member Posts: 5,394
    Yup thats the one .. love the wobbly 4th touch
  • SocksSocks London, UK.Member Posts: 12,822
    Yup thats the one .. love the wobbly 4th touch
    You pay extra for top quality graphics like that.

    ; P
  • gamesfuagamesfua Member Posts: 723
    I'm still away from my computer but can't wait to check this all out. Thanks so much!
  • gamesfuagamesfua Member Posts: 723
    Hey everyone just wanted to say thanks for everything. And @socks I checked out your demo you made, THANK YOU! Makes perfect sense now! Thanks everyone, awfully kind of you :)
Sign In or Register to comment.