Jump to content

I CANT KEEP TRACK OF TIME! No, really, I cant get my part to check time passed X P


CiberX15

Recommended Posts

Ok so I am trying to find out how much time has passed so that I can calculate how long a part has been active for. The general idea is when the player clicks a button the variable TimeActive is set to vessel.missionTime which as I understand it is the total number of seconds since the ship/station/probe left the launch pad.

For every tick I then set the variable ResearchTime to vessel.missionTime - TimeActive which means Research time is equal to the amount of seconds since the player clicked the button.

This works perfectly when the player is focused on the ship in qustion. However, when they swich to another ship then switch back, TimeActive = 0, so when it runs its calculation, ResearchTime ends up being something like 50000.

Now what I don't understand is that TimeActive is supposed to be persistent. In fact I have several variables that are set to be persistent, and all but TimeActive work. For example, IsEnabled is a bool used to determine whether the part should be doing anything. This variable is remembered when I go back and forth between ships. But for some reason TimeActive is always defaulted back to 0. I have checked over my code several times to see if I am zeroing it out anywhere but I didn't see anything. I even tried replacing the variable with the words OrangeJuice to make sure I hadn't accidentally overwritten a variable used by one of the parent classes but no change.

Thank you in advance to anyone who can help me figure this out : )

Full Code is below:


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



/// <summary>
/// ScienceTrickle
/// By: CiberX15
/// Special Thanks to Fractal_UK whose KSPInterstellar ScienceModule I reverse engineered
/// to figure out how to work the code
/// </summary>

public class ScienceTrickle : ModuleScienceExperiment
{
[KSPField(isPersistant = true, guiActive = true)]
protected bool IsEnabled;

[KSPField(isPersistant = true, guiActive = false)]
protected double TimeActive;

[KSPField(isPersistant = true, guiActive = false)]
protected double ResearchTime;

[KSPField(isPersistant = true, guiActive = true)]
protected string CurrentPaper;

protected float crew_capacity_ratio;

[KSPField(isPersistant = true, guiActive = false)]
protected double StoredScience;

[KSPField(isPersistant = true, guiActive = true)]
protected double RecordedData;

/// <summary>
/// Called when the part is started by Unity.
/// </summary>
public override void OnStart(StartState state)
{
base.OnStart(state);
experiment.baseValue = 1; //reset experement value
// Check our crew
crew_capacity_ratio = ((float)part.protoModuleCrew.Count) / ((float)part.CrewCapacity);
}

//Begin Research Button
[KSPEvent(guiActive = true, guiName = "Begin Research", active = true)]
public void BeginResearch()
{
if (crew_capacity_ratio == 0) { return; }

TimeActive = vessel.missionTime; //Planetarium.GetUniversalTime();
ResearchTime = 0;
StoredScience = 0;
RecordedData = 0;
CurrentPaper = "";
IsEnabled = true;
}

//Stop Research Button
[KSPEvent(guiActive = true, guiName = "Stop Research", active = true)]
public void StopActivity()
{
IsEnabled = false;
}

//Clear Research Button
[KSPEvent(guiActive = true, guiName = "Clear Research", active = true)]
public void ClearResearch()
{
IsEnabled = false;
ResearchTime = 0;
StoredScience = 0;
RecordedData = 0;
CurrentPaper = "";
}

new public void DeployExperiment()
{
experiment.baseValue = (float)RecordedData;
base.DeployExperiment();
}
/*
new public void ReviewDataEvent()
{
base.ReviewDataEvent();
print("ORANGE JUICE==============================================================");
}

new public void ResetExperiment()
{
base.ResetExperiment();
experiment.baseValue = 1;
ClearResearch();
print("Finished Experiment Reset =================================================");
}

new public void ResetExperimentExternal()
{
base.ResetExperimentExternal();
experiment.baseValue = 1;
ClearResearch();
print("Finished Experiment Reset =================================================");
}*/

//Stop Research Button
[KSPEvent(guiActive = true, guiName = "Debug", active = true)]
public void DebugThing()
{
print ("BEGIN ======================================================");
print ("Research Time " + ResearchTime);
print ("Mission Time" + vessel.missionTime);
print ("Active Time" + TimeActive);

print ("ResearchTime " + ResearchTime);
print ("CurrentPaper " + CurrentPaper);
print ("StoredScience " + StoredScience);
print ("RecordedData " + RecordedData);

print ("Experemrnt BaseValue" + base.experiment.baseValue);
print ("Experemrnt ScienceCap" + base.experiment.scienceCap);
print ("Experemrnt dataScale" + base.experiment.dataScale);
}

public void CheckFindings()
{
//crew mod
float CrewMod = 0;

//calculate crew contributions
foreach (ProtoCrewMember proto_crew_member in part.protoModuleCrew)
{
CrewMod += 1 - proto_crew_member.stupidity;
}

//add collected science
StoredScience += CrewMod * 4;

RecordedData = Mathf.Round((float)StoredScience);
}

//Always Update
public override void OnUpdate()
{
base.OnUpdate();

//if the modual is suposed to be making science!
if(IsEnabled)
{
float ResourceDraw = part.protoModuleCrew.Count;
if(ResourceDraw > 0 && part.RequestResource("ElectricCharge", ResourceDraw * 0.25f) >= ResourceDraw * 0.25f)
{
ResearchTime = vessel.missionTime - TimeActive;
float Percent = (float)(ResearchTime / 86400) * 100;
CurrentPaper = 0.01 * Math.Round(Percent * 100) + "%";

if(ResearchTime >= 86400)
{
int PapersCompleated = (int)(ResearchTime / 86400);
print ("BEGIN ======================================================");
print ("Research Time " + ResearchTime);
print ("Mission Time" + vessel.missionTime);
print ("Time Active " + TimeActive);
print ("Papers Compleated = " + PapersCompleated);
for(int i = 0; i < PapersCompleated;)
{
print (i);
if(part.RequestResource("Snacks", ResourceDraw) >= ResourceDraw)
{
CheckFindings();
}
else
{
//shutdown if out of snacks
IsEnabled = false;
ResearchTime = 0;
CurrentPaper = "";
print ("Not enough snacks, shutting down");
}
i++;
}
ResearchTime = 0;
TimeActive = vessel.missionTime; //(float)Planetarium.GetUniversalTime();
}
}
else
{
//shutdown if out of power
IsEnabled = false;
print ("Not enough power, shutting down");
}
}
}


//In Game Update
public override void OnFixedUpdate()
{
base.OnUpdate();
}
}

