• Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Question - D&D No way to draw a transformed sprite using alpha

Slyddar

Member
I'm using drag and drop and trying to draw a sprite transformed with a custom alpha value, but the ability seems to be missing in DnD. Is this something that is not possible in DnD?

A) This works:
1/ This draws a sprite with alpha correctly, but not transformed.
works1.png

2/ As above, draws correctly.
works2.png

3/ As above.
Only 5 attachments allowed, so no more images, but as with 1/, this works too.
Set Draw Alpha
Draw Sprite


B) These do not work - they just draw the sprite at full alpha:
1/
does not work1.png

2/
does not work3.png

3/ This one shouldn't work, as *the manual states* set_draw_alpha is not for sprites, but just noting I tried it. *Note afterwards I've tried with Draw Sprite and this does work then, but no transform functionality*.
does not work2.png

I would love to be corrected, so if you know how to do this please advise. If it's not possible it needs addressing by Yoyo Games, by adding an alpha section to the draw sprite transformed code block.
 
Last edited:

rIKmAN

Member
I would love to be corrected, so if you know how to do this please advise. If it's not possible it needs addressing by Yoyo Games, by adding an alpha section to the draw sprite transformed code block.
Just tested this for you as I've never used DnD before.
If you click the white box next to colour then you can set the alpha via a slider in there and that affects the drawing when using draw_sprite_transformed().

set_draw_alpha() does work with sprites - it converts to draw_set_alpha() in regular GML - but it can be overidden, check the manual entry for that function for the specifics, including this part which seems to be the issue you are having because draw_sprite_ext() was using a value of 1 for alpha as it was not set by using the slider mentioned above.
GMS2 Manual said:
so drawing a sprite using draw_sprite_ext with an alpha argument of 1 will draw the sprite fully opaque regardless of the alpha set with draw_set_alpha (this will work the same when using draw_self() and setting the image_alpha of the instance).
 

Slyddar

Member
If you click the white box next to colour then you can set the alpha via a slider in there and that affects the drawing when using draw_sprite_transformed().
Thanks for taking your time to look, I appreciate it.

I should of mentioned I did try that. What I should of clarified by custom alpha, was one using a variable to control it. Dragging it to a locked in value is not a solution for fading objects over time. As you probably noticed, "Draw Sprite Transformed" converts to draw_sprite_ext() in GML, and the alpha value is there obviously coming from the colour selector section. The alpha in the colour selector is used for the images alpha, and for the colours alpha. Not having the ability to use a variable for the alpha specifically though is the issue.

set_draw_alpha() does work with sprites
I should also clarify where I got that information from. The manual entry for dnd for the set draw alpha code block states this:

GMS2 Manual said:
Set Draw Alpha
With this action you can set the alpha to use for any shapes (rectangle/ellipse/line) or for any text that is drawn subsequently.
It doesn't mention sprites, as other draw descriptions do. I've modified my original post to clarify this.
 
Last edited:

Slyddar

Member
I'd like a response from Yoyo on this please, as it seems this is a problem.

As mentioned above this is the description from the manual for the GML draw_set_alpha()
GMS2 Manual said:
draw_set_alpha()
will affect all further drawing, including backgrounds, sprites, fonts, primitives and 3D.
The code block Set Draw Alpha is shown in the live preview to be the function draw_set_alpha() as well, yet it's description from the DND manual is this:
GMS2 Manual said:
Set Draw Alpha
With this action you can set the alpha to use for any shapes (rectangle/ellipse/line) or for any text that is drawn subsequently.
The Set Draw Alpha code block does not work when used with Draw Self or Draw Sprite Transformed, BUT it does work correctly with Draw Sprite, although this block has no transform arguments.

From what I see, the manual needs correcting and Draw Sprite Transformed needs an alpha argument.
 

rIKmAN

Member
Not having the ability to use a variable for the alpha specifically though is the issue.
Ah sorry I didn't really mess with it too much, I don't use DnD.

Hopefully someone more familar with how it's supposed to work can help out, but there aren't many experienced DnD users around so you might be better just filing a Bug Report and attaching your sample project and waiting for them to get back to you - remember they dont work weekends.
 

Slyddar

Member
https://bugs.yoyogames.com/view.php?id=31194 - No way to pass alpha value to draw_sprite_transformed
Any Yoyo dudes able to give an update on this job? I'm trying to give new users more exposure to cool Gamemaker features through Drag and Drop tutorials, and not being able to fade sprites drawn with draw_sprite_transformed really limits many aspects of what I would like to show. Being able to fade a transformed/ext sprite is an absolute staple of any GML users toolkit, it's been 9 months, any chance this can be addressed soon please?
 
