Page 1 of 1

Scripting Question - Can't keep headlights on

Posted: Wed Feb 04, 2015 3:40 am
by chrisonline
I'm cutting my teeth on scripting and experimenting with OnCameraEnter to get various effects when entering the cab on a double-cabbed loco.

Here's the piece of code in question (sorry, tabbing doesn't seem to work here):

function OnCameraEnter (cabEndWithCamera, carriageCam)
if cabEndWithCamera == 1 then
Call ( "CabLightA:Activate", 1 )
Call ( "Fwd_Headlight_1:Activate", 1 )
elseif cabEndWithCamera == 2 then
Call ( "CabLightB:Activate", 1 )
Call ( "Bwd_Headlight_1:Activate", 1 )
end
end

Both the cablights and the headlights are child objects. The cablights work fine - they come on and stay on while I am in the respective cab (they also turn off again when I leave the cab, but I've omitted that code as it is not at issue)

My problem is that the headlights FLASH briefly but don't stay on. Each time I go back to the cab they flash again but turn off. Why is that?

Both of these lights work fine with the normal manual switches, yet react differently under the OnCamera Enter function. If something else is turning the headlights off again, why isn't the same happening to the cablights?

Can anyone point to the error of my ways? Any help is appreciated

Chris

Re: Scripting Question - Can't keep headlights on

Posted: Wed Feb 04, 2015 7:06 am
by markpullinger
Hi Chris, try calling the headlight something else as I believe the game likes to control that.

Re: Scripting Question - Can't keep headlights on

Posted: Wed Feb 04, 2015 2:50 pm
by chrisonline
Thanks for the suggestion Mark

Well that would be a very messy thing to do, as it has a knock-on effect through naming and treatment of several other files (Default/Lights folder among them)

It's actually the JT Class 153 lua script that I'm trying to edit, more to learn the ropes than anything. I tried various functions, the headlight was just an example as it was something I could physically see the results from the cab view. I also tried HornSound - Call ( "HornSound:Activate", 1) - but couldn't get any sound at all from my code (at least a brief "beep" would be consistent!), though the horn continues to work with regular manual controls (direct within the cab, or using the HUD).

The headlights are not anywhere else in the script, so it's not like they are automatically "looped" off again by the timed update function (or so it appears). But if that is happening, how do I prevent it? There is a HornSound function, but then there is also a CabLight function (which work with my code). So I'm none the wiser . . .

Anyone have any ideas?

Chris

Re: Scripting Question - Can't keep headlights on

Posted: Wed Feb 04, 2015 3:22 pm
by DaveDewhurst
OOC,
Ive just had a look in my 153 directory and there is also a .out file in the same directory as the .lua
You have moved/renamed this .out file haven't you?
If you don't the sim will use this in preference to your edited .lua

Dave

Re: Scripting Question - Can't keep headlights on

Posted: Wed Feb 04, 2015 4:04 pm
by chrisonline
Yep, only the .lua script. And as I say, it's having SOME effect, just not the fully desired effect!

Re: Scripting Question - Can't keep headlights on

Posted: Wed Feb 04, 2015 6:13 pm
by scefhwil
Seems to me that the hardcoded part of the sim might be continuously checking the headlight state and updating the headlight nodes to suit. So your OnCabEnter function turns them on only for the sim next loop through to check their state see that they should be off so turns them off. Hence why they flash once.

You could try changing the headlight control value instead then the sim itself might do the node activation part, or create a state whereby you continuously turn the headlight on within the Update function so you continuously override the hardcoded action, until you leave the cab.

Re: Scripting Question - Can't keep headlights on

Posted: Wed Feb 04, 2015 6:49 pm
by nschichan
Good evening,

Just to echo to add to what has been already said, the children names Fwd_Headlight_X / Bwd_Headlight_X where X is a number are reserved by the simulation and are controled by the value of the "Headlights" control.

To work around that, you could try to set it all the time during the Update() function, but it impact performance slightly (by how much I don't know, but it will add up to the rest so the less you change state in the Update function, the better).

Or you could rename the ChildName field for this object in the engine blue print to something else, for instance:

Code: Select all

<ChildName>Fwd_Headlight_1</Childname>
would become

Code: Select all

<ChildName>MyHeadlight1</Childname>
In the same fashion, Bwd_Taillight_X and Fwd_Taillight_X are also overriden by the core game depending on the Headlight control value so that it lights up when the tail lights are to be lit on a given side of the engine.

I hope this helps,

Regards,

Re: Scripting Question - Can't keep headlights on

Posted: Thu Feb 05, 2015 3:42 am
by chrisonline
I appreciate the responses, which have given me some valuable feedback. I certainly "get" that the operation of headlight child objects is more hard-coded and needs further editing to achieve the results I'm looking for.

Having said that, my focus was not really headlights specifically (though admittedly that's what I put in the Subject Line), but a variety of different functions which don't seem to work with my script in the first post. In fact the cab lights are the only items I have managed to get to work correctly. An attempt at conditional placement of a driver (engineer) in the correct "live" cab has also failed miserably so far.

So I still have a lot of work to do. While there are a few guides and tutorials around, there does seem to be quite a dearth of detailed examples, and I guess that will only get worse as new models come along with .out files rather than .lua files