Adding a Timer to Your New Light Source

Mac users will need to use IffSnooper's BHAV editor. I've no instructions for that here at SSoW but you can learn to use the editor from Peter Gould's Understanding the BHAV resource. If any mac users would like to contribute a "translated" version of this process I'd be delighted to add it to the Woobsha Workshop.

Next question - how would I make it light up at night, instead of being on all the time?

For this we'll open to the Main BHAV (BHAV #4096) and add 5 lines to it. The Main only had a single line in it to start with so our additional five will be numbers 1 through 5.

We'll start of by making sure the focus of the code is the floral trim, just as we did with the init BHAV, earlier. There's no reason in the world it should not be, but it can't hurt to add it in just in case.
So for line 1, enter the following into the Data box

    Function 2
    True line: 2
    False line: 253
    parameter1: 0
    parameter2: 11
    parameter3: 1280
    parameter4: 778

Line 1's text should now should read

    Stack Object ID Assign To: My object id

With that out of the way we'll check the time of day next. Sim time runs on 24 hours (0 to 23) and we'll use two lines to do this: They will first check to see if it's after[the morning hour] and if it is not, then will check to see if it's before [the night hour]

Enter the following into Line 2's Data box

    Function: 2
    True line: 3
    False line: 4
    parameter1: 0
    parameter2: 7
    parameter3: 0
    parameter4: 1798

the text for that line should read

    Global ( from Simulation ) Hour > 7


Enter the following into Line 3's Data box

    Function: 2
    True line: 5
    False line: 4
    parameter1: 0
    parameter2: 19
    parameter3: 256
    parameter4: 1798

The text for that line should read

    Global ( from Simulation ) Hour < 19

Parameter 2 in both of those lines holds the Sim Hour. The first line translates into "It is later than 7:00am" If this statement is true, the code goes to line 2 to find out how much later than 7:00 am it is. If it's not true (if it's 5AM, for instance) then it skips right to line 3 which we've not filled in yet. The second line translates into "It is earlier than 7:00pm" If this is true we know it's daytime (remember, this line of code never runs at all before 7AM) and if it's not true then we know it's nighttime.

The lines 4 and 5 that those two are referring to in their True/False returns are the lines that will tell the trimming how much light to cast. These lines are just like the one you put into the INIT BHAV, calling on the Global file for it's BHAV 275 and passing the lighting amount in 'sp0' (that first pair of numbers in the parentheses.

For line 4, enter the following into the Data box

    Function 275
    True line: 0
    False line: 253
    parameter1: 75
    parameter2: 0
    parameter3: 0
    parameter4: 0

This is your On light, the one used if it's earlier than 7am but later than 7pm. It should read

    (glob:) set object lighting (4b 00 00 00 00 00 00 00)

For line 5, enter the following into the Data box

    Function 275
    True line: 0
    False line: 253
    parameter1: 0
    parameter2: 0
    parameter3: 0
    parameter4: 0

This is you "off" light, used when it later than 7am but earlier than 7pm. Your text will now read

    (glob:) set object lighting (00 00 00 00 00 00 00 00)

In both cases they end up returning the routine to line 0 instead of ending in "True". This is because the Main bhav never ends, it loops on itself continuously (like a heart beating). At this point while the 5 lines we added all link to each other none of them will be used by the object itself. This is because the original coding was never altered to include them. On page one, while working with the init that was the first thing we did after adding our additional lines but this time I waited until now because we need to change that line in another way as well.

Modifying the Main BHAV's Original Line

The line that starts this BHAV off is an "idle" behave. That basically means it sits and lets some time pass before going on to the next line. The higher the number, the longer it waits. Right now the number is very high and the object would wait a very long time before checking the time and adjusting the lighting. Since we are using Sim Hours to decide when the light should go on and off we need to shorten the "idle" time to an hour or less ensuring it'll check the time each hour and adjust the lighting at approximately the correct time. The lower the idling time the more accurate everything will be.

image: IffPencil's Select Function menuWhile I could simply give you the number that I know is One Sim Hour (in fact, I will ... it's 1800) I'd rather show you this really neat aspect of IffPencil: The "Select Function" drop down Menu.

Give that little easy-to-miss button a push and you'll get the Select Function window. This is a drop down menu showing everything you can enter into your Function field. While we usually use Function 2 ("Expression", whatever that means) There are several other functions that can be used and, as we saw while working in the INIT BHAV we can enter "global" BHAV numbers ("global calls") as well. Two other types of "calls" (private and semi-global) but right now we are interested in one of the Global Calls.

image: Select Function Dropdown Menu While working in the INIT BHAV I wanted to remember the number for the global call "Set Object Lighting" so I grabbed the first light object I could find and looked inside of it for the number. What we could have done instead was use this dropdown menu and found Set Object Lighting in the list. (I must confess drop-down menus and I don't get along well so I usually forget all about this one). The global call we want to use now is the one pictured "Idle Hours" (you'll have to scroll up from "idle" to find it)

If you scroll up even further you'll fid they've made an "Idle Minutes" one as well. These global BHAVs work very much like the "idle" one the floral trim is already using but they have a few extra lines tossed in to do the math for you, converting the simple "idle for 3 hours" into "idle for 5200 ticks" - no need for you to remember that there are 30 ticks in a Sim-minute and 1800 in an Sim-hour.

For now pick the Idle hours one just so we've got the same thing going on, but later you can switch to idle minutes if you prefer. You'll notice that the 280 that was in the function field before has switched to 366 and the text has changed to "(glob:) idle hours (20 4e 00 00 00 00 00 00)". We don't, however, want it to idle for 20,000 sim hours so we need to change that to a 1 and we must not forget to link our new lines in (right now it's still looping around itself and never going on to line 1.)

So line 0, must end up with the following in it's the Data box

    Function 366
    True line: 1
    False line: 253
    parameter1: 1
    parameter2: 0
    parameter3: 0
    parameter4: 0

And the text will now read

    (glob:) idle hours (01 00 00 00 00 00 00 00)

Ok, back to the main window and Save. Yer done with that bit :)

With this code the object will check the time every (Sim) hour and adjust the light accordingly. The inaccuracy I mentioned earlier is because the Main BHAV begins running when you go into Live-mode. If you go into live-mode at 4:58, Sim-time then it wont check the time again until 5:58,Then again at 6:48 (which will still count as "Earlier than 7) and then again at 7:58 at which time the lighting will change - an nearly hour past the time you'd like it to change. You can alleviate this by using Idle Minutes instead of hours (that's global BHAV 367, by the way) but keep in mind that the shorter the idle the more often it's going to use your computer's resources. Obviously for a little thing like this light that's not a big deal but if you have 100 lights on a lot it may become noticeable. It's much better to use a "save your pennies and the dollars will take care of themselves" frame of mind and keep each object as optimized as you can .. it'll be grouped with a whole lot of objects (I'm sorry there was no way to phrase that without the pun surfacing) later, after all.

Two last points on all of this. If you time the on/off parts to coincide with the Suns's rise and fall chances are you wont notice any difference at all between this and the light that is always on. If this is the case it may be better to leave it on all of the time as the idle can be left at 20000 and the object never needs to check the time - it'll use just a wee less resource.

The other is that this is straight forward On/Off toggle with the Sim hours you set and will not react to a Sim walking in or out of the room at all.

Author: Raeven