NightVision Tutorial

From Valve Developer Community
Revision as of 23:18, 27 January 2006 by Ts2do (talk | contribs)
Jump to navigation Jump to search

Adding Nightvision To your Source Single Player Mod

Here's something that is part of NightFall. And one such thing that many people have been stumped on - Night Vision.

Summary

Before I start, this is only one, and a very early attempt (It works), of one way to get the Night Vision Mode to work with your mod.

I would like to credit Gnolfo from http://www.hl2coding.com and user:ts2do

Seting up the ConVar, The Meat and The Spuds

Open the file src/cl_dll/view_scene.cpp, and append the code below to the end of this ifdef

//-----------------------------------------------------------------------------
// Precache of necessary materials
//-----------------------------------------------------------------------------

#ifdef HL2_CLIENT_DLL

Found around line 170 (to go to a line number quickly, press ctrl+g)

//Precache Night Vision
CLIENTEFFECT_MATERIAL( "HUDoverlays/nightvision" )


Now jump to the end of the page (ctrl+end) and add this class

//nightfall - amckern - amckern@yahoo.com
//NightVision
static void ScreenOver_f( void )
{
	IMaterial *pMaterial = materials->FindMaterial( "HUDoverlays/nightvision", TEXTURE_GROUP_OTHER, true );
//This is the texture we are going to use for the 'effect' - never use an ext on material files

	{
		static bool bDisplayed = false;
		
		if( bDisplayed )
		{
			// turn it off
			view->SetScreenOverlayMaterial( NULL );
			// Deactivate the 'light'
			cvar->FindVar("mat_fullbright")->SetValue(0);
			CLocalPlayerFilter filter;
			C_BaseEntity::EmitSound( filter, 0, "Nightfall.NightVisOff" );
			//play the off sound
		}
		else
		{
			// turn it on
			view->SetScreenOverlayMaterial( pMaterial );
			//this is the HUDoverlays/nightvision texture we made a pointer to above
			// Activate the 'light'
			cvar->FindVar("mat_fullbright")->SetValue(1);
			CLocalPlayerFilter filter;
			C_BaseEntity::EmitSound( filter, 0, "Nightfall.NightVisOn" );
			//On we go - play a sound to let the player know that the NV is on
		}
		
		bDisplayed = !bDisplayed;

		//check if fullbright has been disabled, or enabled
		if (cvar->FindVar("mat_fullbright")->GetInt() == 1)//is it on?
		else( cvar->FindVar("mat_fullbright")->SetValue(0);)//well turn it off.
	}
}

static ConCommand r_screenover( "r_screenover", ScreenOver_f );

So what we have done here is set a bool cvar (on/off) that either enables or disables the night vision effect. If you read the comments, you will understand what each part does.

This section has been edited to keep the Fulbright cheat a cheat, to stop people exploiting it - the code has been updated from that supplied in the comments.

It might sound strange to have the off function first, but you don't want the player to be runing around the game with night vision turned on from the time they spawn.

The Sound

Nightfall.NightVisOn / Nightfall.NightVisOff

This code is calling on the script file nightfall_engine_sounds.txt that in turn is preached in game_sounds_manifest.txt like so

	"precache_file"		"scripts/nightfall_engine_sounds.txt"

I preached the sounds with preachescript("Nightfall.NightVisOn"); in the player.cpp preache function, just incase you are wondering why the game lags when you go to use the sound effect.

You can always use a direct sound event like engine->ClientCmd( "play your_effect_sound.wav\n"); if you don't know how to make a soundscript sound. the downside of this is the game will always lag while it loads the sound file for the first time, and from what i understand, there is no way to preache a sound file direct with in the code.