Jump to content

Help! OnUpdate() never seems to get called


Recommended Posts

I've been trying to make a PartModule that plays an animation when a key is pressed. Unfortunately, OnUpdate() (and OnFixedUpdate() for that matter) never seems to get called, and I can't figure out why.

I've stuck Debug.Log() calls in each overriden method, and they all show up at some point in the Debug Console, so I know that my PartModule is being loaded by KSP. The Debug Console also prints out '[EE.Fuselage.Roof.Bay]: Activated' after I activate the first stage of my test spaceplane, so I'm pretty sure that the part is in an active state at some point as well.


using UnityEngine;

public enum AnimationState : int {

AtBeginning = -2,
AtEnd = 2,
PlayForwards = 1,
PlayBackwards = -1

}

public class ModuleAnimatedPart : PartModule {

[KSPField]
public string animationName;

[KSPField]
public string activationKey;

[KSPField]
public float animationState;

// Hackish workaround to avoid having to convert from the float KSPField and integer enum values
private AnimationState AniState {
get {
return (AnimationState)((int)animationState);
}
set {
animationState = (float)((int)value);
}
}

/// <summary>
/// Constructor style setup.
/// Called in the Part\'s Awake method.
/// The model may not be built by this point.
/// </summary>
public override void OnAwake() {
Debug.Log("[ModAnimatedPart] OnAwake");
}

/// <summary>
/// Called during the Part startup.
/// StartState gives flag values of initial state
/// </summary>
public override void OnStart(StartState State)
{
// Ensure it only runs once per call to Play()
this.animation[this.animationName].wrapMode = WrapMode.Once;
Debug.Log("[ModAnimatedPart] OnStart");
}

/// <summary>
/// Per-frame update
/// Called ONLY when Part is ACTIVE!
/// </summary>
public override void OnUpdate() {
Debug.Log("[ModAnimatedPart] OnUpdate");
Debug.Log("[ModAnimatedPart] animationState: " + this.animationState);
Debug.Log("[ModAnimatedPart] activationKey Pressed: " + Input.GetKey(this.activationKey));
// Getting desperate. Throw an exception just so something will show up
throw new Exception("[ModAnimatedPart] OnUpdate - Test Exception");
switch (AniState) {
case AnimationState.AtBeginning:
if (Input.GetKey(this.activationKey)) {
this.AniState = AnimationState.PlayForwards;
this.animation[this.animationName].speed = 1;
this.animation.Play(this.animationName);
}
break;

case AnimationState.AtEnd:
if (Input.GetKey(this.activationKey)) {
this.AniState = AnimationState.PlayForwards;
this.animation[this.animationName].speed = -1;
this.animation.Play(this.animationName);
}
break;

case AnimationState.PlayForwards:
if (!this.animation.IsPlaying(this.animationName)) {
this.AniState = AnimationState.AtEnd;
}
break;

case AnimationState.PlayBackwards:
if (!this.animation.IsPlaying(this.animationName)) {
this.AniState = AnimationState.AtBeginning;
}
break;
}
}

/// <summary>
/// Per-physx-frame update
/// Called ONLY when Part is ACTIVE!
/// </summary>
public override void OnFixedUpdate() {
Debug.Log("[ModAnimatedPart] OnFixedUpdate");
}

/// <summary>
/// Called when PartModule is asked to save its values.
/// Can save additional data here.
/// </summary>
/// <param name='node'>The node to save in to</param>
public override void OnSave(ConfigNode Node) {
Debug.Log("[ModAnimatedPart] OnSave");
}

/// <summary>
/// Called when PartModule is asked to load its values.
/// Can load additional data here.
/// </summary>
/// <param name='node'>The node to load from</param>
public override void OnLoad(ConfigNode Node)
{
Debug.Log("[ModAnimatedPart] OnLoad");
}

}
using System;



// --- general parameters ---
name = EE_Fuselage_Roof_Bay
module = Part
author = Echo 8 ÉRÀ

// --- asset parameters ---
mesh = model.mu

// --- node definitions ---
node_stack_floor = 0.0, 0.0, 0.0, 0.0, 1.0, 0.0

// --- editor parameters ---
cost = 240
category = 3
subcategory = 0
title = Fuselage Mk III, Cargo Bay, Roof, Bay Doors
manufacturer = Echo Enterprises
description = A bay door equipped roof to a Fuselage Mk 3 compatible cargo bay.

// attachment rules: stack, srfAttach, allowStack, allowSrfAttach, allowCollision
attachRules = 1,0,0,0,1

// --- standard part parameters ---
mass = 0.2
dragModelType = default
maximum_drag = 0.1
minimum_drag = 0.15
angularDrag = 1
crashTolerance = 30
maxTemp = 2900

MODULE {
name = ModuleAnimatedPart
animationName = RoofOpening
activationKey = Z
animationState = -2
}
// Kerbal Space Program - Part Config

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...