Delete draw_sprite_ext

F

floShio

Guest
I'm making a shadow appear on my character when it's morning/daytime in my game, and this is the code I have...

if global.daytime = 1
draw_sprite_ext(sprite_index, 0, x - 16, y + 20, 1, 1, 0, c_black, 0.2);
draw_self();

But I want it to slowly fade away when it turns night time (global.daytime = 2)
and slowly fade back when it's daytime! Help??
 

FrostyCat

Redemption Seeker
You need another variable that changes gradually from 0 (day) to 1 (night) during that phase, then you can use lerp() to scale the opacity.

Code:
if (global.daytime == 2) {
  draw_sprite_ext(sprite_index, 0, x-16, y+20, 1, 1, 0, c_black, lerp(0.2, 0, global.transition_progress));
}
 
F

floShio

Guest
I have morning, which is daytime = 0.5. I tried that code, and it doesn't work in my draw, or step event :s
 
F

floShio

Guest
You need another variable that changes gradually from 0 (day) to 1 (night) during that phase, then you can use lerp() to scale the opacity.

Code:
if (global.daytime == 2) {
  draw_sprite_ext(sprite_index, 0, x-16, y+20, 1, 1, 0, c_black, lerp(0.2, 0, global.transition_progress));
}
I have morning, which is daytime = 0.5. I tried that code, and it doesn't work in my draw, or step event :s
 

FrostyCat

Redemption Seeker
Do you have that second variable I described, and are you transitioning its values between 0 and 1 as I instructed? In the morning it should transition gradually from 1 to 0, and at dusk from 0 to 1. Do you understand what my code is trying to do?

Also, did anyone teach you not to use exact comparisons with floating point decimals? Use another whole number to represent morning.
 
F

floShio

Guest
Do you have that second variable I described, and are you transitioning its values between 0 and 1 as I instructed? In the morning it should transition gradually from 1 to 0, and at dusk from 0 to 1. Do you understand what my code is trying to do?

Also, did anyone teach you not to use exact comparisons with floating point decimals? Use another whole number to represent morning.
I'm a beginner at GMS, I don't really understand your code.
 
F

floShio

Guest
Do you have that second variable I described, and are you transitioning its values between 0 and 1 as I instructed? In the morning it should transition gradually from 1 to 0, and at dusk from 0 to 1. Do you understand what my code is trying to do?

Also, did anyone teach you not to use exact comparisons with floating point decimals? Use another whole number to represent morning.
I have 0.5 for morning, 1 for day and 2 for night. What does my night cycle have to do with anything?
 
B

Blaze

Guest
Why not have a variable called shadow_alpha and use draw_set_alpha(shadow_alpha) before drawing the shadow (make sure you set draw_set_alpha back to 1 before drawing self).
The variable shadow_alpha can start off at 0 in night time and then increment 0.1 amounts until it reaches 1, e.g:

// Alarm 0 event

If (shadow_alpha<1)
{
shadow_alpha+=0.1;
alarm[0]=1;
}
 

FrostyCat

Redemption Seeker
If you don't understand my code, why aren't you helping yourself by reading the Manual?

You don't understand what lerp() does? Read the Manual entry on lerp().

You don't understand why I put lerp() there? Read the Manual entry on draw_sprite_ext() and note which argument it was.

I also asked you to stop using 0.5 for morning because non-whole numbers have precision issues when compared with ==. Stop doing that. Use 0 or another whole number, or even better, use a Macro or an enum.

Put this where global.daytime is declared (assuming that it starts in daytime):
Code:
global.transition_progress = 0;
In the Step event, gradually change this variable to 1 in the morning and back to 0 at night:
Code:
switch (global.daytime) {
  case 0: //This assumes you are now using 0 for morning as you should be
    if (global.transition_progress > 0) {
      global.transition_progress -= 1/room_speed;
      if (global.transition_progress <= 0) {
       global.transition_progress = 0;
      }
    }
  break;
  case 2:
    if (global.transition_progress < 1) {
      global.transition_progress += 1/room_speed;
      if (global.transition_progress >= 1) {
       global.transition_progress = 1;
      }
    }
  break;
}
Then use the same Draw event code with the global.daytime condition removed.
 
F

floShio

