[Solved] Fading out/in actor based on distance from player.

Ask about ACS, DECORATE, ZScript, or any other scripting questions here!

Moderator: GZDoom Developers

Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. If you still don't understand how to use a feature, then ask here.

Please bear in mind that the people helping you do not automatically know how much you know. You may be asked to upload your project file to look at. Don't be afraid to ask questions about what things mean, but also please be patient with the people trying to help you. (And helpers, please be patient with the person you're trying to help!)
User avatar
Joven0culto
Posts: 7
Joined: Thu Aug 22, 2024 4:08 am

[Solved] Fading out/in actor based on distance from player.

Post by Joven0culto »

The intention is to make an actor that represents fog and that disappears or reappears depending on the distance from the player, if you get closer it disappears and if you move away it reappears.

As far as I have been able to inform me this DECORATE should work perfectly, but it does not do it, the sprite simply appears normal without making any type of change in its alpha.

Code: Select all

Actor Mist1 30007
{
	Height 15
	Radius 10
	RenderStyle "Translucent"
	Alpha 1.0
	+NoClip
	+NoGravity
	States
	{
	Spawn:
		MIST C 0
	GoTo State1
	State1:
		MIST C 1 A_SetRenderStyle(1.0,Style_Translucent)
		TNT1 A 0 A_JumpIfCloser(512,"State2")
	Loop
	State2:
		TNT1 A 0 A_JumpIfCloser(100,"Alpha90")
		TNT1 A 0 A_JumpIfCloser(90,"Alpha80")
		TNT1 A 0 A_JumpIfCloser(80,"Alpha70")
		TNT1 A 0 A_JumpIfCloser(70,"Alpha60")
		TNT1 A 0 A_JumpIfCloser(60,"Alpha50")
		TNT1 A 0 A_JumpIfCloser(50,"Alpha40")
		TNT1 A 0 A_JumpIfCloser(40,"Alpha30")
		TNT1 A 0 A_JumpIfCloser(30,"Alpha20")
		TNT1 A 0 A_JumpIfCloser(20,"Alpha10")
		TNT1 A 0 A_JumpIfCloser(10,"Alpha0")
	GoTo State1
	Alpha90:
		MIST C 0 A_SetRenderStyle(0.9,Style_Translucent)
	GoTo State2
	Alpha80:
		MIST C 0 A_SetRenderStyle(0.8,Style_Translucent)
	GoTo State2
	Alpha70:
		MIST C 0 A_SetRenderStyle(0.7,Style_Translucent)
	GoTo State2
	Alpha60:
		MIST C 0 A_SetRenderStyle(0.6,Style_Translucent)
	GoTo State2
	Alpha50:
		MIST C 0 A_SetRenderStyle(0.5,Style_Translucent)
	GoTo State2
	Alpha40:
		MIST C 0 A_SetRenderStyle(0.4,Style_Translucent)
	GoTo State2
	Alpha30:
		MIST C 0 A_SetRenderStyle(0.3,Style_Translucent)
	GoTo State2
	Alpha20:
		MIST C 0 A_SetRenderStyle(0.2,Style_Translucent)
	GoTo State2
	Alpha10:
		MIST C 0 A_SetRenderStyle(0.1,Style_Translucent)
	GoTo State2
	Alpha0:
		MIST C 0 A_SetRenderStyle(0.0,Style_None)
	GoTo State2
	}
}
Any ideas on what might be going wrong?
Last edited by Joven0culto on Fri Sep 20, 2024 8:47 am, edited 1 time in total.
Blue Shadow
Posts: 5019
Joined: Sun Nov 14, 2010 12:59 am

Re: [Need help] Fading out/in actor based on distance from player.

Post by Blue Shadow »

A_JumpIfCloser checks distance between the caller and its target, which is something your actor doesn't have. Here's an alternative way of tackling the code:

Code: Select all

actor Mist
{
	RenderStyle "Translucent"

	States
	{
	Spawn:
		CYBR A 1 A_SetRenderStyle(clamp((GetDistance(true, AAPTR_PLAYER1) - 128) / (768 - 128), 0, 1.0), STYLE_Translucent)
		Loop
	}
}
Note the above code isn't multiplayer friendly, as it checks distance between the actor and player #1 only.
User avatar
Joven0culto
Posts: 7
Joined: Thu Aug 22, 2024 4:08 am

Re: [Need help] Fading out/in actor based on distance from player.

Post by Joven0culto »

Blue Shadow wrote: Fri Sep 20, 2024 3:46 am A_JumpIfCloser checks distance between the caller and its target, which is something your actor doesn't have. Here's an alternative way of tackling the code:

Code: Select all

actor Mist
{
	RenderStyle "Translucent"

	States
	{
	Spawn:
		CYBR A 1 A_SetRenderStyle(clamp((GetDistance(true, AAPTR_PLAYER1) - 128) / (768 - 128), 0, 1.0), STYLE_Translucent)
		Loop
	}
}
Note the above code isn't multiplayer friendly, as it checks distance between the actor and player #1 only.
O.O <(WOU)

This is the HUGE difference between one how know what is doing and one who don't know a shit about programming.
Thanks, it worked like charm, I made some adjustments for the distances.

Could you please make a little breakthrough of the code? because I'm not sure what I'm seeing and I would like to learn.
Blue Shadow
Posts: 5019
Joined: Sun Nov 14, 2010 12:59 am

Re: [Need help] Fading out/in actor based on distance from player.

Post by Blue Shadow »

It uses the distance as a ratio (distance / max distance) to calculate the actor's alpha. The part where 128 is subtracted is just to make it so the actor is completely invisible if the distance 128 or less. clamp() makes sure the resultant alpha doesn't go below 0 or above 1.0.
User avatar
Joven0culto
Posts: 7
Joined: Thu Aug 22, 2024 4:08 am

Re: [Need help] Fading out/in actor based on distance from player.

Post by Joven0culto »

Blue Shadow wrote: Fri Sep 20, 2024 8:39 am It uses the distance as a ratio (distance / max distance) to calculate the actor's alpha. The part where 128 is subtracted is just to make it so the actor is completely invisible if the distance 128 or less. clamp() makes sure the resultant alpha doesn't go below 0 or above 1.0.
Many thanks 🙏

Return to “Scripting”