Hey everyone,
I am very new to game dev (under a week of using gamemaker) and ive been working on a simple platformer game. I wanted to add dialog into the game so I made an NPC called "dummy", which triggers dialog once the player is less than 32 pixels away from them and press the F key.
As well as adding dialog, I wanted to add a shader to the NPC to give it a dynamic outline that changes colour once the player is within the interaction distance, going from a black outline to a white one. I followed Sara Spaulding's tutorial on outline shaders from years ago and miraculously it worked completely fine. But now I'm at the point where I want to add the ability for the shader to change colours. I tried doing it myself but got confused so tried to look elsewhere for help, but I can't find anything on the topic, at least nothing that works with my code.
Here's the code:
sh_outline Fragment Shader
// Simple passthrough fragment shader
//
varying vec2 v_vTexcoord;
varying vec4 v_vColour;
uniform float pixelH;
uniform float pixelW;
void main()
{
//figuring out the size of a pixel
vec2 offsetx;
offsetx.x = pixelW;
vec2 offsety;
offsety.y = pixelH;
vec4 texColour = texture2D(gm_BaseTexture, v_vTexcoord);
//figuring out pixels location on the sprite
float alpha = texColour.a;
float original_alpha = alpha;
alpha += ceil(texture2D(gm_BaseTexture, v_vTexcoord + offsetx)).a;
alpha += ceil(texture2D(gm_BaseTexture, v_vTexcoord - offsetx)).a;
alpha += ceil(texture2D(gm_BaseTexture, v_vTexcoord + offsety)).a;
alpha += ceil(texture2D(gm_BaseTexture, v_vTexcoord - offsety)).a;
if (alpha > 1.0) alpha = 1.0; //claim alpha
if (original_alpha != alpha) {
gl_FragColor = vec4(0.1529,0.1529,0.1529,1.0);
}
else {
gl_FragColor = texColour;
}
gl_FragColor.a = alpha;
}
obj_dummy Create Event
//Outline Shader
uPixelH = shader_get_uniform(sh_outline,"pixelH");
uPixelW = shader_get_uniform(sh_outline,"pixelW");
texelW = texture_get_texel_width(sprite_get_texture(sprite_index,0));
texelH = texture_get_texel_height(sprite_get_texture(sprite_index,0));
//Default Dialog
text = [
"Default Text",
]
obj_dummy Draw Event
//Outline Shader
shader_set(sh_outline);
shader_set_uniform_f(uPixelW,texelW);
shader_set_uniform_f(uPixelH,texelH);
draw_self();
shader_reset();
obj_player Step Event
//dialogue initiation
if (keyboard_check_pressed(ord("F"))) {
if (currently_talking == noone){
var check_x = x + (32 * moveDir);
var who_is_here = instance_place(check_x,y,obj_dummy);
if who_is_here != noone {
audio_play_sound(sfx_interact,1,false);
current_text = (who_is_here.text[0]);
currently_talking = who_is_here;
current_text_index = 0;
current_text_line_number = 0;
instance_create_depth(x,y,1,obj_pauser);
}
} else {
if (current_text_line_number < array_length(currently_talking.text) - 1) {
audio_play_sound(sfx_interact,1,false);
current_text_line_number++;
current_text = currently_talking.text[current_text_line_number];
current_text_index = 0;
} else {
currently_talking = noone;
instance_destroy(obj_pauser);
}
}
}
Basically, the code checks if the F key has been pressed, then if the player is in proximity to the NPC at the time of the key press, dialog initiates. Im thinking I should swap around the keypress and whoishere if statements so that whoishere is being detected constantly, then when the player detects theres an NPC infront of them they send a message to the dummy that tells the shader to change the outline colour to white. Only, thats the bit where I'm stumped.
I'd really appreciate any help I can get on this. Definitely dont want to use AI for coding advice.
Thanks!