找回密码
 注册
X系列官方授权正版
搜索
查看: 1219|回复: 2

[求助] 问个关于KSP API的问题。

[复制链接]
发表于 2014-12-13 20:27:36 | 显示全部楼层 |阅读模式
本帖最后由 Showchen 于 2014-12-13 20:27 编辑

C#代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KSP.IO;
using UnityEngine;


namespace TestConfig
{
    class TestModule :PartModule
    {
        [KSPField(isPersistant=true)]
        private int TestInt1 = 0;

        //出错
        //[KSPField(isPersistant = true)]
        //private List<int> TestList1= new List<int>();

        //未保存
        [KSPField(isPersistant = true)]
        private string[] testStringArray = { "", "", "", "" };

        [KSPField(isPersistant = true)]
        private string TestString1 = "Test";


        [KSPField]
        private ConfigNode TestCFG;

        /// <param name="node">A ConfigNode containing the module's parameters from part.cfg or persistent.sfs</param>
        public override void OnLoad(ConfigNode node)
        {
            if (TestCFG == null && node != null)
            {
                TestCFG = node;
            }

            if (File.Exists<TestModule>("Test.cfg", new Vessel()))
            {
                try
                {
                    TestCFG = ConfigNode.Load(IOUtils.GetFilePathFor(this.GetType(), "Test.cfg", new Vessel()));
                }
                catch (Exception e)
                {
                    Debug.LogError("TestModule.OnLoad caught an exception trying to load Test.cfg: " + e);
                }
            }
        }

        public override void OnSave(ConfigNode node)
        {
            try
            {
                if (TestCFG != null) ConfigNode.CreateConfigFromObject(this);
            }
            catch (Exception e)
            {
                Debug.Log("TestModule caught exception in OnSave for " + this.GetType().Name + ": " + e);
            }
        }
    }
}


保存的结果:

        MODULE
        {
                name = TestModule
                isEnabled = True
                TestInt1 = 0
                TestString1 = Test
                EVENTS
                {
                }
                ACTIONS
                {
                }
        }

怎么利用API保存下面这些参数?数组、List和Dictionary都不行,难道得自己写?这样好麻烦啊
        //未保存
        [KSPField(isPersistant = true)]
        private List<int> TestList1 = new List<int>();

        //未保存
        [KSPField(isPersistant = true)]
        private string[] testStringArray = { "", "", "", "" };

发表于 2014-12-15 18:04:57 | 显示全部楼层
本帖最后由 HoneyFox 于 2014-12-15 18:16 编辑

List<int>这个只能自己写序列化和反序列化函数了。如果是Vector 2/3/4的话KSP倒是支持自动解析。
另外,int[] 大概也支持自动解析,你可以试试看。int[]和List<int>互转这个还是很容易的。
回复

使用道具 举报

 楼主| 发表于 2014-12-16 17:20:42 | 显示全部楼层
HoneyFox 发表于 2014-12-15 18:04
List这个只能自己写序列化和反序列化函数了。如果是Vector 2/3/4的话KSP倒是支持自动解析。
另外,int[] 大 ...

封装成类实现了,效率啥的就顾不上了,哈哈。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

Archiver|手机版|小黑屋|DeepTimes.NET 太空游戏站