saving player information in an external file - unity3d

This is the main menu in my first test 2d game in the world of unity :
I want to save high scores and if the player pressed the "Best Scores" button I want to show them the best scores yet ^^ , so I think I need to use an external file to save this type of information and open it at run time ... How ? which kind of files is the best to perform that ?

In addition to the answer above:
PlayerPrefs doesn't handle and store custom types and collections unless you cast those types to strings or other data, though. It's really useful to convert desired data to JSON and store the JSON string in PlayerPrefs, then fetch and parse that data when you need it again. Doing this will add another layer of complexity, but will also add another layer of protection and the ability to encrypt the data itself. Also, the Web Player is currently the only platform that has limits on PlayerPrefs according to Unity's docs:
There is one preference file per Web player URL and the file size is
limited to 1 megabyte. If this limit is exceeded, SetInt, SetFloat and
SetString will not store the value and throw a PlayerPrefsException.
Source:
http://docs.unity3d.com/Documentation/ScriptReference/PlayerPrefs.html

Writing:
PlayerPrefs.SetInt("score",5);
PlayerPrefs.SetFloat("volume",0.6f);
PlayerPrefs.SetString("username","John Doe");
// Saving a boolean value
bool val = true;
PlayerPrefs.SetInt("PropName", val ? 1 : 0);
PlayerPrefs.Save();
Reading:
int score = PlayerPrefs.GetInt("score");
float volume = PlayerPrefs.GetFloat("volume");
string player = PlayerPrefs.GetString("username");
bool val = PlayerPrefs.GetInt("PropName") == 1 ? true : false;

The simplest solution is using PlayerPrefs. It has limited space and a small set of data that can be saved (int, float, string), but it could be enough for a simple game.
If you need more space or more complex data structures to be saved, than you have to store them on the file system itself (or in a server if you have some backend support). Unity doesn't provide any built-in support for this.

C# is like this
//these variables will send the values in.
public static string exampleString = "hi youtube"; //it needs to be public ans static
public static int exampleInt = 1;
public static float exampleFloat = 3.14;
//these vaibles will collect the saved values
string collectString;
int collectInt;
float collectFloat;
public static void Save () { //this is the save function
PlayerPrefs.SetString ("exampleStringSave", exampleString);
// the part in quotations is what the "bucket" is that holds your variables value
// the second one in quotations is the value you want saved, you can also put a variable there instead
PlayerPrefs.SetInt ("exampleIntSave", exampleInt);
PlayerPrefs.SetFloat ("exampleFloatSave", exampleFloat);
PlayerPrefs.Save ();
}
void CollectSavedValues () {
collectString = PlayerPrefs.GetString ("exampleStringSave");
collectInt = PlayerPrefs.GetInt ("exampleIntSave");
collectFloat = PlayerPrefs.GetFloat ("exampleFloatSave");
)
void Awake () { //this is simialar to the start function
Save ();
CollectSavedValues ();
}
void Update () {
Debug.Log (collectString);
Debug.Log (collectInt);
Debug.Log (collectFloat);
}

Related

store data persistent for learn app with unity

