Jump to content

[Solved] Causing Engine Failure


Recommended Posts

I'm trying to make engines have a random chance of failure in which an engine simply doesn't produce thrust.  However, I can't seem to get this to work in a way that I like.

So far, I have tried setting the maxThrust of all ModuleEngines on the part to zero - which didn't work, I have tried removing all ModuleEngines - which didn't work, and I have tried shutting down all ModuleEngines whenever they become activated - which works, but produces an ungodly death rattle that I don't want.

 

This is the meat of the code I have currently.  Does anyone have any insight into what I'm doing wrong?

public override void OnStart(PartModule.StartState state)
{
    if (state == StartState.Editor) return;

    if (IsNewPart)
    {
        IsActive = true;
        IsNewPart = false;
        if (UnityEngine.Random.value < FailChance)
            HasFailed = true;
    }

    if (HasFailed)
        FailStatus = "Engine Failed";
    else FailStatus = "Engine Working";
}

public override void OnFixedUpdate()
{
    if (this.vessel == null)
        return;

    if (HasFailed)
    {
        foreach (PartModule M in part.Modules)
        {
            ModuleEngines E = M as ModuleEngines;
            if (E == null) continue;
            if(!E.engineShutdown)
                E.Shutdown();
        }
    }
}
Edited by Ontresanas
Thread is solved
Link to comment
Share on other sites

Thanks, that works!  However, now I've discovered that UnityEngine.Random.value doesn't seem to be giving legitimately random values.  The same engines always fail or work regardless of what I do or how many times I launch in sequence (not reverting, actually launching a new rocket).  Is there a "correct" source of randomness that I should be using?

UPDATE: I've dropped using UnityEngine.Random, and am now using a static System.Random object.  This solves the randomness problem.  Now, the hopefully-final question: is there any way to get a list of all the defined parts?  Not on the current vessel, but rather, just a list of every part, like the VAB part selection screen.

Edited by Ontresanas
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...