Jump to content

How do I use Events (that I can't override)?


Recommended Posts

I've searched around a bit (probably not enough), but I can't find anything dealing with how to use events that aren't override-able based on the class you extend--that is, I can override, for example OnFixedUpdate on a PartModule just fine, but don't know how to get access to, say, OnVesselChange--so I've given up searching for now and decided to just ask.

I know that the GameEvents class has a list of all the events, but I don't know how to register code to them. (I'll admit, I'm new to C#, so this may be where I'm going wrong.) I'm currently extending MonoBehaviour and I want to access, among others, these events:

  • OnVesselChange
    onCrewOnEva
    onCrewBoardVessel
    onHideUI
    onJointBreak
    onPartCouple

Should I change what I'm extending, or is there a way (delegates come to mind, but I don't see how to make those work) to register a method to the event so I don't have to override it?

Link to comment
Share on other sites

*Sigh*, doing the research might would have taken a lot less time than writing this thread^^

Assuming that you use visual studio: Type GameEvents.onHideUI into your function. Set the cursor into "onHideUI" and press F12. You will see the generated class layout for where "onHideUI" is defined. The interesting part is

    public static EventVoid onHideUI;

You notice its type is EventVoid. Cursor into that term and F12, again. You now see the public methods of an EventVoid class. Most of them are not interesting, but Add(...) is. It takes an EventVoid.OnEvent, and F12 will turn out it is nothing an (imo) badly named empty delegate. So that is where to put your delegate.


GameEvents.onHideUI.Add( ()=>{
/* my code */
});
/*or*/
GameEvents.onHideUI.Add(this.MyObjectsMethod);

Edited by Faark
Link to comment
Share on other sites

Hi, thanks for being condescending!

That was rude, but in fairness, so was your reply. I did do research, and while I saw the Add() function, and did in fact assume it was for adding code, only onHideUI has the type EventVoid. OnVesselChange has type <Vessel>, on both its Add() and its definition, yet this code:


//inside another function
GameEvents.onVesselChange.Add(HOOK_VesselChange);

public static Vessel HOOK_VesselChange() {
print("Vessel changed.");
return FlightGlobals.ActiveVessel;
}

refuses to compile, with the error "The best overloaded match for 'EventData<Vessel>.Add(EventData<Vessel>.OnEvent)' has some invalid arguments" and as someone relatively new to passing functions as variables, that doesn't help at all.

That's not even mentioning the more complex onCrewOnEva(), which has the type <GameEvents.FromToAction <Part, Part>> or the fact that I don't want most of the code I'll be calling on these events to have to return the expected type just to get it to work, which is what the documentation is implying I'd have to do (to me, at least).

...

Of course, after a bit more fiddling around, I realize that the event is passing said types to the function, not expecting it of them; but still, while I'm glad for the help you could have done it in a kinder way.

For anyone having the same problem I was having, and using the example above, this is what you'd do:


//inside another function
GameEvents.onVesselChange.Add(HOOK_VesselChange);

public static void HOOK_VesselChange(Vessel v) {
print("Vessel changed.");
}

Link to comment
Share on other sites

Also of note (here's expecting this to show up before my other reply, but whatever) is that last time I tried to use Lambda functions, KSP wigged out and (I think? Don't remember the specific error) started throwing null references like crazy.


//sumOxygen = connectedOxygen.Sum(res => res.amount);
//sumCO2 = connectedCO2.Sum(res => res.amount);
//sumWater = connectedWater.Sum(res => res.amount);
//sumSnacks = connectedSnacks.Sum(res => res.amount);

Commented out because I want it in there as reference and am using a different (more expensive?) method instead. Supposedly Kethane used this method to sum the resources on the ship, but like I said it's not working for me.

Link to comment
Share on other sites

Hm missionController uses some of these I will list there codes to maybe help you out if you need to use some of the other events.. To get an idea.. The docking one or couple might interest you later if you want to know when a vessel docks. There some others you can use.

GameEvents.onLaunch.Add(this.onLaunch);
GameEvents.onVesselChange.Add(this.onVesselChange);
GameEvents.onCrewKilled.Add(this.onCrewKilled);
GameEvents.onVesselDestroy.Add(this.onVesselDestroy);
GameEvents.onGameSceneLoadRequested.Add(this.onGameSceneLoadRequested);
GameEvents.onCrash.Add(this.onCrash);
GameEvents.onCollision.Add(this.onCollision);
GameEvents.onPartCouple.Add(this.onPartCouple);
GameEvents.onVesselRecovered.Add(this.onRecovered);
GameEvents.onPlanetariumTargetChanged.Add(this.onTargeted);
GameEvents.onVesselCreate.Add(this.onCreate);

might be iterested in knowing that when nobody originally wrote this code he put a

DontDestroyOnLoad(this);

with these. And placed it under the Awake() to start these up on startup.

there are a massive amount of gameEvents though. ;)

Don't know if this will help but oncouple has the same parameters as the EVA event.

this is how it was written for MC.

/// <summary>
/// This event is fired when two vessels dock.
/// </summary>
/// <param name="action">Action.</param>
private void onPartCouple(GameEvents.FromToAction<Part, Part> action) {
if (HighLogic.LoadedSceneIsFlight) {
eventFlags = eventFlags.Add (EventFlags.DOCKED);
Debug.LogError ("Docked FROM: " + action.from.vessel.vesselName);
Debug.LogError ("Docked TO: " + action.to.vessel.vesselName);

Debug.LogError ("Docked FROM ID: " + action.from.vessel.id.ToString());
Debug.LogError ("Docked TO ID: " + action.to.vessel.id.ToString());
}
}

also there is a eva from Vessel to you can use. its called isEVA.

Edited by malkuth
Link to comment
Share on other sites

Also of note (here's expecting this to show up before my other reply, but whatever) is that last time I tried to use Lambda functions, KSP wigged out and (I think? Don't remember the specific error) started throwing null references like crazy.

Damn it, you are right... totally forgot about that. GameEvent can not handle static functions. I filed a bug about that a week ago, but you know... its just a mod-issue...

Lambdas are a little tricky, since the compiler converts it behind the scene to what it thinks is the best depending on your usage of variables... a static function, method of the current class or an entire new class.

Link to comment
Share on other sites

... And placed it under the Awake() to start these up on startup.

there are a massive amount of gameEvents though. ;)

Don't know if this will help but oncouple has the same parameters as the EVA event.

this is how it was written for MC.

...

also there is a eva from Vessel to you can use. its called isEVA.

Start() is actually a better place to put it all, I'd argue, because then you know for a fact everything else has at least initialized. And the massive amount of events is what I'm counting on. :P

Thanks for the isEva note! That'll make my life all sorts of easier. Now I just need to figure out the proper syntax for dynamic resource adding.

Damn it, you are right... totally forgot about that. GameEvent can not handle static functions. I filed a bug about that a week ago, but you know... its just a mod-issue...

Lambdas are a little tricky, since the compiler converts it behind the scene to what it thinks is the best depending on your usage of variables... a static function, method of the current class or an entire new class.

Oh, so that was part of my problem... don't use static in GameEvent, noted. Alright then, now that I've got it working... time to rewrite all my code! D:

Figures that this'd make me have to restructure 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...