I try to make savefile as follows;
string savedata ="aaaa"; //crypted string
StreamWriter writer = new StreamWriter(Path, false);
writer.WriteLine(savedata);
writer.Flush();
writer.Close();
But in this way ,if you exit the application before the writing is finished, you may not be able to save the file.
How can we address this issue?
If i understand right, maybe you could make the save in the "OnApplicationQuit()". I think even if the user decide to leave unity will execute this function and wait for it to finish. So your game will be saved anyway and you don't have to change your code much.
Related
I need to load/save the number of coins a user has earned in my Unity game with saved games for Play Games Services.
There is an example on how to save an image on this page: https://developer.android.com/games/pgs/unity/saved-games#write_a_saved_game
Can someone tell me how I can load/save a number instead of an image?
To be precise, I wouldn't say you are exactly saving an image. I mean, you do save it, but only as a cover for your game save file and I don't know if you can retrieve it (maybe you can, I haven't checked that really).
Probably the most important part of using google play save system is byte[] savedData argument. It's just a byte array, and it's up to you what are you going to pass there and how are you going to interpret that data on game load.
There are a lot of ways you could approach it. I personally create a custom GameSave object with all my data that I want to save, then I serialize it using JsonUtility.
string json = JsonUtility.ToJson(gameSave);
After that, I use MemoryStream and BinaryFormatter to convert the data in my json to byte array:
MemoryStream memoryStream = new MemoryStream();
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(memoryStream, json);
byte[] data = memoryStream.GetBuffer();
Then you would have to pass that data to savedGameClient.CommitUpdate method as an argument.
Of course, that's just one of the ways of doing that, you can send something else than json from serialized class object.
Other stuff is pretty well documented, so once you handle that, you should manage to do the rest.
I try to make a spigot plugin that can detect if nobody is on the server. I need this for a timer, if nobody is on the server, it should stop the timer and save the time of the timer in a text file. Is there any way I can do this with a plugin.
Thanks for every help, Aaron.
How to create text files:
try {
File file = new File(Yourplugin.getPlugin(YourPlugin.class).getDataFolder().getPath(), "filename.txt");
file.getParentFile().mkdirs();
file.createNewFile();
} catch (IOException exception){
System.out.println(exception.toString());
}
How to read from text files:
File file = new File(YourPlugin.getPlugin(Yourplugin.class).getDataFolder().getPath(), "filename.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String line = br.readline(); //can be null, if nothing is in file
How to write to Files:
File file = new File(YourPlugin.getPlugin(Yourplugin.class).getDataFolder().getPath(), "filename.txt");
BufferedWriter wr = new BufferedWriter(new FileWriter(file));
br.write("yourtime");
You should make a try catch construction in every example like in the first one.
See Bukkit#getOnlinePlayers(); and check if it is empty. You can check when a player disconnects from the server, or you can do a repeating scheduled task.
To save the timer in a yml file, use YamlConfiguration. See here for more details : https://www.spigotmc.org/wiki/config-files/#creating-the-file
If you need more help (and a quicker answer, probably) you can add me on discord: Pierre#7757. I'll edit this comment if we find any good solution.
I'm looking for an easy way too save the current state of my GUI and be able to load this saved state again. I know there is a video for this (http://blogs.mathworks.com/videos/2010/12/10/how-to-save-and-restore-state-of-a-gui-in-matlab/) but somehow it doesn't work on my computer.
I thought of something like this: (I used gcf because I didn't know the "mainhandle" for my entire GUI)
%Save Data
currentdata = getappdata(gcf);
uisave('currentdata',date);
%Load Data
[filename,pathname]=uigetfile({'*.mat'},'Select input file');
load([pathname,filename]);
The problem with this way is, that the saved Data is opened in a new figure and not the current one from which I chose to open it.
I was also wondering if it is possible to set a folder(which is added to the matlab path) as the deault folder for saving/loading.
Thank you for your help! Klaus
Since your currentdata keeps the whole handles of your GUI, including the handle to the GUI's main figure, so when you load it, there will be a new figure opened.
What one did in the mentioned video is: just save the fields/data that you need to be remained/reloaded in later session.
So either you save these specific fields in the GUI's handles and reload them one by one, or you can use the exchange functions mentioned in the comment under the video.
I have developed a C# application, in the application the users choose a photo for each record. However the user should also be able to change the pre-selected photo with a newer one. When the user changes the photo the application first deletes the old photo from the application directory then copies the new photo, but when it does that the application gives an exception because the file is used by the application so it cannot be deleted while the application is running. Does any one have a clue how to sort this out? I appreciate your help
This is the exception
The process cannot access the file
'D:\My
Projects\Hawkar'sProject\Software\Application\bin\Debug\Photos\John
Smith.png' because it is being used by
another process.
//defining a string where contains the file source path
string fileSource = Open.FileName;
//defining a string where it contains the file name
string fileName = personNameTextBox.Text + ".png" ;
//defining a string which specifies the directory of the destination file
string fileDest = dir + #"\Photos\" + fileName;
if (File.Exists(fileDest))
{
File.Delete(fileDest);
//this is a picturebox for showing the images
pbxPersonal.Image = Image.FromFile(dir + #"\Photos\" + "No Image.gif");
File.Copy(fileSource, fileDest);
}
else
{
File.Copy(fileSource, fileDest);
}
imageIDTextBox.Text = fileDest;
First of all, you code is not good.
The new image is only copied if there is currently no image (else).
But if there is an old image, you only delete this image, but never copy the newer one (if).
The code should better look like this:
if (File.Exists(fileDest))
{
File.Delete(fileDest);
}
File.Copy(fileSource, fileDest);
imageIDTextBox.Text = fileDest;
This code should work, but if you are getting an exception that the file is already in use, you should check "where" you use the file. Perhaps you are reading the file at program start. Check all parts of your program you are accessing these user files if there are some handles open.
Thanks a lot for your help and sorry for the mistake in the code I just saw it, my original code is just like as you have written but I don't know maybe when I posted accidentally I've put like this. when the application runs there is a picturebox which the image of each record is shown, that is why the application is giving an exception when I want to change the picture because it has been used once by the picturebox , however I've also tried to load another picture to the picture box before deleting the original one but still the same. I've modified the above could if you want to examine it
I am trying to register to a "Device added/ Device removed" event using WMI. When I say device - I mean something in the lines of a Disk-On-Key or any other device that has files on it which I can access...
I am registering to the event, and the event is raised, but the EventType propery is different from the one I am expecting to see.
The documentation (MSDN) states : 1- config change, 2- Device added, 3-Device removed 4- Docking. For some reason I always get a value of 1.
Any ideas ?
Here's sample code :
public class WMIReceiveEvent
{
public WMIReceiveEvent()
{
try
{
WqlEventQuery query = new WqlEventQuery(
"SELECT * FROM Win32_DeviceChangeEvent");
ManagementEventWatcher watcher = new ManagementEventWatcher(query);
Console.WriteLine("Waiting for an event...");
watcher.EventArrived +=
new EventArrivedEventHandler(
HandleEvent);
// Start listening for events
watcher.Start();
// Do something while waiting for events
System.Threading.Thread.Sleep(10000);
// Stop listening for events
watcher.Stop();
return;
}
catch(ManagementException err)
{
MessageBox.Show("An error occurred while trying to receive an event: " + err.Message);
}
}
private void HandleEvent(object sender,
EventArrivedEventArgs e)
{
Console.WriteLine(e.NewEvent.GetPropertyValue["EventType"]);
}
public static void Main()
{
WMIReceiveEvent receiveEvent = new WMIReceiveEvent();
return;
}
}
Well, I couldn't find the code. Tried on my old RAC account, nothing. Nothing in my old backups. Go figure. But I tried to work out how I did it, and I think this is the correct sequence (I based a lot of it on this article):
Get all drive letters and cache
them.
Wait for the WM_DEVICECHANGE
message, and start a timer with a
timeout of 1 second (this is done to
avoid a lot of spurious
WM_DEVICECHANGE messages that start
as start as soon as you insert the
USB key/other device and only end
when the drive is "settled").
Compare the drive letters with the
old cache and detect the new ones.
Get device information for those.
I know there are other methods, but that proved to be the only one that would work consistently in different versions of windows, and we needed that as my client used the ActiveX control on a webpage that uploaded images from any kind of device you inserted (I think they produced some kind of printing kiosk).
Oh! Yup, I've been through that, but using the raw Windows API calls some time ago, while developing an ActiveX control that detected the insertion of any kind of media. I'll try to unearth the code from my backups and see if I can tell you how I solved it. I'll subscribe to the RSS just in case somebody gets there first.
Well,
u can try win32_logical disk class and bind it to the __Instancecreationevent.
You can easily get the required info
I tried this on my system and I eventually get the right code. It just takes a while. I get a dozen or so events, and one of them is the device connect code.