Link to comment
Share on other sites

Actually, this is the right section, or rather the Plugin Development subforum is.

What you do want to in BeginResearch() set timeActive = Planetarium.GetUniversalTime(); then whenever you want to find out how long you've been doing research, check Planetarium.GetUnviersalTime() - timeActive

I wouldn't use MET for that.

And *don't change it again*. Keep it like that until the research ends, or you do the next BeginResearch()

Also try giving timeActive a default value (like 0).

Link to comment
Share on other sites

Try this and see what happens:


[KSPField(isPersistant = true, guiActive = false)]
protected double _TimeActive;
protected double TimeActive {
get {
return _TimeActive;
}
set {
print(string.Format("TimeActive was changed from {0} to {1}", _TimeActive, value));
_TimeActive = value;
}
}

BTW, I think you can do Math.Round(Percent, 2) instead of 0.01 * Math.Round(Percent * 100)

Link to comment
Share on other sites

DiEvAl, I just tried that but it didn't work : P

Actually, this is the right section, or rather the Plugin Development subforum is.

What you do want to in BeginResearch() set timeActive = Planetarium.GetUniversalTime(); then whenever you want to find out how long you've been doing research, check Planetarium.GetUnviersalTime() - timeActive

I wouldn't use MET for that.

And *don't change it again*. Keep it like that until the research ends, or you do the next BeginResearch()

Also try giving timeActive a default value (like 0).

Actually I was using Planetarium.GetUniversalTime() with the exact same results. The problem is that TimeActive keeps getting wiped.

Would giving it a default value make it remember its current value?

Also if this is in the wrong spot please feel free to move it to the correct spot 8 O

Edited by CiberX15
Link to comment
Share on other sites

Ok, Update, I just found out that it IS NOT keeping any of the variables I set to be persistent except for IsEnabled. RAWRG!!! I think it is mocking me now 8 <

Edit: You know what? I don't think doubles can be persistent. There are two persistent variables that are being stored, Percent, which is a string, and IsEnabled which is a bool. I am going to try to convert the doubles to floats and see what happens.

More Edit: That was it, apparently doubles CAN NOT be persistent. However floats work fine. So yeah, anyone having issues with their persistent double variables being wiped might want to try floats if they can.

Edited by CiberX15
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...