Light switch tutorial

Revision as of 18:18, 13 February 2009 by imported>Puf the majic dragon (Added summary of collision properties for the activator)

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.

Create Your Switch

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.

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.

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 Switch

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

Placing Your Lights

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:

File:Lighttutflame.jpg
Properties of a light source

You then must edit each light source and parent it to one of your switches:

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. 

How It Works

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.