Guest
If you don't understand my code, why aren't you helping yourself by reading the Manual?

You don't understand what lerp() does? Read the Manual entry on lerp().

You don't understand why I put lerp() there? Read the Manual entry on draw_sprite_ext() and note which argument it was.

I also asked you to stop using 0.5 for morning because non-whole numbers have precision issues when compared with ==. Stop doing that. Use 0 or another whole number, or even better, use a Macro or an enum.

Put this where global.daytime is declared (assuming that it starts in daytime):
Code:
global.transition_progress = 0;
In the Step event, gradually change this variable to 1 in the morning and back to 0 at night:
Code:
switch (global.daytime) {
  case 0: //This assumes you are now using 0 for morning as you should be
    if (global.transition_progress > 0) {
      global.transition_progress -= 1/room_speed;
      if (global.transition_progress <= 0) {
       global.transition_progress = 0;
      }
    }
  break;
  case 2:
    if (global.transition_progress < 1) {
      global.transition_progress += 1/room_speed;
      if (global.transition_progress >= 1) {
       global.transition_progress = 1;
      }
    }
  break;
}
Then use the same Draw event code with the global.daytime condition removed.
isn't there a much more simple method? this method seems to work around things much more complicated than needed.
 

FrostyCat

Redemption Seeker
Then why don't you trace through the code yourself and see if it's really more complicated than what you need?

You said you wanted a gradual transition, I gave you exactly that. Keeping track of how far along the fade would inevitably be part of the deal, don't tell me you don't know that.

You have two distinct behaviours, I gave you two distinct behaviours the second time. The first time I gave you the night-bound portion, and there you go running back to me for more.

You asked for non-basic behaviour, you got non-basic code. Man up, trace through that code yourself and work the code to your own liking.
 
F

floShio

Guest
Then why don't you trace through the code yourself and see if it's really more complicated than what you need?

You said you wanted a gradual transition, I gave you exactly that. Keeping track of how far along the fade would inevitably be part of the deal, don't tell me you don't know that.

You have two distinct behaviours, I gave you two distinct behaviours the second time. The first time I gave you the night-bound portion, and there you go running back to me for more.

You asked for non-basic behaviour, you got non-basic code. Man up, trace through that code yourself and work the code to your own liking.
I came on this forum to get helped, not bullied.
Thanks anyway.
 

Weird Dragon

Wizard
GMC Elder
! ! ! Please be patient with new people, well actually behave well with anyone. ! ! !

I think that the manual's description on the lerp() function can be quite difficult to understand if you are not used to the way programmers speak.

***
Anyway, why not simply use the inbuilt variable image_alpha ?
Then the code in the Draw Event could be this:

Code:
draw_sprite_ext(sprite_index, 0, x - 16, y + 20, 1, 1, 0, c_black, image_alpha);
That is all the code you need in the draw event, you change the value of image_alpha somewhere else dependning on global.daytime; image_alpha should have a value in the range 0 to 1.
I agree with FrostyCat that it makes sense to set global.daytime to 0 on mornings, 1 on day and 2 on night.

In the Create Event you could have:

Code:
global.daytime = 0; // you might initialize global.daytime in another object and not here
image_alpha = 0; //that is morning
In Step Event you could have:

Code:
switch (global.daytime)
{
    case 0: image_alpha = 0; break;
    case 1: if image_alpha < 0.2
        {
            image_alpha += 0.002; break;
        }
    case 2: if image_alpha > 0
        {
            image_alpha -= 0.002; break;
        }
}
If you want image_alpha bigger than 0.2 you can change the value. Above code will set the sprite to 100% transparent in mornings, it will increase up to 0.2 in daytime and fade away in night time.
 
Last edited:

lolslayer

Member
I would just use the timer trick Blaze suggested

Here is a little tutorial:

  • Create a new object called: "obj_daynight"
  • Put this piece of code in the create event:

Code:
global.intensity = 0;
fase = 0;
alarm[0]=10
  • Put this piece of code in the alarm 0 event:

Code:
if global.intensity == 1{
fase = 1;
}
if global.intensity == 0{
fase = 0;
}

if fase == 0{
global.intensity += 0.01;
}
if fase == 1{
global.intensity -= 0.01;
}

alarm[0] = 10;
  • Use the global.intensity value in your draw_ext call like this:

