Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by WillyJenkins · May 30, 2013 at 08:35 PM · coroutinewwwdatabaseonapplicationquit

How to save to a database on application close

I'm currently working on a multiplayer game that requires player attributes and preferences to be saved to an external online database. I've heard a few people mention a NoSQL object database method over MySQL but for the time being my web hosting's MySQL database will suffice.

Anyway, my question is regarding intervals at which the game saves the users state (position, experience, level, currency, etc) to the database. My initial thoughts where to do this every so many seconds (any insight onto a feasible time interval to do this would be much appreciated, at the moment its just every 10 seconds for testing), on logout and on application close. Logout is fine, when they click a button, save the state, however I'm struggling to figure out how to save the state on close.

At the moment my save state script contains an OnApplicationQuit method which works and calls my save state function. The problem is that the function utilises a WWW object inside a Coroutine, this takes a little time to complete and the application closes before the state of the character can be saved. Is there a way to postpone quit until the WWW object has successfully saved the changes? Or is there another option you would recommend?

Thanks in advance.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Memige · May 30, 2013 at 10:02 PM

Edit** Added example at bottom for clarification

OnApplicationQuit is merely a callback, and unfortunately you cannot reliably run any delayed logic in it. As you would run into a similar problem in an accidental exit (i.e. a crash) I would concur with the interval idea. What I would recommend would be to register significant data changes to a listener class that will stock pile these delta changes until the interval is reached, then at the interval check to see if it has new data. This way it will contact the database no more often than the interval you set, while also skipping a round if no new data is available, this way you aren't needlessly contacting your database when there are no significant changes. You will occasionally lose data (if a player accomplishes something just before a crash) but you can tweak your interval to minimize the impact. As long as the player doesn't lose more than a couple minutes of progress they likely won't get upset.

Example:

Interval set to 2 minutes

After 1 minute the player accomplishes something significant (got a new weapon) this is registered to the listener class. Database is not called.

After 1 Miunte and 45 seconds player gets 500 gold, this is registered to the class. Database is not called.

2 Minute mark, Listener class sees that it has data and we save the new weapon and gold to the database.

nothing happens for the next 2 minutes

4 minute mark, Listener class sees that it does -not- have data and does -not- contact database

4 minutes and 20 seconds, player earns 200 gold

4 minutes and 25 seconds, program crashes

Player reboots game, they still have their weapon and the 500 gold, but not the 200

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Graham-Dunnett · May 30, 2013 at 08:37 PM

Depends on the platform, but maybe `CancelQuit`?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image WillyJenkins · May 30, 2013 at 09:54 PM 0
Share

It will be the standalone player, so I guess it will work fine when used in an actual build. Rather than using a specific timeout could I just use 'yield return www' so it waits until a value is returned from the WWW object and my PHP script? On a tangent do you have any recommendation on a good interval during gameplay to save the player info?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

NullReferenceException in nested Coroutine 0 Answers

Loading files via WWW class and Unity hangs for a few seconds if I use coroutines. If I don't, it works perfectly. What is going on? 1 Answer

Quit application while downloading with www.loadfromcacheordownload makes application crash 0 Answers

Using a method to get the string from a download. 3 Answers

Connection with a MySQL Database 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges