Jump to content

Func<,> delegates prevent module loading


Recommended Posts

I've got the following code in my MonoBehavior derived class:

[COLOR="#0000FF"]private[/COLOR] [COLOR="#40E0D0"]Func[/COLOR]<[COLOR="#40E0D0"]AvailablePart[/COLOR], [COLOR="#0000FF"]bool[/COLOR]> filter;

[COLOR="#0000FF"]private bool[/COLOR] NoAero([COLOR="#40E0D0"]AvailablePart[/COLOR] part)
{
[COLOR="#0000FF"]return[/COLOR] part.category == [COLOR="#40E0D0"]PartCategories[/COLOR].Aero;
}

[COLOR="#0000FF"]public void[/COLOR] OnGUI()
{
[COLOR="#0000FF"]if[/COLOR] ([COLOR="#40E0D0"]GUI[/COLOR].Button([COLOR="#0000FF"]new[/COLOR] [COLOR="#40E0D0"]Rect[/COLOR](400, 200, 50, 50), [COLOR="#800000"]"Push Me"[/COLOR]))
{
[COLOR="#40E0D0"]Debug[/COLOR].Log([COLOR="#800000"]"Button"[/COLOR]);
filter = NoAero; [COLOR="#008000"]// PROBLEM LINE[/COLOR]
[COLOR="#40E0D0"]Debug[/COLOR].Log(filter.ToString());
}
}

The whole assembly fails to load when the game starts up. (I can see the failure in KSP.log.) If I comment out the problem line, the assembly loads fine. I originally was using a lambda expression in place of the Func instantiation, but when that wasn't working, I whittled the problem down to this. Many of the functions in the KSP assembly require Func predicates or callbacks. How do I use them?

Edit: I'm currently at work and won't be home to make changes/check on proposed fixes/gather and supply more information until this evening. But I'd appreciate any suggestions in the meantime.

Edited by Mr Shifty
Link to comment
Share on other sites

Most of the callbacks in KSP are used simply by:

Object.callbackVariable += yourMethodName;

&

Object.callbackVariable -= yourMethodName;

This syntax typically only works with events, though Unity might have some strange operator override that uses it for void return type delegates. (I've been coding in C# for over a decade, but the whole Unity "scripting interface grafted into a procedural language" thing is very strange.)

Regardless, I'm using the Func as a predicate (notice it has a bool return type), not a callback, so I can't use that syntax.

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