Jump to content

Persisting KSPField that is an enum


Recommended Posts

I have hit a snag trying to ues an enum as the type of a KSPField that needs to be persisted. My code looks a bit liek this:


public enum DockingPortHatchStatus
{
NOT_DOCKING_PORT = 0,
DOCKING_PORT_HATCH_CLOSED = 1,
DOCKING_PORT_HATCH_OPEN = 2
}



public class ModuleConnectedLivingSpace : PartModule
{

...

[KSPField(isPersistant = true)]
public DockingPortHatchStatus hatchStatus = DockingPortHatchStatus.DOCKING_PORT_HATCH_CLOSED; // Default to closed hatches. Id the part has been saved this will be overritten when it is loaded. If the part is not saved, and it is not a docking port, then it will be overritten in OnStart.

...

}

In the log I get the error:

Cannot parse Enum value: enumType is not an Enum type.

Does anyone know how to use an emnum as a KSPField that gets persisted?

Link to comment
Share on other sites

This may not be the proper way to do it, but what I'd do is this: Make the KSPField of type int, and when you read or write that enum, cast it either to the int or enum type.

Something like...

[KSPField(isPersistant = true)]
public int hatchStatus = 1; //DOCKING_PORT_HATCH_CLOSED

...
DockingPortHatchStatus hatchStatusEnum = (DockingPortHatchStatus) hatchStatus;
if (DockingPortHatchStatus.DOCKING_PORT_HATCH_OPEN == hatchStatusEnum)
{
hatchStatus = (int) DockingPortHatchStatus.DOCKING_PORT_HATCH_CLOSED;
}

KSPField only supports limited types, for example, floats are good but doubles are not (had to do some funny stuff to work around a rounding error from that :) )

Edited by notfirestorm
Added code tags
Link to comment
Share on other sites

An alternative is to make the KSPField a string, and have a private field to hold the enum. I *think* you can do something like


if(hatchStatus == DockingPortHatchStatus.DOCKING_PORT_HATCH_OPEN.ToString())
{
hatchStatusEnum = DockingPortHatchStatus.DOCKING_PORT_HATCH_OPEN;
}
...etc...

I don't know if the ToString is going to be just "DOCKING_PORT_HATCH_OPEN" in this case, or if it includes the enum name as well.

Link to comment
Share on other sites

Enum <--> string Conversion is very, VERY slow. It's using Reflection and that explains it all. The best way to do it is to create a Dictionary<EnumType, string> and store the enum/string pairs in there, and simply convert from one another using the dictionary. Dictionaries are quite fast and convenient.

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