Getting a highscore to Save and Load using My.Settings
Getting a highscore to Save and Load using My.Settings
Getting a highscore to Save and Load using My.Settings
Getting a highscore to Save and Load using My.Settings
Getting a highscore to Save and Load using My.Settings
Getting a highscore to Save and Load using My.Settings Getting a highscore to Save and Load using My.Settings Getting a highscore to Save and Load using My.Settings Getting a highscore to Save and Load using My.Settings Getting a highscore to Save and Load using My.Settings Getting a highscore to Save and Load using My.Settings Getting a highscore to Save and Load using My.Settings Getting a highscore to Save and Load using My.Settings
Getting a highscore to Save and Load using My.Settings Getting a highscore to Save and Load using My.Settings
Getting a highscore to Save and Load using My.Settings
Go Back  Xtreme Visual Basic Talk > > > Getting a highscore to Save and Load using My.Settings


Reply
 
Thread Tools Display Modes
  #1  
Old 05-20-2011, 06:31 AM
45minutes 45minutes is offline
Regular
 
Join Date: Jan 2011
Posts: 75
Default Getting a highscore to Save and Load using My.Settings


Hi again, I've tried to implement this highscore saving feature, so that when my application closes, the highscore saves within the my.settings storage function, and when my program loads up again, the same highscore loads up from this file. atm, it works ONLY within the application (I can close forms and it's still there, but if i end the program it resets to 0 next time it's loaded).

it's basically 2 lines of code

1 for saving and 1 for loading

is this the best way to save a highscore for a game? I was initially thinking of making a database and linking it to that, but apprantly it's easier with the My.settings. even thought of using stremawriter but it's more uneccessary work over my.settings.

code for my form to save highscore:

Code:
Public Class SnakeX_SP_UserHighscore

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


        My.Settings.Name1_SSP = Highscore1.Text 'saves a new user name as a setting
        Highscore1.Text = My.Settings.Highscore1_SSP ' inputs highscore1
        Me.Close()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Close()
    End Sub

    Private Sub HighscoreUserInput_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Name1.Text = SystemInformation.UserName
        Name1.ReadOnly = True
        Highscore1.Text = My.Settings.Highscore1_SSP



    End Sub
End Class


In my main form load event i have:

   numhighscore.Text = My.Settings.Highscore1_SSP ' loads highscore from settings
thanks
Reply With Quote
  #2  
Old 05-20-2011, 09:18 AM
AtmaWeapon's Avatar
AtmaWeaponGetting a highscore to Save and Load using My.Settings AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

That's kind of odd; usually the settings file is saved when your program closes.

For good measure, handle each form's Closing event and call My.Settings.Save(). I think it's supposed to happen automatically, but forcing it can't hurt.

Here's another thought: what's the data type of My.Settings.Highscore1_SSP? If it's a string, all should be well. If it's an integer, there could be a conversion problem going on. I won't explain it in detail until you confirm that.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #3  
Old 05-20-2011, 09:37 PM
45minutes 45minutes is offline
Regular
 
Join Date: Jan 2011
Posts: 75
Default

Quote:
Originally Posted by AtmaWeapon View Post
That's kind of odd; usually the settings file is saved when your program closes.

For good measure, handle each form's Closing event and call My.Settings.Save(). I think it's supposed to happen automatically, but forcing it can't hurt.

Here's another thought: what's the data type of My.Settings.Highscore1_SSP? If it's a string, all should be well. If it's an integer, there could be a conversion problem going on. I won't explain it in detail until you confirm that.
Oh, that could be it actually, Highscore1_SSP is an integer, but I've also declared highscore to be an integer, so it should be working. I'll give it a shot.

EDIT: I went to the drop down menu, and couldn't find a closing event for the form??

I am now becoming frustrated with this method as it isn't working. Do you know any database management ways in which i could store a couple of variables? thanks

Last edited by 45minutes; 05-21-2011 at 09:35 AM.
Reply With Quote
  #4  
Old 05-22-2011, 10:43 AM
AtmaWeapon's Avatar
AtmaWeaponGetting a highscore to Save and Load using My.Settings AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

Are you using VS 2008 or VS 2010? This is really easy to demonstrate but I don't want to waste time on the wrong version.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #5  
Old 05-23-2011, 02:20 AM
45minutes 45minutes is offline
Regular
 
Join Date: Jan 2011
Posts: 75
Default

Quote:
Originally Posted by AtmaWeapon View Post
Are you using VS 2008 or VS 2010? This is really easy to demonstrate but I don't want to waste time on the wrong version.
VS 2010.

I was following this guide on how to connect a database to a vb application. I was using a provider called "JET" but as I went to debug, it said JET is not registered on this local machine and soon found out that JET isn't included on 64 bit machines, which will cause compatibility issues with my program. I don't want. I later compiled my program to run as a x86 (32 bit) application, but some code for a button wasn't being executed properly and was being displayed as .enabled = True, when I had it at .enabled = False upon form load.

website I was following: http://www.homeandlearn.co.uk/net/nets12p4.html

I'm am now thinking of using stream writer, another thread I saw yesterday on here, I will follow it.
Reply With Quote
  #6  
Old 05-23-2011, 07:11 AM
PlausiblyDamp's Avatar
PlausiblyDampGetting a highscore to Save and Load using My.Settings PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

If you are using VS 2010 you might find SqlExpress a better choice over access as the relevant prerequisites can be deployed along with your application - they are also provided in both 32 bit and 64 bit versions.

Equally if you build the app as 32 bit (x86) rather than the default of AnyCpu it will still run as a 32 bit executable even on a 64 bit machine and therefore just will work just fine.