Code:
draw_sprite_ext(sprite_index, 0, x - 16, y + 20, 1, 1, 0, c_black, global.intensity);
This will give you a really quick day/time cycle, but you get the idea, just play around with the variables to change it to your likings. The shadows will increase and decrease in density over time now.

The easiest thing you can do now to get a full day/night cycle to work, is by drawing a completely black square filling the whole screen, the alpha value of the square should be equal to: 1-global.intensity

The code will look somewhat like this: (don't forget to give the object that draws this rectangle will have a really low depth, like -10000, this way it will draw above everything else)

Code:
draw_set_alpha(1-global.intensity);
draw_rectangle(0,0,room_width,room_height,false);
draw_set_alpha(1);
If the rectangle is drawn white instead of black, use this code:

Code:
draw_set_alpha(1-global.intensity);
draw_set_colour(c_black);
draw_rectangle(0,0,room_width,room_height,false);
draw_set_colour(c_white);
draw_set_alpha(1);
Well, I can't really make this tutorial more detailed than it already is, so good luck with this!
 
F

floShio

Guest
I would just use the timer trick Blaze suggested

Here is a little tutorial:

  • Create a new object called: "obj_daynight"
  • Put this piece of code in the create event:

Code:
global.intensity = 0;
fase = 0;
alarm[0]=10
  • Put this piece of code in the alarm 0 event:

Code:
if global.intensity == 1{
fase = 1;
}
if global.intensity == 0{
fase = 0;
}

if fase == 0{
global.intensity += 0.01;
}
if fase == 1{
global.intensity -= 0.01;
}

alarm[0] = 10;
  • Use the global.intensity value in your draw_ext call like this:

Code:
draw_sprite_ext(sprite_index, 0, x - 16, y + 20, 1, 1, 0, c_black, global.intensity);
This will give you a really quick day/time cycle, but you get the idea, just play around with the variables to change it to your likings. The shadows will increase and decrease in density over time now.

The easiest thing you can do now to get a full day/night cycle to work, is by drawing a completely black square filling the whole screen, the alpha value of the square should be equal to: 1-global.intensity

The code will look somewhat like this: (don't forget to give the object that draws this rectangle will have a really low depth, like -10000, this way it will draw above everything else)

Code:
draw_set_alpha(1-global.intensity);
draw_rectangle(0,0,room_width,room_height,false);
draw_set_alpha(1);
If the rectangle is drawn white instead of black, use this code:

Code:
draw_set_alpha(1-global.intensity);
draw_set_colour(c_black);
draw_rectangle(0,0,room_width,room_height,false);
draw_set_colour(c_white);
draw_set_alpha(1);
Well, I can't really make this tutorial more detailed than it already is, so good luck with this!
That code works great! But the only problem is, it fades in at the start of the game (and the start of the game is mid-day, so I kinda want it to stay there until night) and I don't know how to make it fade *out* at night? How do I do that? I've tried
if global.daytime = 2
global.intensity = 0;
fase = 0;
alarm[0]=10
but it abruptly dissapears when it turns night time..
 

lolslayer

Member
mid-day? Well. then you should put the variable "global.intensity" to 1 and the variable "fase" to 1 at the second step, this way you will start at complete daytime
 
B

bojack29

Guest
Lerp is a simple function. It takes the percentage between 2 numbers for its out put.

So, if you need some number 50% between 2 and 4, your output is 3.

75% between 2 and 4 is 3.5.

Make sense?

So if your daytime is 1 and your nighttime is 0 lerping between the two will give you a fractional result between 0 and 1. To match your sprite opacity shadow you merely multiply the resulting number by .2. Because at full day (1) * .2 will produce .2 for you. And likewise at (0) * .2 = 0. A decent way to cover a shadow from day to night.

You could also pull a trig trick to rotate the sprite throughout the day. If day is 1 and night is 0 you could rotate a shadow as:
Code:
draw_sprite_ext(sprite_index, image_index, x, y, 1, 1, 135 - 90 * global.dayTime, c_black, lerp(global.day, 0, variableThatMovesBetween0and1) * .2);
This would start the shadow at an angle of 45 and increase to 135 as the day goes on. Combined with the lerping trick above would give your shadow a slick appearance.
 
Top