How to set Random event by a random timer

Discussion in 'Programming' started by Pkirkby, Jun 13, 2019.

  1. Pkirkby

    Pkirkby Member

    Joined:
    Mar 25, 2019
    Posts:
    115
    Hey everyone, essentially I have a light in my game that flickers to a different alpha:

    image_alpha = random_range(0.35,.5);
    draw_self();

    However, it just constantly flickers, and is too fast. I'm trying to find a simple way to randomize WHEN it flickers, so that it only happens every 2-6 seconds or something like that?

    I've tried setting up some sort of random alarm, but I'm not able to figure it out, any suggestions? Thanks in advance.
     
  2. Sk8dududu

    Sk8dududu Member

    Joined:
    Aug 14, 2017
    Posts:
    189
    alarm_set(0,irandom_range(60,180));
     
  3. Pkirkby

    Pkirkby Member

    Joined:
    Mar 25, 2019
    Posts:
    115
    Okay, so now how do I incoorporate my random flicker to be affected by that?
     
  4. Kyon

    Kyon Member

    Joined:
    Jun 27, 2016
    Posts:
    467
    CREATE
    Code:
    time=0;
    
    STEP
    Code:
    if time>0{time-=1;}
    else{
    time=random_range(40,180);
    image_alpha=.5;
    }
    
    if image_alpha>.35{
    image_alpha-=.01;
    }
    


    Something like this would do the trick
     
  5. YoSniper

    YoSniper Member

    Joined:
    Jun 24, 2016
    Posts:
    422
    I'm not a huge fan of alarms, so I'll just write code that I would use:

    Create Event
    Code:
    time_to_flicker = random_range(2, 6) * room_speed; //Flicker will happen after some time between 2 and 6 seconds
    flicker_duration = random_range(1, 2) * room_speed; //Flicker will last between 1 and 2 seconds
    flicker_timer = 0;
    Draw Event
    Code:
    if flicker_timer < time_to_flicker {
        image_alpha = 0.5;
    } else if flicker_timer < time_to_flicker + flicker_duration {
        image_alpha = random_range(0.35, 0.5);
    } else {
        time_to_flicker = random_range(2, 6) * room_speed; //Flicker will happen after some time between 2 and 6 seconds
        flicker_duration = random_range(1, 2) * room_speed; //Flicker will last between 1 and 2 seconds
        flicker_timer = 0;
    }
    draw_self();
     
  6. Sk8dududu

    Sk8dududu Member

    Joined:
    Aug 14, 2017
    Posts:
    189
    Literally just put that in the alarm event and it will keep activating forever. Every 2 - 6 seconds.
     
  7. Pkirkby

    Pkirkby Member

    Joined:
    Mar 25, 2019
    Posts:
    115
    Great, that worked perfect. I'm going to try to make the flicker a little smoother, so it fades instead of flickers I guess. Any thoughts to point me in the right direction?
     
  8. Sk8dududu

    Sk8dududu Member

    Joined:
    Aug 14, 2017
    Posts:
    189
    Alright, so if your sprite just 1 subimage? The entire sprite blinking as a light source?
    Then you can put fade = 0; in the create event, and in the alarm event fade = 1;
    Then in the step event put
    Code:
    if fade == 1 and image_alpha > 0.35
        {
        image_alpha -= 0.05;
        }
    else
        {
        fade = 0;
        if image_alpha < 1
            {
            image_alpha += 0.05;
            }
        }
    
    That will make it quickly fade out and then instantly back in once it reaches the 35% mark.
    That is how I would do it anyway.
    Pretty easy to understand the code, maybe it's not the best way, but it's better if you're able to read your own code..
    So the alarm resets it self, and it also triggers the fade sequence.
    you can just adjust the "0.05" for how quickly you want the image to fade once the fade has started. And change the alarm code to change how often it flickers.
     
    Blan likes this.
  9. Pkirkby

    Pkirkby Member

    Joined:
    Mar 25, 2019
    Posts:
    115
    Great, I'm going fool around with it this weekend. I'm still learning obviously, but these forums are the best way for me to make sense of the manual entries. Thanks!
     
    Blan and woods like this.
  10. Blan

    Blan Member

    Joined:
    Jun 26, 2016
    Posts:
    25
    Normaly i would make 1 sprite, then i would use the sprite editor and select animation disappear, then enter amount of images(10), in your object set image_speed to cycle through at required speed.0.25 is like car direction indicator(at room speed 30).
    More images is smoother.
     
  11. Sk8dududu

    Sk8dududu Member

    Joined:
    Aug 14, 2017
    Posts:
    189
    If you animate it in code you get a new image every step. So it's technically more images. While also saving data.
     
  12. Pkirkby

    Pkirkby Member

    Joined:
    Mar 25, 2019
    Posts:
    115
    Hmm, I didn't know you could do an animation disappear, how would you go about coding something like this?
     
  13. Pkirkby

    Pkirkby Member

    Joined:
    Mar 25, 2019
    Posts:
    115
    Hey man, this code worked great for me, however I'm having difficulty understand exactly how it works. I want to have it control the alpha of my shadows, so that when the flicker happens, my shadow_alpha gets lower. I was going to put in a global variable to tie it back to my oLighting for when it dips under a certain image_alpha number, but I can't seem to figure out what number is default and what it drops to with these if statements.
     
  14. Kyon

    Kyon Member

    Joined:
    Jun 27, 2016
    Posts:
    467
    That's great that you ask for an explanation instead of using it without understanding it.
    Ok let me try to explain;

    In the CREATE event we make a variable called "time". The value is 0.

    Then in the STEP event make it so that when time is higher than 0, we count down to 0. so:
    Code:
    if time>0{time-=1;}
    Now if it IS 0:
    Code:
    else{
    time=random_range(40,180);
    image_alpha=.5;
    }
    
    We set variable "time" to a random number between 40 and 180. So time=random_range(40,180);
    We set our image_alpha. Which is the default variable in an object to make it transparent to 0.5, so 50% transparent. (is use .5 it's shorter)


    Then I added some sort of "smoothener" to make the image alpha smoothly go back to it's origin 0.35 (which can be any number, but I got this number from your earlier post)

    Code:
    if image_alpha>.35{
    image_alpha-=.01;
    }
    
    So IF the image alpha is higher than 0.35, decrease the image alpha by 0.01 every step.
    So we set the image alpha every time to 0.50, and then it automaticaly decreases to 0.35.



    So right now, default is 0.35 image alpha (which is barely visible) and it jumps to 0.50 every random amount of time (between a 1/2th of a second to 3 seconds or so)
     
  15. Pkirkby

    Pkirkby Member

    Joined:
    Mar 25, 2019
    Posts:
    115
    That really helps, thanks for taking the time to do that. I'm going to go fart around and see what I can do now that it makes sense. Thanks alot!
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice