- Home /
Saving informations from game
Hi, hope someone can help me. I have created a game for Webplayer that works so far. For saving things like highscores I've used Playerprefs. But additonally I need to save all kinds of data, e.g. how often the player is game over, how often the player moves left or right,... But this kinds of data I only need to be saved and not loaded when starting the game again. This data should be stored in one file (one plays the game and all data is stored in this file. When someone else plays the game all the data from this game should be stored below the existing data.) Is it possible to save all this information to a csv file and have access to this file
Thanks
Answer by Rabbit · Oct 06, 2013 at 02:16 PM
You can easily save data to a CSV using c#, this is pretty self explanatory :)
using UnityEngine;
using System.Collections;
using System.Text;
using System.IO;
public class Save : MonoBehaviour {
void Start () {
Savecsv();
}
void Savecsv() {
string filePath = @"/Saved_data.csv";
string delimiter = ",";
string[][] output = new string[][]{
new string[]{"Col 1 Row 1", "Col 2 Row 1", "Col 3 Row 1"},
new string[]{"Col1 Row 2", "Col2 Row 2", "Col3 Row 2"}
};
int length = output.GetLength(0);
StringBuilder sb = new StringBuilder();
for (int index = 0; index < length; index++)
sb.AppendLine(string.Join(delimiter, output[index]));
File.WriteAllText(filePath, sb.ToString());
}
}
Also another approach since your using the webplayer you could keep it on your server using PHP
<?php
$allowed = @$_GET["key"];
$data_to_save = $_GET["data"];
if($allowed == "secretkey") {
$myFile = "saved_data.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh,$data_to_save);
fclose($fh);
} else {
return;
}
?>
Peace, Love, Happiness. Wazza.
is this also possible in javascript? and the script below gives me an error: System.IO.File' does not contain a definition for `WriteAllText'
To avoid mistakes with PHP (since it's clunky and likes to break for no reason sometimes) you should add in a check if the file exists, and if it does, unset($file)
and then write to $file.
You forgot to put
using System.Text;
using System.IO;
at the top of the c# script
using System.Text; using System.IO;
is at the top of the script an i always get the error error CS0117: System.IO.File' does not contain a definition for
WriteAllText'
There is no reason that error should be showing up if you have them included, so you must have done something wrong with copying it over.
Answer by tw1st3d · Oct 06, 2013 at 04:08 PM
So this is a little something I threw together that creates an un-encrypted file with saved data. Not fully finished, but it reads a line and returns it from the file you specify, so it should be a great starting point.
using UnityEngine;
using System.Collections;
using System;
using System.IO;
namespace Load_Save
{
public class saveGame
{
public String fileName, saveName, Town;
public int level, health, mana, xp;
public Hashtable vars = new Hashtable();
public void setFileName(String name)
{
fileName = name;
setSaveName();
}
public void setSaveName()
{
String[] lines = File.ReadAllLines(fileName);
String[] vars = new String[250];
int count = 0;
foreach(String i in lines)
{
String[] theVars = i.Split('=');
String theData = theVars[0] + " " + theVars[1];
vars[count] = theData;
count++;
}
saveName = vars[0].ToString();
}
public String getSaveName()
{
return saveName;
}
public int getLevel()
{
return level;
}
}
}
Answer by Rabbit · Oct 07, 2013 at 04:21 AM
OK webplayer cannot access the clients computer it's sandboxed, your going to have to do it using php -> create a file - append data to it. I have just tested this and works fine, of course you have to add security to the php but this will get you going.
This php script will create a text file named by the data received.
<?php
$name_to_save = $_GET["name"];
$data_to_save = $_GET["data"];
$myFile = $name_to_save . ".txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh,$name_to_save . "," . $data_to_save . ",");
fclose($fh);
?>
Now the C# script (sorry i only know c#)
using UnityEngine;
using System.Collections;
public class SaveOnline : MonoBehaviour {
public string postDataURL = "http://YOUR URL HERE/index.php?"; //add a ? to your url
public string Playername;
public string Playerdata;
void Start()
{
Playername = "PlayerName";
Playerdata = "SomeData";
StartCoroutine(PostData(Playername,Playerdata));
}
IEnumerator PostData(string name, string data)
{
//This connects to a server side php script that will write the data
string post_url = postDataURL + "name=" + WWW.EscapeURL(name) + "&data=" + data ;
// Post the URL to the site and create a download object to get the result.
WWW data_post = new WWW(post_url);
yield return data_post; // Wait until the download is done
if (data_post.error != null)
{
print("There was an error saving data: " + data_post.error);
}
}
}
If your having cut and paste problems PM me :) Peace. Love. Happiness. Wazza.