Last edited:
https://bugs.yoyogames.com/view.php?id=31192 - Draw_self is not effected by set_draw_alpha/draw_set_alpha
As they mentioned in the ticket, this is not a bug. The alpha of an instance's sprite is determined by the built-in variable "image_alpha" and you can set this using DnD as you would any other variable. Or you can use the Set Instance Alpha as you showed in your example.

DnD - Draw Self - Alpha.PNG

-

https://bugs.yoyogames.com/view.php?id=31194 - No way to pass alpha value to draw_sprite_transformed
Again, not sure this really qualifies as bug and isn't entirely true. If anything this is a feature request.

I will say that passing an alpha value into Draw Sprite Transformed may not seem very intuitive for DnD users.

The value used in the color field needs to be passed in as a hexidecimal value. If you check out the documentation on Color in the manual, you'll see that hexidecimal values can be stored into variables by preceding the value with $. You can store your alpha as a hexidecimal value and pass it in this way, but it gets a little tricky since you have to pass the alpha and color as a single value.

To do this, you could store the alpha and color as separate values and then add them together in the "Colour" field for Draw Sprite Transformed. The format for "Colour" field is $AABBGGRR where, AA = Alpha, BB= blue, GG = green, RR = red.

e.g., if you want an alpha value of 0.5 you first need to figure out the hexidecmial value for this number. In GMS2, the Alpha channel of a color is going to be a value between 0 and 255, so 0.5 will be 127. The hex value for 127 is $7F. Since you'll be adding this to your color value, you need to store this value as a full $AABBGGRR formatted value: $7F000000.

Next you need your color with 0 alpha in $AABBGGRR format, which will likely be white: $00FFFFFF.

Add these values together and you have the value that needs to be entered into the "Colour" field of Draw Sprite Transformed.

DnD - Draw Sprite Transformed - Alpha.PNG

-

edit: corrected the color format from $AARRGGBB to $AABBGGRR.
 
Last edited:

Slyddar

Member
As they mentioned in the ticket, this is not a bug.
Thanks for that, but I know this was solved, hence why I only linked the 2nd question.

Your reply for the 2nd part is excellent! It would of really helped me 9 months ago if they had told me something like that. Thank you for taking the time to describe the reason it works like it does. Sure it's not intuitive for DnD, but at least it's possible.
 

TsukaYuriko

šŸŒ 
Forum Staff
Moderator
Since you were asking for an official response multiple times: Please note that the GameMaker Community is not a valid means to contact YoYo Games or to get official responses - this would have to go through the helpdesk as YoYo Games does not actively monitor these forums. Any such replies are done in their spare time and of their own volition.
 

Slyddar

Member
Understood. Was hoping to generate some conversation at the same time, and that was achieved, so we happy Vince.
 

Slyddar

Member
It worked great for what I wanted it for. Obviously very complicated compared to how easy it should be for DnD users.

I also wanted to use the same method for some surface drawing with lighting, and there the alpha doesn't control the amount of light, rather the colour 'strength' from black to white. So I have a colour, (ignoring the 2 alpha values) say $7FC3FF, and am wanting to decrease it to $000000 over time. I've searched online with little success, so when I try to lerp it to 0, or minus values off of it, in order to slowly move it to zero, it just flashes various colours. I guess my knowledge of hex is very limited. Any suggestions on how to move a hex colour to black over time?

EDIT: Well I found a solution. Not sure if it's a good method, but I used color_get_red/green/blue to extract the 3 single colours, then lerped each one to zero as a round, before using make_color_rgb to combine them back again into a hex value. This then allowed it to fade out. Happy to hear any better suggestions though.
 
Last edited:
Maybe merge_color would suit your purpose better? Use the color of your light for col1 and black for col2. Amount would essentially be your fade value.

From the manual:
merge_colour(col1, col2, amount);

col1The first colour to merge
col2The second colour to merge
amountHow much of each colour should be merged. For example, 0 will return col1, 1 will return col2, and 0.5 would return a merge of both colours equally
 

SSJCoder

Member
I think you need to lerp while keeping the proportions in each respective colour, either getting colour as you did, or using something like:
GML:
round( R*$FF*light )*$10000 + round( G*$FF*light )*$100 + round( B*$FF*light );
where R, G, and B are from 0-1 and represent the original colour, and light is from 0-1, and represents how much light there is (0 = black). This code assumes $RRGGBB formatting - do check since when testing in GM8, it seems to be $BBGGRR.
 
Last edited:

Slyddar

Member
Maybe merge_color would suit your purpose better? Use the color of your light for col1 and black for col2. Amount would essentially be your fade value.
Great idea! Changed to this method, and it works perfectly, in a much cleaner way. Had totally forgot about merge_color, even though I've used it many times in the past. Thanks again for the prod in the right direction.

Thanks also @SSJCoder, I appreciate your input, it's great to see other options, but with merge_color doing the job I'll stick with that method.
 