I'm currently working on a language learn app with unity. I want to implement that when you guessed a work (e.g. a number) incorrect, you need to guess the word again in the next iteration. I thought of a way that you store for each word in every play mode a value between +10 to -10 and when an item has a big negative number the word occurrence more often than if it has a big positiv number.
My Problem is that I don't know how to store the data properly. PlayerPrefs are too inconvenient for this problem, and I don't know how to modify a JSON file properly.
Currently, I store the data for the items in a class.
Maybe you could have a structure like:
Numbers
write
zero: -5
one: +3
match
zero: +5
one: +4
Alphabet
write:
A: -10
match:
A: +5
Does anyone have an idea how to solve this problem?
One of the best JSON serialization libraries is Newtonsoft.Json.
You can use your class and serialize an object to JSON object, and then save it as a string to file.
public static string Serialize(object obj)
{
var settings = new JsonSerializerSettings
{
MissingMemberHandling = MissingMemberHandling.Ignore,
NullValueHandling = NullValueHandling.Ignore
};
return JsonConvert.SerializeObject(obj, settings);
}
After that you can save it to file in the Application.persistentDataPath directory.
var text = Serialize(data);
var tmpFilePath = Path.Combine(Application.persistentDataPath, "filename");
Directory.CreateDirectory(Path.GetDirectoryName(tmpFilePath));
if (File.Exists(tmpFilePath))
{
File.Delete(tmpFilePath);
}
File.WriteAllText(tmpFilePath, text);
After that you can read the file at any time using File.ReadAllText and deserialize it to an object.
public static T Deserialize<T>(string text)
{
var settings = new JsonSerializerSettings
{
MissingMemberHandling = MissingMemberHandling.Ignore,
NullValueHandling = NullValueHandling.Ignore
};
try
{
var result = JsonConvert.DeserializeObject<T>(text, settings);
return result ?? default;
}
catch (Exception e)
{
Debug.Log(e);
}
return default;
}
T result = default;
try
{
if (File.Exists(path))
{
var text = File.ReadAllText(path);
result = Deserialize<T>(text);
}
}
catch (Exception e)
{
Debug.LogException(e);
}
return result;
Unfortunately, there is no easy way to store data persistently between play sessions in Unity. PlayerPrefs and creating your own JSON file are the simplest ways of doing this.
The good news is that JSON files are quite easy to make and modify, thanks to the builtin JSONUtility Unity provides.
If you make a separate class or struct to hold your scores, give that clas a [Serializable] tag and keep a reference to that in your current setup (which is probably a MonoBehaviour).
You can use the File class (specifically File.CreateText() and File.OpenText()) to write to/read from a file. If you do this every time a value changes, you should end up with persistent saved data across multiple play sessions.

Meteor - alter data on publish

