• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

SOLVED I need to make the character blink after taking damage using a shader

P

Purple_Shy_Guy

Guest
Hello everyone, I'm working on my first project and I need some help. I wanted to make the character blink (in white) during his invincibility time after taking damage. I created a shader for it and it kinda works. The only problem is that the character becomes white during the whole invincibility time, and don't blink. I don't really know how to do it, since I'm still learning lots of things. These are the codes I'm using:


Sh_Flash
GML:
varying vec2 v_vTexcoord;
varying vec4 v_vColour;

void main()
{
    vec4 base_color = v_vColour * texture2D( gm_BaseTexture, v_vTexcoord );
    base_color.rgb = vec3(1.0);
    gl_FragColor = base_color;
}
Obj_Player
Create Event
Code:
cooldown = 0;
blink = false;
Step Event
Code:
if cooldown > 0 {
    blink = true;
} else {
    blink = false;
}
the variable cooldown is the one I'm using to make the character invincible after taking damage, which works perfectly, so I don't think I should write its code here too, since that's not the point. But if that would help, let me know.


Draw Event
Code:
if (blink) {
    shader_set(Sh_Flash);
    draw_self();
    shader_reset();
} else {
    draw_self();
}
I know this code won't make the character blink, but I don't know what to add here to make it happens, and that's why I'm asking for help.
 

SoapSud39

Member
If you want the character to blink, you have to alternate between setting and not setting the shader. So what you can do is write something that will give 'blink' true or false in the above step event code. One way to achieve this is with a combination of mod and div. e.g.
GML:
//step event
if cooldown > 0 {
    blink = (cooldown div 3) mod 2;
}
With this code, 'blink' will alternate between true and false every three frames (you can change the number after 'div' depending on how fast you want it to flash white).
(I don't know how familiar you are with div and mod, but if not, look them up to understand what the code is doing here)
 
P

Purple_Shy_Guy

Guest
If you want the character to blink, you have to alternate between setting and not setting the shader. So what you can do is write something that will give 'blink' true or false in the above step event code. One way to achieve this is with a combination of mod and div. e.g.
GML:
//step event
if cooldown > 0 {
    blink = (cooldown div 3) mod 2;
}
With this code, 'blink' will alternate between true and false every three frames (you can change the number after 'div' depending on how fast you want it to flash white).
(I don't know how familiar you are with div and mod, but if not, look them up to understand what the code is doing here)
Thanks for your help! This code really worked perfectly! I'll definetly search more about div and mod later!
 
P

Purple_Shy_Guy

Guest
Simplest explanation:
- div performs division with no remainder
- mod (modulo) performs division but only returns the remainder

Also, modulo has its own operator: %
x mod 2 == x % 2
Ohh, thanks for the explanation, I think I got it how they work, and I'll search more about it!
 
Top