SSJCoder

Member
Thanks also @SSJCoder, I appreciate your input, it's great to see other options, but with merge_color doing the job I'll stick with that method.
you do you, I'm merely showing you the correct way of lerping the colour since you have said:
I've searched online with little success, so when I try to lerp it to 0, or minus values off of it, in order to slowly move it to zero, it just flashes various colours. I guess my knowledge of hex is very limited. Any suggestions on how to move a hex colour to black over time?
 
Happy to help, although, @SSJCoder pointed out something important that I got totally wrong in my original post about the color formatting. I forgot Game Maker does in fact use BGR formatting instead of RGB. Didn't occur to me I had it backwards because I was working with the same value for all channels anyways.

Updated that post just in case someone comes looking here after the fact.
 

Slyddar

Member
if you want an alpha value of 0.5 you first need to figure out the hexidecmial value for this number.
So the next step is how to use a variable for alpha, rather then a set value such as 0.5. Since alpha still needs to be passed in as a hex value, I'm not sure how to convert into the $xx format required to combine with the colour. Using merge_color doesn't help, as it seems to ignore the alpha values. I've tried dec_to_hex scripts, but the output for say 255 ends up as FF, where as adding hex values I need it in the $xx format to add to the colour.
 
Last edited:
I've tried dec_to_hex scripts, but the output for say 255 ends up as FF, where as adding hex values I need it in the $xx format to add to the colour.
Just to clarify, is the issue that your dec_to_hex script is returning the string "FF" and not the hex value $FF?

If so, I'm honestly not sure it's even possible to convert a string to a hex value, but it's also not really necessary. As far as GameMaker is concerned, ($FF == 255) evaluates as true.

If your problem is getting the alpha value to properly add to your color, you simply need to shift it to the left by 24 bits. The following is the same as $FFFFFFFF:
GML:
(255 << 24) + $FFFFFF
-

Also, is it really necessary to use a decimal value to begin with? If you know you ultimately need a hex value, why not start with a hex value and eliminate the need to do any conversions?

For example, if you're trying to set up a sprite to fade in/out:

Setup vars - Displayed values assume fade out. To change to fade in, set myAlpha to $00 and fadeDir to 1.
fade_vars.PNG

Step event - Increment myAlpha by the fadeSpeed in the desired direction
fade_step.PNG

Draw event - Draw the sprite
fade_draw.PNG

May also be worth noting that you don't necessarily NEED to use hex values, but hex values are more readable as colors and consistent with what DnD users see when they use the color selector. Ultimately, GameMaker is converting these hex values to real values, it's just not easy to look at these real values and understand how they correlate to colors.

If you really want to use real values for alpha, you could do this instead:

Setup vars - Displayed values assume fade out. To change to fade in, set myAlpha to 0 and fadeDir to 1.
dec_fade_vars.PNG

Step event - Increment myAlpha by the fadeSpeed in the desired direction (this is unchanged from the hex value setup)
fade_step.PNG

Draw event - Draw the sprite
dec_fade_draw.PNG
 

Slyddar

Member
Thank you again for taking the time to explain, especially in such detail. Everything you have for the 2nd example is the pretty much the same as what I'm doing, but the missing piece was the conversion of "myAlpha << 24". Having that piece of info is actually all I needed. Interesting to see the first method though.

Just to understand what is actually happening, bit shifting an integer under 255 to the left by 24 bits converts it to a hex value since that's a base 16 system?
 
Last edited:
Just to understand what is actually happening, bit shifting an integer under 255 to the left by 24 bits converts it to a hex value since that's a base 16 system?
Not exactly. It has to do with how binary values work and how GameMaker expects the ABGR color to be formatted.

Each channel is a value between 0 - 255 which can be expressed in 8 bits. So your full BGR color is 24 bits: [8 bits of blue] [8 bits of green] [8 bits of red].

In binary, white (255, 255, 255) would look like: 11111111 11111111 11111111.

Your alpha value is also 8 bits, and 255 alpha is 111111111.

When GameMaker expects an ABGR value, it wants 32 bits: [8 bits of alpha] [8 bits of blue] [8 bits of green] [8 bits of red].

You can't simply add your A value to your BGR, as this will give you 0100 0000000 0000000 11111110.

Instead, you want to shift your alpha value to left by 24 bits, which gives you: 11111111 00000000 00000000 00000000.

Now you can add this to your BGR to get a proper 32 bit ABGR value:
11111111 00000000 00000000 00000000 + 11111111 11111111 11111111 = 11111111 11111111 11111111 11111111.
 

Slyddar

Member
That makes complete sense. Great explanation again. Thank you for your time and patience.

Now I bet next patch Yoyo will finally separate the alpha and colour on draw blocks, and this won't be needed, hehe :) Until then though, this is a handy reference for others.
 
Top