Light switch tutorial

Revision as of 09:35, 15 April 2010 by imported>Darkness X
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Many users have requested information on setting up a light switch in the game. This tutorial explains in detail with screenshots how to set up a generic light switch which can easily be replicated throughout your mod with only one simple script.

Tools used in this tutorial

Required


Create Your SwitchEdit

First you will need to create a new activator world object which will act as your switch. This can be any object you desire such as a Welkynd stone or a new mesh of your design. This tutorial uses a .nif file with only a dome shaped collision object and Editor Marker, which appears as an invisible object in game which the player can still interact with. This file can be downloaded from TESNexus here

If you create your own mesh, keep in mind that certain settings on the collision node in NifSkope need to be changed in order to keep the object from falling due to gravity or moving when it is bumped. In short, the "Layer" on the bhkRigidBody needs to be OL_STATIC, the "Collision Response" needs to be "RESPONSE_SIMPLE_CONTACT", and the "Motion System" needs to be "MO_SYS_FIXED". Other properties may also need to be set inside this node as well, so it is best to compare your settings to the properties of an unedited Bethesda mesh.
If your activator has no visible mesh, make sure to add an editor marker to the mesh so you can see it when you place it in the render window.

Once you have created the new activator, attach the following script to it:

scn twLightSwitchScript
ref parent

Begin OnActivate
	set parent to getParentRef
	if GetDisabled == 0
		if parent != 0
			if parent.getDisabled == 0
				parent.disable
			else
				parent.enable
			endif
		endif
		disable
	else
		if parent != 0
			if parent.getDisabled == 0
				parent.disable
			else
				parent.enable
			endif
		endif
		enable
	endif
End

What this script does is when the object is activated, it detects if the object has a parent reference, then whether that parent reference is disabled, and enables or disables that parent ref accordingly, and then disables or enables itself. The purpose of this script is to allow you to use one single base reference to place multiple switches for multiple lights in the game without ever having to go back and edit the script.

Set Up Your SwitchEdit

File:Lighttutstatics.jpg
Statics placed in the game for visual effect

Now you need to set up your switch in the game. Open up the cell you wish to have your switch in in the render window and place an object in the cell which will act as the visible reference for your switch, basically setting up your switch how you want it to appear in game. If you used a visible object, such as a Welkynd Stone, for your activator, you can skip this step.

File:Lighttutswitch.jpg
The editor marker showing the placement of the invisible activator

Next place your activator in the game arranged near your switch as you see fit.

File:Lighttutoff.jpg
Properties of the Off switch

Then duplicate it by pressing ctrl+D. Now that you have two activators in the render window, edit them by giving them names and setting their properties as detailed below. One of the activators will be the switch for when the lights are on, and the other will be the switch for when the lights are off, so name them accordingly.

File:Lighttuton.jpg
Properties of the On switch

Make sure that both objects are "Persistent References" and that the "On" switch is parented to the "Off" switch with "Set Enable State to Opposite of Parent" checked.

File:Lighttutflamerender.jpg
A flame placed as a light source activated by the switch
Rather than duplicating your activator, you can place only one activator and then place an xmarker or any other object of your choice (even one of your lights). Parent your activator to the xmarker and modify the activator script to only enable/disable its parent. In the next section, you will need to parent all of your lights to the xmarker rather than the switch. There is no real advantage to either method, it's up to your personal preference.

Placing Your LightsEdit

The next step is to place your light sources in the render window and arrange them how you want them to appear in game. For example, placing a flame over a brazier (see screenshot).

File:Lighttutflame.jpg
Properties of a light source

You then must edit each light source and parent it to one of your switches, as shown in the screenshot.

If you want to have candles or chandeliers or other visible objects as your light source, you can simulate the candle in the off and on states by placing the "off" version of the candle (usually listed under World Objects > Statics > Lights in the Object Window of the CS) and parenting it to the Off switch, then place the "Fake" version of the same candle (usually found in the same place as the off version) and parent it to the "On" switch. Then place an ordinary light (represented by the light bulb) somewhere nearby.

You can place a "light" version of the candle, found under World Objects > Light > Lights in the Object Window, however the more lights you place in a cell, the lower your framerate will be. It is best to place several "fake" candles and then only one real light to improve performance. 
There is a bug in the lighting system when using objects as light sources that sometimes causes the lighting to not update properly after the object has been disabled. Again, using the "Fake" version of a light source combined with a regular "light bulb" light will work around this issue.

How It WorksEdit

When you enter the game and approach the switch, the lights will be off. By activating the switch, which will be the "off" switch, it disables itself which automatically enables the "On" switch. The "On" switch can be activated repeating the process in reverse. Now by parenting all of your light sources to either switch, they will be enabled or disabled (turned on or off) according to which switch is enabled or disabled. Any number of light sources or other objects can be added to the switch simply by parenting them to the switches or daisy-chaining them together without ever editing the script. Also, you can select the switches and light sources in the render window and duplicate them (ctrl+d) and move the copies anywhere you wish and the copied lights will work with the copied switch without any additional editing. Finally, you can set up multiple sets of switches to activate the same lights, for example a set of switches on each end of a long hallway, simply by parenting the second set of switches to the first "Off" switch.

File:Lighttutgame1.jpg
The switch turned off in game
File:Lighttutgame2.jpg
The switch turned on in game