Here is a simple and compact library for handling lights and switches which I have just written.
In order to use it, you have to do the following:
1. Add the Lights.aslx library to your game
2. Create object
light which will inherit from
light_source type.
3. Create object
switch which will inherit from
light_switch type.
4. On the object
switch add the attribute
source of type
object and assign the object
light to it.
5. On the object
light add the attribute
room of type
object and assign the object
room (where "room" is the one containing light) to it.
Features and limitations:1. You cannot turn the lights on if the room is not dark.
If you want the lights to work, you need to call SetDark(room) or set it to an initial dark state from the editor.2. You can have multiple light sources in the same room but each requires its own switch.
Reason for this is that it would be much more complicated to write code to deal with one switch that controls multiple lights (yes I was lazy).3. You can have multiple light sources in the same room but only one can be active at a time.
Reason for this is the way how Quest CheckDarkness() function works -- one strong light source is enough to illuminate the room and there is no code in Quest to handle addition of weak lights and checking if they together make strong light. If you don't like it you can always ignore darkness status of the room but then why did you want lights in the first place?4. You can have a switch in one room and light in another room.
This comes in handy when part of puzzle.5. You can have more than one light switch in different locations controlling the same light.
For example, you can have a bedside switch for a central bedroom light, and one by the entrance. That way player could reach the light if they are in bed.Final notes:
- If you have time keeping in your game and you want to make some rooms dark at night using a turn script, the proper thing to do is to use SetDark(room) and SetLight(room) for each room affected by global illumination (i.e. the one that has windows and is not underground).
- This library overrides default behavior of CheckDarkness() function -- keep that in mind if you notice any weird behavior with light sources in game.
- If you do not assign light to a switch or if you do not assign parent room to a light, you will get a runtime error message saying which object you forgot to assign something to.
- If you want to translate or otherwise change the messages, they are all in templates and dynamic templates at the beginning of the library code.
Code is free for non-commercial purposes provided that you mention me in your project as its author.
Criticism, comments, and suggestions are welcome.
UPDATE:2013-03-24 - Version 1.1 -
There is a new boolean attribute "disabled" for light switches so you can control whether they can be used or not without having to make code editable. Also, there is a new script attribute "disabled_message" for light switches which gets executed when the player tries to switch the disabled light on or off.P.S. For those curious: the reason why light must "know" about its parent room is because otherwise you have to perform extensive search of light's parents and override even more Quest code to make things work. Quest by default doesn't "see" the light which is not a direct descendant of the room. For example, if you turn on the light which is on a table, CheckDarkness() won't change the darklevel attribute of the room, and the room will for all purposes and intents stay dark even though the light is on. This will probably be fixed in Quest 5.4 release so expect an update afterwards.