How to set Random event by a random timer

Pkirkby

Member
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.
 

Kyon

Member
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
 

YoSniper

Member
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();
 

Pkirkby

Member
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
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?
 

Sk8dududu

Member
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?
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.
 

Pkirkby

Member
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.
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!
 
B

Blan

Guest
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.
 

Sk8dududu

Member
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.
If you animate it in code you get a new image every step. So it's technically more images. While also saving data.
 

Pkirkby

Member
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.
Hmm, I didn't know you could do an animation disappear, how would you go about coding something like this?
 

Pkirkby

Member
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
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.
 

Kyon

Member
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.
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)
 

Pkirkby

Member
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)
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!
 
Top