Jump to content

[ANSWERED] Detecting Animation duration


Recommended Posts

I created a MultiModeConverter module, which enables switching converter setup instead of having multiple converters or multiple converter module in a single converter that can run simultaneously.

Now I've run into an issue. The module seems to work just fine most of the time, but I ran into an issue when the converter has the "ModuleAnimationGroup". I cloned and modified the Karbonite "KA-2500 Particle Collector", which has this animation, and each time it is activated, it automatically activates all converters at the same time....

	MODULE
	{
		name = ModuleAnimationGroup
		deployAnimationName = Deploy
		activeAnimationName = 
		moduleType = Collector
	}
Spoiler

PART:NEEDS[UmbraSpaceIndustries]
{
    // --- general parameters ---
    name = GT_ParticleCollector_250
    module = Part
    author = Warezcrawler

    // --- asset parameters ---
    MODEL
    {
        model = UmbraSpaceIndustries/Karbonite/Assets/RamScoop
        scale = 1.07,1.07,1.07
    }
    rescaleFactor = 1

    // --- node definitions ---
    node_stack_bottom = 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 2

    // --- editor parameters ---
    TechRequired = fuelSystems
    entryCost = 16000
    cost = 150000
    category = Utility
    subcategory = 0
    title = GT Particle Collector 2.5m
    manufacturer = Guybrush Threepwood Industries
    description = After repeatedly having to clear all kinds of gunk out of this air intake, our engineers modified it's design to selectively filter out airborne Karbonite particles.  This version will work on the edge of planetary atmospheres.
    // attachment rules: stack, srfAttach, allowStack, allowSrfAttach, allowCollision
    attachRules = 1,0,0,0,0

    // --- standard part parameters ---
    mass = 1
    dragModelType = default
    maximum_drag = 0.2
    minimum_drag = 0.3
    angularDrag = 2
    crashTolerance = 6
    breakingForce = 250
    breakingTorque = 250
    maxTemp = 2900
    
    MODULE
    {
        name = GTI_MultiModeConverter
        
        availableInFlight = true
        availableInEditor = true
        
        useModuleAnimationGroup = true
    }

    
    MODULE
    {
         name = ModuleResourceConverter
         tag = Karbonite
         ConverterName = Karbonite
         StartActionName = Start Karbonite Collector
         StopActionName = Stop Karbonite Collector
         INPUT_RESOURCE
         {
            ResourceName = ElectricCharge
            Ratio = 80
         }
         OUTPUT_RESOURCE
         {
            ResourceName = Karbonite
            Ratio = 0.8
            DumpExcess = false
         }
    }
    MODULE
    {
         name = ModuleResourceConverter
         tag = DarkGoo
         ConverterName = Dark Goo
         StartActionName = Start Dark Goo Collector
         StopActionName = Stop Dark Goo Collector
         INPUT_RESOURCE
         {
            ResourceName = ElectricCharge
            Ratio = 100
         }
         OUTPUT_RESOURCE
         {
            ResourceName = DarkGoo
            Ratio = 0.001
            DumpExcess = false
         }
    }
    MODULE
    {
         name = ModuleResourceConverter
         tag = Dust
         ConverterName = Dust
         StartActionName = Start Dust Collector
         StopActionName = Stop Dust Collector
         INPUT_RESOURCE
         {
            ResourceName = ElectricCharge
            Ratio = 10
         }
         OUTPUT_RESOURCE
         {
            ResourceName = Dust
            Ratio = 0.1
            DumpExcess = false
         }
    }
    
    MODULE
    {
        name = ModuleAnimationGroup
        deployAnimationName = Deploy
        activeAnimationName = 
        moduleType = Collector
    }

}

So my questions are.

  1. How do I in my plugin disable the behavior of "activating all the converters" when deploying the part - if this is at all possible....
  2. alternatively how do I detect if the animation is finished, then I can invoke my oversteering after that, and regain control.
  3. alternatively how do I detect how long the animation takes (which seem to be the basis of invoking this change), then I can invoke my oversteering after that, and regain control.

alternative 2+3 are not optimal, but acceptable. Maybe there is an alternative I haven't considered yet?

Currently I have hardcoded my plugin to update 2 sec after the module is activate (hence after animation finishes), it works, but is not pretty.

Edited by Warezcrawler
Link to comment
Share on other sites

This is all going to revolve around the Animation Time value. If there is also an Animate State enum like ModuleDeployableSolarPanel.deployState, that would probably also work.

When ModuleAnimateGeneric.animTime =0f, animation is at it's "beginning" state, so in this case the converters are fully retracted.

When it's 1f, they are fully deployed.

Any other value between those two means the converter is actively deploying/retracting and the animation is playing.

So:

1) Check .animTime ==0f or 1f, otherwise lock your code out.

2) .animTime = 1f, animation finished playing.

3) No clue, never looked at that. Not sure it's available as all ModuleAnimateGeneric does is tell the animation made in Blender to stop/start/reverse, I don't know how much information of the animation itself it exposes.

D.

 

Edited by Diazo
Link to comment
Share on other sites

You need to find the actual Unity Animation component being played by the ModuleAnimationGroup. And if it isn't accessible you should be able to manually retrieve it from the part using animation name specified:

Animation anim = part.FindModelAnimators("Animation Name")[0];

 

Then you can use all of the properties of that animation (length, normalized time, isplaying, etc...) to figure out what's going on.

As for disabling ModuleAnimationGroup's activating of things when it finishes the animation, that might not be possible, since that is one of the primary purposes of the module (what it's actually doing is activating all of the Part Modules on that same part that implement IAnimatedModule).

Link to comment
Share on other sites

@Diazo and @DMagic Thanks for your advice! After a lot of messing around I think I got it to work. I ended up going about it like DMagic suggested.

I anybody out there needs the answer, I'll try and sum it up.

//Initializing the objects
MAG = part.FindModuleImplementing<ModuleAnimationGroup>();			//Finding the ModuleAnimationGroup
Anim = part.FindModelAnimator(MAG.deployAnimationName);				//Finding the Animation

then in my event I use invoke to make sure I check when the animation is finished

//Getting the lenght of the animation. I add 0.1 to get on the other side of the animation.
try { AnimLength = Anim.clip.length + 0.1f; } catch { AnimLength = 1f; }		//I'm unsure if it might in some cases not exist, so I use a try statement

//Then I invoke my update of the converter modules when it is expected to be finished
Invoke("ModuleAnimationGroupEventInvoke", AnimLength);

I have my invoke call itself until the animation is finished with a very small interval.

        private void ModuleAnimationGroupEventInvoke()
        {
			//Here I use the ".isPlaying" boolean of the animation to check if it is finished, and ready to be updated
            if (Anim.isPlaying == false)
            {
                Debug.Log("'ModuleAnimationGroupEventInvoke': not deployed");
                updateConverter(silentUpdate: true);

                //reset invoke counter
                _InvokeCounter = 0;
            }
            else
            {
                //If no ready, I recall the invoke shortly again until update could be performed
                if (_InvokeCounter < 60) { Invoke("ModuleAnimationGroupEventInvoke", 0.01f); _InvokeCounter++; } else { _InvokeCounter = 0; }
            }
        }

In this way I was able to take control over the converter modules, even with the "ModuleAnimationGroup" module present. It's not absolutely pretty, but it works. My invoke usually don't reinvoke so far.

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