Jump to content

Forcing the activation of a PartModule


Recommended Posts

Hi,

So I have a module that modifies properties of a ModuleEngines module from GUI buttons. The actual game effects of the code are fine, but there is one issue: the activation of the module. It appears that OnFixedUpdate only runs when a part is actually activated, usually (always?) by a staging event. This is problematic because you can also activate a ModuleEngines module via the GUI right click menu, which activates the engine module just fine outside of staging, but which doesn't enable my addon module.

I know that I can use part.forceactivate() to force the activation of all a part's modules on load, but that forces activation of ALL the modules on a part. In my case that means that the ModuleEngines would also be forced active, which isn't the end of the world, but is also bad because engines will get activated without player input. So, is there an equivalent way to force the activation of a single PartModule? Or am I misunderstanding how this works?

The easiest solution is moving my code from OnFixedUpdate to Unity's default FixedUpdate and so it will run when the actual GameObject was active, which is pretty all the time (evidently with some checks for editor 'n such). However, that's not the most elegant solution, and I'd still like to know whether this is possible.

Link to comment
Share on other sites

I'm wondering the same, I've been using part.force_activate() too since parachutes don't often have other problematic modules on them, but it would be nice if there was another way to go through it. I'll have to test part.activate(stage, vessel) to see if the bool doesn't have some activation code intricated.

Link to comment
Share on other sites

  • 3 months later...

Did you guys get an answer to this? I am also writing a PartModule and want the OnFixedUpdate method to get called whenever it is in flight, regardless of the staging situation. Is there a need solution? I have tried


public override void OnStart(PartModule.StartState st)
{
base.OnStart(st);

// As long as we have not started in the editor, ensure the module is active / enabled.
if (st != StartState.Editor)
{
this.enabled = true;
}
}

but OnFixedUpdate is still not being called :(

Link to comment
Share on other sites

OK, I have a nasty feeling I know what my problem is...

I am creating a class that inherits from ModuleDockingNode. The idea is to add a little extra functionality, but have the original docking node stuff work as it is. So I have code abit like this:


public class ModuleDockingNodeHatch : ModuleDockingNode
{
public override void OnFixedUpdate()
{
// blah
}

public override void OnStart(PartModule.StartState st)
{
this.isEnabled = true;

}
}

So it turns out that the OnStart method is being alled, but the OnFixedUpdate is not. I suspect that this is because ModuleDockingNode does not provide an implimentation of OnFixedUpdate, but it does implement OnStart. Could it be that you can not override a method in the grandparent class is the parent class does not override it? That is nt the behaviour I am used to from c++, but I am not that familiar with c# - can someone wiser and more experienced that I please confirm if that could be the problem? Can anyone suggest a way around it?

Link to comment
Share on other sites

I suspect that this is because ModuleDockingNode does not provide an implimentation of OnFixedUpdate, but it does implement OnStart. Could it be that you can not override a method in the grandparent class is the parent class does not override it?

No, it works as you'd expect it to

What's wrong with FixedUpdate? That's what Part is almost certainly calling OnFixedUpdate from anyway...

public void FixedUpdate()
{
if (HighLogic.LoadedSceneIsFlight)
{
// logic
}
}

Link to comment
Share on other sites

No, it works as you'd expect it to

What's wrong with FixedUpdate? That's what Part is almost certainly calling OnFixedUpdate from anyway...

public void FixedUpdate()
{
if (HighLogic.LoadedSceneIsFlight)
{
// logic
}
}

I am working on using FixedUpdate. The problem is that ModuleDockingNode that I am inheriting from also impliments FixedUpdate as private, so my implementation gets called and the ModuleDockingNode version does not :( I am now using reflection to knock up a hack to call it!

Link to comment
Share on other sites

I am working on using FixedUpdate. The problem is that ModuleDockingNode that I am inheriting from also impliments FixedUpdate as private, so my implementation gets called and the ModuleDockingNode version does not :( I am now using reflection to knock up a hack to call it!

Ah, that's a wrinkle. Well, if anybody frowns on your use of reflection to get private stuff, there's the alternative of creating a child GameObject of your own for the sole reason of having it kick FixedUpdate calls back at you

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