Without knowing exactly how the button visibility should be changed it is hard to say what the problem there might be, however I wouldn't consider replacing a database with a simple text file just over a UI glitch. Databases have a lot more functionality that a simple file and in the long run you will probably want to take advantage of these features.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #7  
Old 05-23-2011, 09:37 AM
AtmaWeapon's Avatar
AtmaWeaponGetting a highscore to Save and Load using My.Settings AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

I've attached a program that demonstrates saving a high score in My.Settings. You can study it and try to figure out why yours isn't working. You'd get the most benefit if you post your code, because then we could tell you *exactly* what's wrong.

The application displays a high score (0 at the start.) The "game" consists of entering a number in the text box and pushing "Submit". The number is your score. If your high score beats the current best, it is saved to the settings and the label is updated.

I don't really need to call My.Settings.Save() in the FormClosing handler; if you look at the project properties you'll find a "Save My.Settings on shutdown" checkbox that is checked. That usually works. In general I've found that the My.Settings class usually saves stuff as soon as you change a setting. Sometimes it doesn't. When in doubt, if you call Save() it will save its settings.

There's really not much to it so if you're not getting your settings persisted you're either:
  • Not updating a control after you change the settings.
  • Not storing the new value in the settings.

With respect to using a database, I say ick. If you use a plain old text file, you don't really need an installer and if the file gets corrupted you can probably fix it. When you decide to use a database, it gets an order of magnitude more complicated. First, you have to figure out how to deploy the database manager on the user's machine; for SQL Express this involves installing the SQL Express server. Now you can't get by without an installer, and users might be agitated they have to run a whole database server for your game (I could be wrong here; I think there's a way to make SQL Express file-based; MS isn't advertising it well enough if so.) Then, you have to worry about a myriad of scenarios like, "What if the user's security software is blocking connections to the SQL server?" If the high score table gets corrupted, that's that. If the user's lucky your program will notice and fix it. There's other databases I'd recommend instead but they have similar problems. For example, SQLite doesn't use a server daemon and is file-based, so it's much more lightweight than most other solutions. But since it's not an MS product it's not built in to .NET, so you'll have to distribute the adapter libraries with your application; again it makes you require an installer.

That's not even touching the order of magnitude of complexity that data access code introduces on top of a plain old text file. Relational tables and SQL just plain stink for this kind of thing. (Document-based DBs like MongoDB are more attractive, but are still overkill.)
Attached Files
File Type: zip HighScoreDemo.zip (12.4 KB, 35 views)
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #8  
Old 05-24-2011, 04:02 AM
45minutes 45minutes is offline
Regular
 
Join Date: Jan 2011
Posts: 75
Default

Quote:
Originally Posted by AtmaWeapon View Post
I've attached a program that demonstrates saving a high score in My.Settings. You can study it and try to figure out why yours isn't working. You'd get the most benefit if you post your code, because then we could tell you *exactly* what's wrong.

The application displays a high score (0 at the start.) The "game" consists of entering a number in the text box and pushing "Submit". The number is your score. If your high score beats the current best, it is saved to the settings and the label is updated.

I don't really need to call My.Settings.Save() in the FormClosing handler; if you look at the project properties you'll find a "Save My.Settings on shutdown" checkbox that is checked. That usually works. In general I've found that the My.Settings class usually saves stuff as soon as you change a setting. Sometimes it doesn't. When in doubt, if you call Save() it will save its settings.

There's really not much to it so if you're not getting your settings persisted you're either:
  • Not updating a control after you change the settings.
  • Not storing the new value in the settings.

With respect to using a database, I say ick. If you use a plain old text file, you don't really need an installer and if the file gets corrupted you can probably fix it. When you decide to use a database, it gets an order of magnitude more complicated. First, you have to figure out how to deploy the database manager on the user's machine; for SQL Express this involves installing the SQL Express server. Now you can't get by without an installer, and users might be agitated they have to run a whole database server for your game (I could be wrong here; I think there's a way to make SQL Express file-based; MS isn't advertising it well enough if so.) Then, you have to worry about a myriad of scenarios like, "What if the user's security software is blocking connections to the SQL server?" If the high score table gets corrupted, that's that. If the user's lucky your program will notice and fix it. There's other databases I'd recommend instead but they have similar problems. For example, SQLite doesn't use a server daemon and is file-based, so it's much more lightweight than most other solutions. But since it's not an MS product it's not built in to .NET, so you'll have to distribute the adapter libraries with your application; again it makes you require an installer.

That's not even touching the order of magnitude of complexity that data access code introduces on top of a plain old text file. Relational tables and SQL just plain stink for this kind of thing. (Document-based DBs like MongoDB are more attractive, but are still overkill.)
Hi, thank you so much, i had a play around with my code and after attempting many alternations. I still didn't work. So i ended up putting in the formclosing event in, and used the My.settings.save and NOW IT WORKS Thanks so much. that 1 line of code turns out to be very important.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Getting a highscore to Save and Load using My.Settings
Getting a highscore to Save and Load using My.Settings
Getting a highscore to Save and Load using My.Settings Getting a highscore to Save and Load using My.Settings
Getting a highscore to Save and Load using My.Settings
Getting a highscore to Save and Load using My.Settings
Getting a highscore to Save and Load using My.Settings Getting a highscore to Save and Load using My.Settings Getting a highscore to Save and Load using My.Settings Getting a highscore to Save and Load using My.Settings Getting a highscore to Save and Load using My.Settings Getting a highscore to Save and Load using My.Settings Getting a highscore to Save and Load using My.Settings
Getting a highscore to Save and Load using My.Settings
Getting a highscore to Save and Load using My.Settings
 
Getting a highscore to Save and Load using My.Settings
Getting a highscore to Save and Load using My.Settings
 
-->