I am doing a card game in Meteor. Each played game is registered in the collection "Games".
A Games' record contain some of the game info as well as some players info and the cards hand of the players (players[i].cards, with values like 4S - 4 of spade, KH - king of heart...). Depending on the cards value, I can display the correct card to the frontend (div.card-KH).
Most of the game data is public but, of course, only the current player's cards must be visible.
One option could be to exclude the cards of the other players at publish time but I still need the number of cards to display the decks (only the remaining cards, hidden) of the other players.
Is there a way to replace the card value of each cards (except user's ones) in players[i].cards to, say, BACK (div.card-BACK) at publish time? Else, what would be the good design pattern to do this in Meteor?
Interesting question which I think there should be an official guideline to.
They have a bit in the docs about publishing:
http://docs.meteor.com/#/full/meteor_publish (secretinfo field only published to admins)
You could do the same but you would have to check the userId and have a player1secretinfo, player2secretinfo etc. (or perhaps a secretinfo array/object but then you would have to only publish one particular index/property)
You could also do a transform in the publications:
https://www.eventedmind.com/items/meteor-transforming-collection-documents
Please post it, if you find a good solution :)
Here is the only solution I found (I don't really like it but it works) :
Basically, you have to store a temporary document, with the same id but modified data, to another Collection, publish that document from that Collection and finally, from client side, subscribe to that publication.
Of course, never make hidden part of the original document available through another publication.
Meteor.publish('gameWithHiddenCards', function (gameId) {
var self = this;
//Transform function
var transform = function(item) {
for (var i=0; i<item.players.length; i++){
if ((item.players[i].id != self.userId))
for (var j = 0; j < item.players[i].cards.length; j++)
item.players[i].cards[j] = 'back';
return item;
};
/* 1. Observe changes in game ;
2. Apply transform function on game data ;
3. Store it to another Collection. */
var observer = Games.find({_id: gameId}).observe({
added: function (document) {
self.added('secure_games', document._id, transform(document));
},
changed: function (newDocument, oldDocument) {
self.changed('secure_games', newDocument._id, transform(newDocument));
},
removed: function (oldDocument) {
self.removed('secure_games', oldDocument._id);
}
});
self.onStop(function () {
observer.stop();
});
// Return altered game
return SecureGames.find({_id: gameId});
});

How to reject numeric values in Lucene.net?

I want to know whether is it possible to reject numeric phrases or numeric values while indexing or searching in Lucene.net.
For example (this is one line),
Hi all my no is 4756396
Now, when I index or search it should reject the numeric value 4756396 to be indexed or searched. I tried making a custom stop word list with 1, 2, 3, 4, 5, 6, etc, but I guess it will only ignore if a single number will appears.
You can copy the StandardAnalyzer and customize the grammar (simple JFlex stuff) to reject number tokens. If you do that, you'll need to port back the analyzer to Java since JFlex will generate java code, tho you could give it a try with C# Flex.
You could also write a TokenFilter that scans tokens one by one and rejects them if they are numbers. If you wanna filter only whole numbers and still retain numbers that are for example separate by hyphens, the filter could simply attempt a double.TryParse() and if it fails you accept the Token. A more robust and customizable solution would still use a lexical parser.
Edit:
Heres a quick sample of what I mean, with a little main method that shows how to use it. In this I used a TryParse() to filter out tokens, if it were for a more complex production system I'd use a lexical parser system. (take a look at C# Flex for that)
public class NumericFilter : TokenFilter
{
private ITermAttribute termAtt ;
public NumericFilter(TokenStream tokStream)
: base(tokStream)
{
termAtt = AddAttribute<ITermAttribute>();
}
public override bool IncrementToken()
{
while (base.input.IncrementToken())
{
string term = termAtt.Term;
double res ;
if(double.TryParse(term, out res))
{
// skip this token
continue;
}
// accept this token
return true;
}
// no more token in the stream
return false;
}
}
static void Main(string[] args)
{
RAMDirectory dir = new RAMDirectory();
IndexWriter iw = new IndexWriter(dir, new KeywordAnalyzer(), IndexWriter.MaxFieldLength.UNLIMITED);
Document d = new Document();
Field f = new Field("text", "", Field.Store.YES, Field.Index.ANALYZED);
d.Add(f);
// use our Filter here
f.SetTokenStream(new NumericFilter(new LowerCaseFilter(new WhitespaceTokenizer(new StringReader("I have 300 dollars")))));
iw.AddDocument(d);
iw.Commit();
IndexReader reader = iw.GetReader();
// print all terms in the text field
TermEnum terms = reader.Terms(new Term("text", ""));
do
{
Console.WriteLine(terms.Term.Text);
}
while (terms.Next());
reader.Dispose();
iw.Dispose();
Console.ReadLine();
Environment.Exit(42);
}

Getting line locations with iText

How can one find where are lines located in a document with iText?
Suppose say I have a table in a PDF document, and want to read its contents; I would like to find where exactly the cells are located. In order to do that I thought I might find the intersections of lines.
I think your only option using iText will be to parse the PDF tokens manually. Before doing that I would have a copy of the PDF spec handy.
(I'm a .Net guy so I use iTextSharp but other than some capitalization differences and property declarations they're almost 100% the same.)
You can get the individual tokens using the PRTokeniser object which you feed bytes into from calling getPageContent(pageNum) on your PdfReader.
//Get bytes for page 1
byte[] pageBytes = reader.getPageContent(1);
//Get the tokens for page 1
PRTokeniser tokeniser = new PRTokeniser(pageBytes);
Then just loop through the PRTokeniser:
PRTokeniser.TokType tokenType;
string tokenValue;
while (tokeniser.nextToken()) {
tokenType = tokeniser.tokenType;
tokenValue = tokeniser.stringValue;
//...check tokenValue, do something with it
}
As far a tokenValue, you'd want to probably look for re and l values for rectangle and line. If you see an re then you want to look at the previous 4 values and if you see an l then previous 2 values. This also means that you need to store each tokenValue in an array so you can look back later.
Depending on what you used to create the PDF with you might get some interesting results. For instance, I created a 4 cell table with Microsoft Word and saved as a PDF. For some reason there are two sets of 10 rectangles with many duplicates, but the general idea still works.
Below is C# code targeting iTextSharp 5.1.1.0. You should be able to convert it to Java and iText very easily, I noted the one line that has .Net-specific code that needs to be adjusted from a Generic List (List<string>) to a Java equivalent, probably an ArrayList. You'll also need to adjust some casing, .Net uses Object.Method() whereas Java uses Object.method(). Lastly, .Net accesses properties without gets and sets, so Object.Property is both the getter and setter compared to Java's Object.getProperty and Object.setProperty.
Hopefully this gets you started at least!
//Source file to read from
string sourceFile = "c:\\Hello.pdf";
//Bind a reader to our PDF
PdfReader reader = new PdfReader(sourceFile);
//Create our buffer for previous token values. For Java users, List<string> is a generic list, probably most similar to an ArrayList
List<string> buf = new List<string>();
//Get the raw bytes for the page
byte[] pageBytes = reader.GetPageContent(1);
//Get the raw tokens from the bytes
PRTokeniser tokeniser = new PRTokeniser(pageBytes);
//Create some variables to set later
PRTokeniser.TokType tokenType;
string tokenValue;
//Loop through each token
while (tokeniser.NextToken()) {
//Get the types and value
tokenType = tokeniser.TokenType;
tokenValue = tokeniser.StringValue;
//If the type is a numeric type
if (tokenType == PRTokeniser.TokType.NUMBER) {
//Store it in our buffer for later user
buf.Add(tokenValue);
//Otherwise we only care about raw commands which are categorized as "OTHER"
} else if (tokenType == PRTokeniser.TokType.OTHER) {
//Look for a rectangle token
if (tokenValue == "re") {
//Sanity check, make sure we have enough items in the buffer
if (buf.Count < 4) throw new Exception("Not enough elements in buffer for a rectangle");
//Read and convert the values
float x = float.Parse(buf[buf.Count - 4]);
float y = float.Parse(buf[buf.Count - 3]);
float w = float.Parse(buf[buf.Count - 2]);
float h = float.Parse(buf[buf.Count - 1]);
//..do something with them here
}
}
}

How Do I detect Text and Cursor position changes in Word using VSTO

I want to write a word addin that does some computations and updates some ui whenever the user types something or moves the current insertion point. From looking at the MSDN docs, I don't see any obvious way such as an TextTyped event on the document or application objects.
Does anyone know if this is possible without polling the document?
Actually there is a way to run some code when a word has been typed, you can use SmartTags, and override the Recognize method, this method will be called whenever a word is type, which means whenever the user typed some text and hit the space, tab, or enter keys.
one problem with this however is that if you change the text using "Range.Text" it will detect it as a word change and call the function so it can cause infinite loops.
Here is some code I used to achieve this:
public class AutoBrandSmartTag : SmartTag
{
Microsoft.Office.Interop.Word.Document cDoc;
Microsoft.Office.Tools.Word.Action act = new Microsoft.Office.Tools.Word.Action("Test Action");
public AutoBrandSmartTag(AutoBrandEngine.AutoBrandEngine _engine, Microsoft.Office.Interop.Word.Document _doc)
: base("AutoBrandTool.com/SmartTag#AutoBrandSmartTag", "AutoBrand SmartTag")
{
this.cDoc = _doc;
this.Actions = new Microsoft.Office.Tools.Word.Action[] { act };
}
protected override void Recognize(string text, Microsoft.Office.Interop.SmartTag.ISmartTagRecognizerSite site,
Microsoft.Office.Interop.SmartTag.ISmartTagTokenList tokenList)
{
if (tokenList.Count < 1)
return;
int start = 0;
int length = 0;
int index = tokenList.Count > 1 ? tokenList.Count - 1 : 1;
ISmartTagToken token = tokenList.get_Item(index);
start = token.Start;
length = token.Length;
}
}
As you've probably discovered, Word has events, but they're for really coarse actions like a document open or a switch to another document. I'm guessing MS did this intentionally to prevent a crappy macro from slowing down typing.
In short, there's no great way to do what you want. A Word MVP confirms that in this thread.