-1

I have a cookie clicker like game. How would I make so that my score in my clicker game saves to a file on close. Code for score: int points = 0; Code for exit: this.Close();

  • How did you write this game. Is it a web site, a Unity game, a Windows Forms app, WPM, Xamarin, a Windows Store app, something else? If the entire code for score is `int points = 0;`, you don't really need to store anything, just set it back to zero when you restart (yeah, I know that's not what you meant - but what you said means nothing more than that). What's a "cookie clicker like"? – Flydog57 Apr 10 '21 at 00:52
  • @Flydog57 In visual studio, I want whatever score was last made saved if you get what I mean, cookie clicker is just a idle clicker game you click a button and your score goes up, Image: https://prnt.sc/118l38z – quinncodes Apr 10 '21 at 00:56
  • Sorry, but saying "in Visual Studio" isn't really saying anything. How did you start project you are working on (Visual Studio asks what template you want to use to create your project)? If you show enough code that we can see the `using System.This.That;` statements at the top of your C# program, it will give us a hint. The reason why everyone here really insists that you show your code (preferably with a [mcve]) is so that we can understand what you are doing and what you are asking – Flydog57 Apr 10 '21 at 01:02
  • @Flydog57 I used windows forum template. Also heres my full code, (dont critize the messiness lol): https://pastebin.com/BEBKhcvn – quinncodes Apr 10 '21 at 01:04
  • The best thing is to edit your question and paste your code in to the body of the question. The `{}` button will indent it 4 spaces will identify it as code. That way, people can copy all or some of your code and try it out. – Flydog57 Apr 10 '21 at 03:32
  • Here's a hint... If you wire up the `FormClosing` event of your form (open the form's properties, click on the Events icon (a lightening bolt) and then find the FormClosing event. Click in the property value field and press return. That will create a stub event handler. I tend to store data in _IsolatedStorage_ (as a JSON or XML file). You could also store it the `SpecialFolders.ApplicationData` folder. – Flydog57 Apr 10 '21 at 03:38

1 Answers1

1

You're using Windows forms

Right click the project and choose Properties

Click Settings on the left

Click "create one" if you see "your project doesn't contain a settings file"

Add a row with name=Score, type=int, scope=User, value=0

In code increment the score: Properties.Settings.Default.Score = 1234

In FormClosing event (go to Form Designer, click on form background, look in properties grid, in lightning bolt, FormClosing line, double click it) write Properties.Settings.Default.Save()

(And have a corollary Load in your form constructor)

Caius Jard
  • 72,509
  • 5
  • 49
  • 80