Jump to content

[SOLVED] onCrewKilled and onKerbalStatusUpdate Help


Recommended Posts

Hi. I'm basically working on a simple plugin and i ran into this :

calling onCrewKilled or onKerbalStatusChange

void onCrewKilled(EventReport e)
{
Debug.Log("KSN onCrewKilled!");
}

onKerbalStatusChange looks the same. Just a debug.log line to check when its called.

And i get this:

"[00:02:50]: Bill Kerman was killed.

KSN onCrewKilled!

KSN onCrewKilled!

KSN onCrewKilled!

KSN onCrewKilled!

KSN onCrewKilled!

KSN onCrewKilled!

KSN onCrewKilled!

KSN onCrewKilled!

KSN onCrewKilled!

KSN onCrewKilled!

KSN onCrewKilled!

KSN onCrewKilled!

KSN onCrewKilled!"

Same applies for onKerbalStatusChange.. can someone explain why its running more than one time? and how do i get around this to call/run it only once? The test rocket is just a mk1 cmd pod + srb used to kill the kerbal.

onCrewKilled / onKerbalStatusChange is being called on Awake - and yes, i have no clue what i'm really doing since this is my 1st plugin and i've never tried C# before.

The plugin i'm trying to make should take funds when a kerbal dies (even by TACLS & when vessel is not focused, thats why i've hoped to use onKerbalStatusChange).. so i was gonna use so any ideas or tips are welcome!

Thank you!

Edited by DJK
Problem Solved
Link to comment
Share on other sites

It looks like it works as expected to me, called once for each Kerbal killed. Can you show more code? The phrase "onCrewKilled / onKerbalStatusChange is being called on Awake" makes me think you haven't registered the event in GameEvents or there's something else going wrong in your code

Link to comment
Share on other sites

Thanks for the reply xEvilReeperx.

Here is the simple version of the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using System.Threading.Tasks;
using UnityEngine;
using KSP;

namespace DJK_KS
{
[KSPAddon(KSPAddon.Startup.EveryScene, false)]
public class KSN : MonoBehaviour
{
public void Awake()
{
GameEvents.onCrewKilled.Add(onCrewKilled);
}

private void onCrewKilled(EventReport e)
{
Debug.Log("KSN onCrewKilled!");
}
}
}

Which does this:

"[00:01:42]: Jebediah Kerman was killed.

KSN onCrewKilled!

KSN onCrewKilled!

KSN onCrewKilled!

KSN onCrewKilled!

KSN onCrewKilled!

KSN onCrewKilled!

KSN onCrewKilled!

KSN onCrewKilled!

KSN onCrewKilled!

KSN onCrewKilled!

KSN onCrewKilled!

KSN onCrewKilled!

KSN onCrewKilled!

KSN onCrewKilled!"

Thats on a clean KSP install (no mods).. new game (sandbox this time) with no kerbal killed so far. Jeb was the first and only crew member on a mk1 pod + srb that died.. shouldn't "KSN onCrewKilled!" be called just once in debug? Like:

"[00:01:42]: Jebediah Kerman was killed.

KSN onCrewKilled!"

and not 14 times?

Link to comment
Share on other sites

The Problem is that Awake() Gets executed Everytime the Scene changes, So your onCrewKilled(EventReport) Function gets registered Multiple times to the onCrewKilled Event.

The KSPAddon Constructor has a once Argument, true means it only gets executed once and false means it gets executed everytime your Startup argument happens.

Link to comment
Share on other sites

Is this what your log actually looks like (the line repeated 10 or so times), or does it continually print this?

If it repeats a certain number of times that's probably because you are registering the callback every time the scene changes, but never removing it.

Put this in your OnDestroy() method.


GameEvents.onCrewKilled.Remove(onCrewKilled);

Link to comment
Share on other sites

The Problem is that Awake() Gets executed Everytime the Scene changes, So your onCrewKilled(EventReport) Function gets registered Multiple times to the onCrewKilled Event.

The KSPAddon Constructor has a once Argument, true means it only gets executed once and false means it gets executed everytime your Startup argument happens.

Changing the argument to true worked as a charm :D

Putting this:

GameEvents.onCrewKilled.Remove(onCrewKilled);

in OnDestroy() method had no effect.

Thank you for the help guys, i really appreciate it!

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