Flashlight not staying on player

M

Monster25

Guest
Hello! I have a simple surface darkening everything and I have a flashlight sprite going form the player with bm_add. Thing is, everything looks perfect but the flashlight is drawn on a different position rather than the player's position and it moves at different speeds as well. I am using views that follow the player. Here is the draw event of the player where everything lighting based happens. (I create the surf in the create event)

//Lighting
surface_set_target(surf);
draw_clear(c_black);

draw_set_blend_mode(bm_add);
draw_sprite_ext(spr_flashlight,0,x- view_xview[0], y-view_yview[0],1,1,player_angle,c_white,1);
draw_set_blend_mode(bm_normal);

surface_reset_target()

if (surface_exists(surf))
{
draw_set_blend_mode_ext(bm_dest_color, bm_inv_src_alpha)
draw_surface_ext(surf,0,0,1/surfscale,1/surfscale,0,c_white,0.85)
draw_set_blend_mode(bm_normal);
}
else
surf = surface_create(room_width,room_height);

This is my only problem, the sprite gets drawn at a different position. I tried working around this with having the sprite being drawn in another object and updating that object's x and y to match the player through the draw event but the resulting flashlight is a lot weaker than this one.
 

Perseus

Not Medusa
Forum Staff
Moderator
Since the surface is as large as the room, you don't need to subtract view coordinates as the instance's position would be where the sprite should get drawn in the first place. Also, I don't think you need a surface that large. Just make it equal to the view size, draw the sprite the way you're doing it right now (position - view position) then draw the surface at view coordinates.
 
M

Monster25

Guest
Thank you for the fast reply. I did that but I narrowed the problem down, the flashlight sprite gets drawn inside the view parameters (top left corner) and stays there, the surface gets drawn on top of my moving player and view but for some reason the flashlight sprite is drawn on the top left corner.
The sprite drawing behaves like I would put view_wview[0] and view_hview[0] in its coordinates instead of x-view_xview[0] and y-view_yview[0]. I am not sure why it does that.
 

Perseus

Not Medusa
Forum Staff
Moderator
What does the new code setup look like? Without having a look at that, it would be difficult to tell what the problem is.
 
M

Monster25

Guest
This is the updated code with what you provided.

//Lighting
surface_set_target(surf);
draw_clear(c_black);

draw_set_blend_mode(bm_add);
draw_sprite_ext(spr_flashlight,0,x-view_xview[0],y-view_yview[0],1,1,player_angle,c_white,1);
draw_set_blend_mode(bm_normal);

surface_reset_target()

if (surface_exists(surf))
{
draw_set_blend_mode_ext(bm_dest_color, bm_inv_src_alpha)
draw_surface_ext(surf,0,0,1/surfscale,1/surfscale,0,c_white,0.85)
draw_set_blend_mode(bm_normal);
}
else
surf = surface_create(view_xview[0],view_yview[0]);
 
M

Monster25

Guest
If I use an intermediate object and split the code into 2, one for the flash light and one for the dark surface it works fine but the light is so dim, even if I use depth to put the flashlight on top
 

Perseus

Not Medusa
Forum Staff
Moderator
You're not doing what I told you to do. Read what I said again:

Since the surface is as large as the room, you don't need to subtract view coordinates as the instance's position would be where the sprite should get drawn in the first place. Also, I don't think you need a surface that large. Just make it equal to the view size, draw the sprite the way you're doing it right now (position - view position) then draw the surface at view coordinates.
You're creating a surface with a size equal to the view coordinates and drawing it to the top-left corner of the room. In fact, the rest of your code is a bit messed up as well. Instead of creating the surface in the Create event, you could create it directly in the Draw event if it doesn't exist. Set the value of surf to -1 in the Create event and do this before dealing with it:

Code:
if (!surface_exists(surf)) {
    surf = surface_create(view_wview[0], view_hview[0]);
}
surface_set_target(surf);
draw_clear(c_black);
draw_set_blend_mode(bm_add);
draw_sprite_ext(spr_flashlight, 0, x - view_xview[0], y - view_yview[0], 1, 1, player_angle, c_white, 1);
draw_set_blend_mode(bm_normal);
surface_reset_target();

draw_set_blend_mode_ext(bm_dest_color, bm_inv_src_alpha);
draw_surface_ext(surf, view_xview[0], view_yview[0], 1, 1, 0, c_white, 0.85);
draw_set_blend_mode(bm_normal);
I have removed the surface scaling part, for I was not sure what you were trying to do with that. From what I can tell, it's not needed. What you have described can be done using the aforementioned setup. But if it's not so, please feel free to let me know what the purpose of that part was.
 
M

Monster25

Guest
Oh my god it actually worked! Thank you so much!
I believe the scaling was a remnant of an old code I used back when I tried to do the same but have lights as well, I took it as if it was part of the same code but it wasn't. The lighting is perfect now thank you so much <3
 
Top