Jump to content

[ANSWERED] Something very unique for a particular game


Recommended Posts

Does anyone know something very unique for a particular game started in KSP? The uniqueness must satisfy the following:

1. If I start a new game the value must remain the same through out this game. It shouldn't depend on settings etc.

2. If I delete a game and start a new one with the same name and settings, the value must be different.

For now I've found only one such value: the creation date of the game's save folder. But it is not very good because a user may back his game up, moving and recreating the folder. Additionally I'll have to mention everywhere "in clear form" that my mod reads outside its folder not to steal something from a user's PC.

May be there's something accessible within KSP public API?

Edited by Ser
answered
Link to comment
Share on other sites

Try this :


using System.IO;

//.....

[KSPAddon(KSPAddon.Startup.SpaceCentre, false)]
public class SpaceCentreLoading : MonoBehaviour
{
int GameValue;
string persistentFileName = "test.txt"; // full name of the unique file including extension. The file is stored in the save folder
System.Random random = new System.Random();
void Start()
{
if (File.Exists("saves/" + HighLogic.SaveFolder + "/" + persistentFileName))
{
StreamReader sr = new StreamReader("saves/" + HighLogic.SaveFolder + "/" + persistentFileName);
GameValue = int.Parse(sr.ReadLine());
sr.Close();
}
else
{
int randomValue = random.Next();
TextWriter tw = new StreamWriter("saves/" + HighLogic.SaveFolder + "/" + persistentFileName);
tw.Write(randomValue);
tw.Close();
GameValue = randomValue;
}
}
}

The unique value is stored in GameValue as an integer.

Edited by simon56modder
Link to comment
Share on other sites

simon56modder, Thanks. But why not to store it in the save file by the means of persistence API? I'll have to save my mod's state anyway.

Link to comment
Share on other sites

Are you looking for something that already exists or to create your own?

I think the resource system random seed might work for you.

ResourceScenario.Instance.gameSettings.Seed

You can ask Roverdude, but I think that value is independent of anything else and persists throughout a save file.

Link to comment
Share on other sites


[KSPScenario(ScenarioCreationOptions.AddToAllGames, GameScenes.SPACECENTER, GameScenes.EDITOR, GameScenes.FLIGHT, GameScenes.TRACKSTATION)]
public class MySeedScenario : ScenarioModule
{
[KSPField(isPersistant = true)]
public int seed = 0;

public Start()
{
if (seed == 0)
seed = something;
}
}

Link to comment
Share on other sites

Are you looking for something that already exists or to create your own?

I think the resource system random seed might work for you.

ResourceScenario.Instance.gameSettings.Seed

You can ask Roverdude, but I think that value is independent of anything else and persists throughout a save file.

I'm looking for anything that might satisfy my requirements. I'll try that. Thanks.

@sarbian

Thanks for the script. What are the params in [KSPScenario(ScenarioCreationOptions.AddToAllGames, GameScenes.SPACECENTER, GameScenes.EDITOR, GameScenes.FLIGHT, GameScenes.TRACKSTATION)] ?

Link to comment
Share on other sites

Are you looking for something that already exists or to create your own?

I think the resource system random seed might work for you.

ResourceScenario.Instance.gameSettings.Seed

You can ask Roverdude, but I think that value is independent of anything else and persists throughout a save file.

I've tested the method you've advised and that's exactly what I've been looking for.

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