lua command to activate animation?

Discussion of rolling-stock creation & re-painting.

lua command to activate animation?

Unread postby dick8299 » Sun Dec 08, 2013 10:09 am

I created a box car with animated doors, that opened when I pressed either "l" or "r" on the key board. The doors worked fine in TS2013, but do not work correctly in TS2014 (the animation keeps repeating - instead of the doors staying open, they open repeatedly and don't stop until I press the appropriate key again).

I have tried every variation in the input mapper and wagon control container that I can think of and have had no success. I thought maybe there would be a way to activate the animation via a wagon script.

Is there a lua command sequence that would activate an animation when a certain keyboard key is pressed?
User avatar
dick8299
 
Posts: 385
Joined: Sat Feb 28, 2009 7:50 am

Re: lua command to activate animation?

Unread postby mrennie » Sun Dec 08, 2013 12:14 pm

dick8299 wrote:I created a box car with animated doors, that opened when I pressed either "l" or "r" on the key board. The doors worked fine in TS2013, but do not work correctly in TS2014 (the animation keeps repeating - instead of the doors staying open, they open repeatedly and don't stop until I press the appropriate key again).

I have tried every variation in the input mapper and wagon control container that I can think of and have had no success. I thought maybe there would be a way to activate the animation via a wagon script.

Is there a lua command sequence that would activate an animation when a certain keyboard key is pressed?


For cab view, it's just a matter of putting the key mapping into your Input Mapper Blueprint and then filling in a corresponding control value in the Control container component of the Engine blueprint. You can use LUA if you want to see things animated in outside view. An alternative is to give your control value an "exterior animation" interface element to accompany the interior animation, so that both run at the same time. I prefer to use LUA because it gives me better control over the way the animations play.

Here's a sample of some LUA script to open the storm window in exterior view in the Consolidation:

Code: Select all

gStormWindowRightValue = 0.0
gPrevStormWindowRightValue = 0.0
cStormWindowAnimSensitivity = 2 -- adjust until the window opens the same amount in cab view and exterior view

function UpdateStormWindowRight()

   gStormWindowRightValue = Call ("*:GetControlValue", "StormWindowRight",0);   
      
   if gStormWindowRightValue ~= gPrevStormWindowRightValue then
      
      increment = (gStormWindowRightValue - gPrevStormWindowRightValue)*cStormWindowAnimSensitivity;
      
      Call ("AddTime", "StormWindowRight", increment);
 
      gPrevStormWindowRightValue = gStormWindowRightValue;
  end
end -- function UpdateStormWindowRight

function OnControlValueChange ( name, index, value )
...
   ------------------------
   -- Right Storm Window --
   ------------------------
   if name == "StormWindowRight" then   
           UpdateStormWindowRight();
   end

end



If your animation is repeating, you've probably used an Exterior animation and accidentally set the "Anim style" to something other than "Once".

Hope that helps.

!*cheers*!

Mike
User avatar
mrennie
 
Posts: 3214
Joined: Wed May 30, 2012 12:22 pm

Re: lua command to activate animation?

Unread postby Chacal » Sun Dec 08, 2013 6:15 pm

What is special in your request is that you want to animate a box car, not something in the engine cab.
This means the engine script has to pass the event to the car script.

There's exactly that in the free Britkit's Russel Snow Plow kit.
When the snow plow is coupled with the specially adapted RS3, hitting the "M" key will deploy the plow wings, and "Shift-M" will retract them.
There are four possible positions for the wings.

The key is mapped in the RS3 input mapper like this:
Code: Select all
            <iInputMapper-cInputMapEntry d:id="84499184">
               <State d:type="sInt32">0</State>
               <Device d:type="cDeltaString">Keyboard</Device>
               <ButtonState d:type="cDeltaString">ButtonDown</ButtonState>
               <Button d:type="cDeltaString">Key_M</Button>
               <ShiftButton d:type="cDeltaString">NoShift</ShiftButton>
               <Axis d:type="cDeltaString">NoAxis</Axis>
               <Name d:type="cDeltaString">IncreaseControlStart</Name>
               <Parameter d:type="cDeltaString">Winter</Parameter>
               <NewState d:type="sInt32">0</NewState>
            </iInputMapper-cInputMapEntry>
            <iInputMapper-cInputMapEntry d:id="83599044">
               <State d:type="sInt32">0</State>
               <Device d:type="cDeltaString">Keyboard</Device>
               <ButtonState d:type="cDeltaString">ButtonUp</ButtonState>
               <Button d:type="cDeltaString">Key_M</Button>
               <ShiftButton d:type="cDeltaString">NoShift</ShiftButton>
               <Axis d:type="cDeltaString">NoAxis</Axis>
               <Name d:type="cDeltaString">IncreaseControlStop</Name>
               <Parameter d:type="cDeltaString">Winter</Parameter>
               <NewState d:type="sInt32">0</NewState>
            </iInputMapper-cInputMapEntry>
            <iInputMapper-cInputMapEntry d:id="28691744">
               <State d:type="sInt32">0</State>
               <Device d:type="cDeltaString">Keyboard</Device>
               <ButtonState d:type="cDeltaString">ButtonDown</ButtonState>
               <Button d:type="cDeltaString">Key_M</Button>
               <ShiftButton d:type="cDeltaString">Shift</ShiftButton>
               <Axis d:type="cDeltaString">NoAxis</Axis>
               <Name d:type="cDeltaString">DecreaseControlStart</Name>
               <Parameter d:type="cDeltaString">Winter</Parameter>
               <NewState d:type="sInt32">0</NewState>
            </iInputMapper-cInputMapEntry>
            <iInputMapper-cInputMapEntry d:id="83404024">
               <State d:type="sInt32">0</State>
               <Device d:type="cDeltaString">Keyboard</Device>
               <ButtonState d:type="cDeltaString">ButtonUp</ButtonState>
               <Button d:type="cDeltaString">Key_M</Button>
               <ShiftButton d:type="cDeltaString">Shift</ShiftButton>
               <Axis d:type="cDeltaString">NoAxis</Axis>
               <Name d:type="cDeltaString">DecreaseControlStop</Name>
               <Parameter d:type="cDeltaString">Winter</Parameter>
               <NewState d:type="sInt32">0</NewState>
            </iInputMapper-cInputMapEntry>


As you can see, hitting the keys will increase/decrease the "Winter" control value.
The engine lua script reacts to this value changing:
Code: Select all
function OnControlValueChange ( name, index, value )
   if Call( "*:ControlExists", name, index ) then
      Call( "*:SetControlValue", name, index, value )
   end
   -- Change MOW variables when the player uses the controls
   if name == "Winter" then
      gMOWwings   = value
      if gMOWwings < 0  then
         gMOWwings = 0
      elseif gMOWwings > 4  then
         gMOWwings = 4
      end
   end
...

 end

As you can see the script updates the gMOWwings variable according to the "winter" control value, and limits its value.
Then you need to send this event to the snow plow:
Code: Select all
-- Russel plow variables
   gMOWwings = 0
   gPrevMOWwings = 0
   PLOWANIM01 =  8318 -- Britkits snow plows only
   PLOWANIM02 =  8319 -- Britkits snow plows only

function Update ( time )
   -- Plow animation code
   if gMOWwings ~= gPrevMOWwings then
      -- Driver has moved the wings, send this info to the plow
      gPrevMOWwings = gMOWwings
      Call( "SendConsistMessage", PLOWANIM01, gMOWwings, 1 )
      Call( "SendConsistMessage", PLOWANIM01, gMOWwings, 0 )
   end
end


Then you need a lua script for the snow plow (Russ-plow_script.lua) that will receive the message and activate the animation:
Code: Select all
function Initialise ()
  PLOWANIM01 =  8318 -- Britkits snow plows only
  PLOWANIM02 =  8319 -- Britkits snow plows only
  gMOWanim = 0 -- animation frame for wings (0 .. 4)
  Call("BeginUpdate")
end

function OnConsistMessage ( msg, argument, direction )
  if ( msg == PLOWANIM01 )  then
     gMOWanim = tonumber(argument)
  else
     -- send the message further down the consist
     Call ( "SendConsistMessage", msg, argument, direction )
  end
end

function Update( time )
  if gMOWanim == 0 then
    Call ( "ActivateNode", "wings-closed", 1 )
    Call ( "ActivateNode", "wings-open01", 0 )
    Call ( "ActivateNode", "wings-open02", 0 )
    Call ( "ActivateNode", "wings-open03", 0 )
    Call ( "ActivateNode", "wings-open04", 0 )
  elseif gMOWanim == 1 then
    Call ( "ActivateNode", "wings-closed", 0 )
    Call ( "ActivateNode", "wings-open01", 1 )
    Call ( "ActivateNode", "wings-open02", 0 )
    Call ( "ActivateNode", "wings-open03", 0 )
    Call ( "ActivateNode", "wings-open04", 0 )
  elseif gMOWanim == 2 then
    Call ( "ActivateNode", "wings-closed", 0 )
    Call ( "ActivateNode", "wings-open01", 0 )
    Call ( "ActivateNode", "wings-open02", 1 )
    Call ( "ActivateNode", "wings-open03", 0 )
    Call ( "ActivateNode", "wings-open04", 0 )
  elseif gMOWanim == 3 then
    Call ( "ActivateNode", "wings-closed", 0 )
    Call ( "ActivateNode", "wings-open01", 0 )
    Call ( "ActivateNode", "wings-open02", 0 )
    Call ( "ActivateNode", "wings-open03", 1 )
    Call ( "ActivateNode", "wings-open04", 0 )
  elseif gMOWanim == 4 then
    Call ( "ActivateNode", "wings-closed", 0 )
    Call ( "ActivateNode", "wings-open01", 0 )
    Call ( "ActivateNode", "wings-open02", 0 )
    Call ( "ActivateNode", "wings-open03", 0 )
    Call ( "ActivateNode", "wings-open04", 1 )
  end
end


In this particular case, it does not run an animation but activates a child node in the geometry file, but you get the idea.

Also I remember a scenario for Portland Terminal where there's an animated crane on a flatcar.
Its legs extend/retract when you hit a key. Probably the same technique
Over the hill and gathering speed
Chacal
Site Admin
 
Posts: 6599
Joined: Tue Jul 05, 2011 1:11 pm
Location: Quebec, Canada

Re: lua command to activate animation?

Unread postby mrennie » Sun Dec 08, 2013 7:45 pm

Good stuff there Chacal !!*ok*!!

I did something similar with the "AI Helper" Consolidation. It receives instructions from the player-driven loco at the head of the consist, so that it sets its throttle the same as the player (and that's how you get the sounds to come from the rear loco). It works with HSC consists because those cars are scripted to pass messages on down the consist.
User avatar
mrennie
 
Posts: 3214
Joined: Wed May 30, 2012 12:22 pm

Re: lua command to activate animation?

Unread postby dick8299 » Tue Dec 10, 2013 2:20 pm

I resolved my issue by making separate animations for opening and closing of the doors, used anim style 'once' in the wagon and engine bin files, and 'togglecontrols' in the input mapper.

Thanks for everyone's input.
User avatar
dick8299
 
Posts: 385
Joined: Sat Feb 28, 2009 7:50 am

Re: lua command to activate animation?

Unread postby mrennie » Tue Dec 10, 2013 3:35 pm

dick8299 wrote:I resolved my issue by making separate animations for opening and closing of the doors, used anim style 'once' in the wagon and engine bin files, and 'togglecontrols' in the input mapper.

Thanks for everyone's input.


Glad that you got it working :D
User avatar
mrennie
 
Posts: 3214
Joined: Wed May 30, 2012 12:22 pm

Re: lua command to activate animation?

Unread postby Chacal » Tue Dec 10, 2013 9:53 pm

dick8299 wrote:I resolved my issue by making separate animations for opening and closing of the doors, used anim style 'once' in the wagon and engine bin files, and 'togglecontrols' in the input mapper.


Which input mapper ? Engine or wagon?
Over the hill and gathering speed
Chacal
Site Admin
 
Posts: 6599
Joined: Tue Jul 05, 2011 1:11 pm
Location: Quebec, Canada

Re: lua command to activate animation?

Unread postby dick8299 » Wed Dec 11, 2013 10:11 am

Chacal wrote:Which input mapper ? Engine or wagon?


The input mapper goes with the engine, the wagon does not need one.
User avatar
dick8299
 
Posts: 385
Joined: Sat Feb 28, 2009 7:50 am

Re: lua command to activate animation?

Unread postby Chacal » Wed Dec 11, 2013 11:16 pm

So how do you tell the wagon about the key press? Is there some lua code sending messages like the code I posted for the Russell snow plow?
Over the hill and gathering speed
Chacal
Site Admin
 
Posts: 6599
Joined: Tue Jul 05, 2011 1:11 pm
Location: Quebec, Canada

Re: lua command to activate animation?

Unread postby dick8299 » Thu Dec 12, 2013 8:38 am

Chacal wrote:So how do you tell the wagon about the key press? Is there some lua code sending messages like the code I posted for the Russell snow plow?

You have the same control container in the engine and the wagon and where it says apply to consist, you put true. Here is one of the control container entries:

<cControlContainerBlueprint-cControlValue d:id="14959648">
<ControlName d:type="cDeltaString">RightDoorOpen</ControlName>
<DefaultValue d:type="sFloat32" d:alt_encoding="0000000000000000" d:precision="string">0</DefaultValue>
<MinimumValue d:type="sFloat32" d:alt_encoding="0000000000000000" d:precision="string">0</MinimumValue>
<MaximumValue d:type="sFloat32" d:alt_encoding="000000000000F03F" d:precision="string">1</MaximumValue>
<BriefDescription d:type="cDeltaString">Open doors on right side</BriefDescription>
<DetailedDescription d:type="cDeltaString">Open doors on right side</DetailedDescription>
<ApplyToConsist d:type="cDeltaString">eTrue</ApplyToConsist>
<InterfaceElements>
<cControlContainerBlueprint-cExteriorAnimation d:id="14998384">
<ElementName d:type="cDeltaString">RightDoor</ElementName>
<DifficultyType d:type="cDeltaString">Expert</DifficultyType>
<AnimationID d:type="cDeltaString">RightDoorOpen</AnimationID>
<AnimStyle d:type="cDeltaString">Once</AnimStyle>
</cControlContainerBlueprint-cExteriorAnimation>
</InterfaceElements>
</cControlContainerBlueprint-cControlValue>
User avatar
dick8299
 
Posts: 385
Joined: Sat Feb 28, 2009 7:50 am

Re: lua command to activate animation?

Unread postby OldProf » Thu Dec 12, 2013 9:46 am

dick8299 wrote:The input mapper goes with the engine, the wagon does not need one.


So, the hobo in the yard can't open the boxcar doors? Bummer!
Tom Pallen (Old Prof)

{Win 10 Home 64-bit; Intel Core i7 6700 @ 3.40GHz; 16.0GB Single-Channel @ 1063 MHz (15-15-15-364); 2047MB NVIDIA GeForce GTX 960}
User avatar
OldProf
 
Posts: 2743
Joined: Wed Sep 09, 2009 10:09 am

Re: lua command to activate animation?

Unread postby Chacal » Thu Dec 12, 2013 12:46 pm

Thanks, it seems the lua isn't necessary for this then.
This may explain some problems I'm having, maybe I have redundant code.
Over the hill and gathering speed
Chacal
Site Admin
 
Posts: 6599
Joined: Tue Jul 05, 2011 1:11 pm
Location: Quebec, Canada


Return to Rolling-Stock Design

Who is online

Users browsing this forum: No registered users and 16 guests