Jump to content

Custom Engine Activation with Animation


Recommended Posts

Hello everybody...

Still trying to figure out how I possibly could get this to work...

I read all the resources about Plugin-Developing I found, but

I don't really get it.

What i intend to do:

I built a engine that folds it's nozzle. (Save space while stored)

Used ModuleGenericAnimator and everything worked beautifully...

But:

Now I wanted to add a custom Engine Activation routine:

On 1st activation the engine should unfold and activate after the animation is complete

On deactivation and every other activation it would stay in it's unfolded state.

How could I elegantly translate this into working code?

Edited by MWJ
Link to comment
Share on other sites

Use some Boolean logic- check to see if the engine is active and that another variable (deployed) is false. When the engine is first activated, play the animation and then set deployed to true. Pretty basic stuff, hope you can get it to work. :)

Link to comment
Share on other sites

Works fine now.

Thanks for the tips.

Maybe this could be merged with the "Help a fellow Dev"-Thread? Saw that one a bit too late.

Here's the Code, not commented, but it's not that complex...


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;


public class ModuleEngineFolder : PartModule
{
[KSPField]
public string AnimationName = "hydrashift";
public bool folded = true;
public bool thrustcut = false;
float MaxThrustRest;
float MinThrustRest;

ModuleEngines engine;

bool EngineIgnited
{
get
{
return engine.EngineIgnited;
}
}

Animation foldinganim
{
get
{
return part.FindModelAnimators(AnimationName)[0];
}
}

public override void OnStart(PartModule.StartState state)
{
if (part.Modules.Contains("ModuleEngines"))
engine = part.Modules["ModuleEngines"] as ModuleEngines;
}

public override void OnUpdate()
{

if (folded == true)
{
if (thrustcut == false)
{
MaxThrustRest = engine.maxThrust;
MinThrustRest = engine.minThrust;
engine.minThrust = 0;
engine.maxThrust = 0;
thrustcut = true;
}
}
if (engine.EngineIgnited == true)
{
if (folded == true)
{
foldinganim[AnimationName].speed = 1;
foldinganim.Play(AnimationName);
folded = false;
}
}
if (folded == false)
{
if (foldinganim.isPlaying == false)
{
engine.minThrust = MinThrustRest;
engine.maxThrust = MaxThrustRest;
}
}
}
}

Link to comment
Share on other sites

Ok. Thanks for tip.

Somehow I had the impression part activation was equal to part loading rather than staging / in game activation.

Am I right to say the main advantage with "onActive" is that the code won't run every frame and therefore is less heavy on the CPU?

Link to comment
Share on other sites

Somehow I had the impression part activation was equal to part loading rather than staging / in game activation.

OnAwake is called to initialise the plugin

OnStart is called when that particular copy of the part is created (including on scene change, e.g. editor to flight)

OnLoad is called when the part data needs to be loaded from a ConfigNode (config file, craft file, persistance file, save file)

OnActive is called when the part is set to the active state (staging, action groups etc.) and it also seems to be called when an active part is on a ship that has just performed a docking operation

Am I right to say the main advantage with "onActive" is that the code won't run every frame and therefore is less heavy on the CPU?

The main advantage is code clarity, with something simple like that the effect on performance isn't going to be noticeable.

Edited by EndlessWaves
Link to comment
Share on other sites

Well, here is the final "OnActive" Code I am using.

Pictures of the Engine can be found here


using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;


public class ModuleEngineFolder : PartModule
{
[KSPField]
public string AnimationName = "hydrav2shift";
public bool folded = true;
public bool thrustcut = false;
float MaxThrustRest;
float MinThrustRest;

ModuleEngines engine;

Animation foldinganim
{
get
{
return part.FindModelAnimators(AnimationName)[0];
}
}

public override void OnStart(PartModule.StartState state)
{
if (part.Modules.Contains("ModuleEngines"))
engine = part.Modules["ModuleEngines"] as ModuleEngines;
foldinganim[AnimationName].layer = 2;
}

public override void OnActive()
{
if (thrustcut == false)
{
MaxThrustRest = engine.maxThrust;
MinThrustRest = engine.minThrust;
engine.minThrust = 0;
engine.maxThrust = 0;
thrustcut = true;
}

if (folded == true && thrustcut == true)
{
StartCoroutine(timedanimator());
}
}

IEnumerator timedanimator()
{
foldinganim[AnimationName].speed = 1;
foldinganim.Play(AnimationName);
folded = false;
yield return new WaitForSeconds(foldinganim[AnimationName].length);
engine.maxThrust = MaxThrustRest;
engine.minThrust = MinThrustRest;
}
}

The only problem I'm encountering is a non-working emissive heat animation... (Animation works if toggled by "ModuleAnimateGeneric")

The Engine seems to produce no heat at all, not even with ridiculously high heating-settings.

Maybe because the heat-function is based on maxThrust and I set it to 0 on activate?

And a minor thing, the fairing is visible in the part preview in VAB, even tough it's invisible In-VAB editor window and In-Game as long as the bottom node isn't used. (MeshRenderer and FairingObject turned off in Unity)

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...