Jump to content

Making a part module, having trouble with persistance. Please help!


Whyren

Recommended Posts

Hi all! I'm working on making my first part module. The intent is to make a very simple module that will record the last time the vehicle was active and then, the next time the vehicle is loaded after you pilot other vessels, the amount of time you were away is calculated and a resource you should have been generating will be calculated and added to the ship. The code all seems to work, except that the "lastUpdated" time is still 0 when the vessel is reloaded. It seems like it isn't being saved or isn't being loaded. Does anyone have any advice for me?

Part Config

MODULE
{

name = AFKResources
Resource = Snacks
RatePerSecond = 1
}

Part Module

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


public class AFKResources : PartModule
{
[KSPField(guiActive = true)]
public string Resource = "";


[KSPField(guiActive = true)]
public float RatePerSecond = 0.0f;


[KSPField(isPersistant = true, guiActive = true)]
public double lastUpdate = 0;


public override void OnStart(PartModule.StartState x)
{



Debug.LogWarning("////////////////////////////////////////////////////////////////");
Debug.Log("AFKResources OnStart. lastUpdate: " + lastUpdate.ToString() + " Resource: " + Resource + " Start State: " + x.ToString());
Debug.LogWarning("////////////////////////////////////////////////////////////////");
if (lastUpdate > 0)
{
if (lastUpdate < Planetarium.GetUniversalTime())
{
double passedTime = Planetarium.GetUniversalTime() - lastUpdate;
double resourceAmount = passedTime * RatePerSecond;
double waste = part.RequestResource(Resource, resourceAmount * -1);
lastUpdate = 0;


ScreenMessages.PostScreenMessage(part.vessel.name + ": " + resourceAmount.ToString() + " Snacks Harvested", 5f, ScreenMessageStyle.UPPER_CENTER);
Debug.LogWarning("////////////////////////////////////////////////////////////////");
Debug.Log("AFKResources WORKING. Harvset: " + resourceAmount.ToString() + " Waste: " + waste.ToString());
Debug.LogWarning("////////////////////////////////////////////////////////////////");
}
}


}
public override void OnUpdate()
{
lastUpdate = Planetarium.GetUniversalTime();
}


public override void OnSave(ConfigNode node)
{
node.SetValue("lastUpdate", lastUpdate.ToString());
Debug.LogWarning("////////////////////////////////////////////////////////////////");
Debug.Log("AFKResources SAVED. lastUpdate: " + lastUpdate.ToString());
Debug.LogWarning("////////////////////////////////////////////////////////////////");
}


public override void OnLoad(ConfigNode node)
{
if(node.HasNode("lastUpdate")){lastUpdate = Double.Parse(node.GetValue("lastUpdate"));}
Debug.LogWarning("////////////////////////////////////////////////////////////////");
Debug.Log("AFKResources LOADED. lastUpdate: " + lastUpdate.ToString());
Debug.LogWarning("////////////////////////////////////////////////////////////////");
}


}

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