Jump to content

Adding KSPField attribute to c# property (rather than c# field)


Recommended Posts

I am striggling a bit, I wonder if you can help...

In my Module that inherits from PartModule I have a member field of type bool. I want be hable to display its status on right clicking on the part, but not as "true or false" bu a text of my choosing. So I wrote some code like this:



KSPField(isPersistant = true,guiActive = true, guiName = "*option")]
public bool option;


[KSPField(isPersistant=false, guiActive = true, guiName = "Option Status")]
public string OptionStatus
{
get
{
if (option)
{
return "The option has been set";
}
else
{
return "The option has nit been set";
}
}
}

However when I right click on the part *option is included but Option Status is not. Why is this? I suspect it is because OptionStatus is a property, however KSPField does appear to be value for c# properties as well as c# fields. Any ideas?

Link to comment
Share on other sites

I am striggling a bit, I wonder if you can help...

In my Module that inherits from PartModule I have a member field of type bool. I want be hable to display its status on right clicking on the part, but not as "true or false" bu a text of my choosing. So I wrote some code like this:



KSPField(isPersistant = true,guiActive = true, guiName = "*option")]
public bool option;


[KSPField(isPersistant=false, guiActive = true, guiName = "Option Status")]
public string OptionStatus
{
get
{
if (option)
{
return "The option has been set";
}
else
{
return "The option has nit been set";
}
}
}

However when I right click on the part *option is included but Option Status is not. Why is this? I suspect it is because OptionStatus is a property, however KSPField does appear to be value for c# properties as well as c# fields. Any ideas?

I'm pretty sure KSPField only work for Fields, as it kinda says in the name. However you could do something like this:

KSPField(isPersistant = true,guiActive = true, guiName = "*option")]
public bool option;


[KSPField(isPersistant = false, guiActive = true, guiName = "Option Status)]
public string _optionStatus = string.Empty;

private string optionStatus
{
get
{
this._optionStatus = this.option ? "The option has been set" : "The option has not been set";
return this._optionStatus;
}
}

Then all you need to do is to call optionStatus for your own use, and _optionStatus will update on it's own without you ever touching it and display the right thing in the right click menu.

Link to comment
Share on other sites

  • 4 months later...

It is a compile error to assign an attribute to something it's not designed for. The error says "Attribute 'KSPField' is not valid on this declaration type. It is only valid on 'property, indexer, field' declarations." So it should work on properties. It is possible Squad declared it as applying to properties but didn't put in the code to actually fetch the value. Perhaps this belongs in the bug reports?

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